question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

matplotlib rc('text', usetex=True) breaks plotting from .ipynb to .py (jupyter --> spyder)

See original GitHub issue

Description of your problem

I loaded a .py file from jupyter to use it in spyder. But the plotting does not work anymore. I narrowed it down to the rc(‘text’, usetex=True) command. I asked this on stackoverflow:

Original Question: http://stackoverflow.com/questions/40266888/migrating-jupyter-notebook-to-py-file-spyder-basic-plotting-is-killed-by-late

What steps will reproduce the problem?

create a .py script in spyder:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs

from matplotlib import rc # this is the matplotlib suggestion
rc('font',**{'family':'sans-serif','sans-serif':['Arial']})
rc('text', usetex=True)

mat = np.random.rand(5,5)

fig = plt.figure()
gsall = gs.GridSpec(6, 1)
ax1 = fig.add_subplot(gsall[:])
source1 = ax1.contourf(mat,extend='both',cmap=mpl.cm.viridis)
plt.title('Random field \n test')
plt.colorbar(source1)
plt.show()

run it.

What is the expected output? What do you see instead?

I expect a figure with the random matrix and the text, but I get many errors.

Please provide any additional information below

In jupyter notebook everything is fine. In ipython shell I get a figure without the text and no errors.

Versions and main components

  • Spyder Version: 2.3.9
  • Python Version: 3.5.1
  • Operating system: macOS sierra v.10.12.1
  • Matplotlib Version: 1.5.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ccordoba12commented, Sep 16, 2018

@ImportanceOfBeingErnest, if you define or extend PATH in your .bashrc and don’t start Spyder in a console but using a graphical menu or launcher, then Spyder is unable to detect those modifications to PATH because it doesn’t read your environment variables before launching. By the way, Pycharm also has this problem, as reported here:

https://stackoverflow.com/a/9706384/438386

And I think the same happens in the Jupyter notebook. I mean, I haven’t seen any code to read env vars in Jupyter or IPython. But the problem could be alleviated there because you usually start the notebook server in a console, and so their environment is picked up by the kernels started by it.

To summarize, I don’t know if you could say this is a Spyder problem or people not knowing how env vars work.

1reaction
malliwi88commented, Oct 28, 2016

Hi @louika

I think you only need:

  • Latest version of spyder (3.0.1)
  • sudo apt-get install dvipng (library for manage latex character conversion on matplotlib)

for you can to reproduce this example over Ipython console without problems with your actual anaconda environment.

seleccion_035

I hope this help you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating jupyter notebook to .py file (spyder) basic plotting is ...
I am using Anaconda with python 3.5 on a mac with os sierra. I have an extensive script with several plot commands from...
Read more >
Convert Jupyter Notebook to Python script in 3 ways - MLJAR
Convert Jupyter Notebook to Python script Jupyter Notebook saves files in .ipynb format. It is a JSON with code, Markdown, and outputs.
Read more >
Interactive figures — Matplotlib 3.6.2 documentation
The pyplot module provides functions for explicitly creating figures that include interactive tools ... We recommend using IPython for an interactive shell.
Read more >
Built-in magic commands — IPython 8.7.0 documentation
If you are looking for an IPython version compatible with Python 2.7, ... In particular, running the command 'history -f FILENAME' from the...
Read more >
Python Cheatsheet - Basic Syntax and Usage
Jupyter Notebooks have the file extension ipynb, e.g. Python-Tutorial.ipynb. If you open an *.ipynb file with a simple text editor such as Notepad++, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found