matplotlib figures does not appear - matplotlib

matplotlib figures show up fine in Ipython3 (v 6.0.0) but won't appear in either the qtconsole 4.3.0, under python 3.4.6, or the notebook. for example, plt.plot(range(7)) does not appear
and plt.show() does not help
However, if I do %matplotlib inline then I do get the plots in both the notebook and qtconsole.
Separate figure windows used to work for me and I can't figure it out.
thanks,

Related

jupyter & matplotlib - how to turn on and off the interactive mode on a figure

Question
In Jupyter notebook, is it possible to turn off the interactive mode on a figure? The ax.plot() does not show a line when running from the command line python but in a Jupyter notebook cell, it shows up.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7,5))
plt.ioff()
ax.plot([1.6, 2.7]) # <--- Still the line shows up in a jupyter notebook
...
plt.ion()
plt.draw()
plt.show()
Version
$ jupyter notebook --version
6.2.0
from platform import python_version
print(python_version())
3.8.8
Related
matplotlib python inline on/off suggests using %matplotlib but not sure why using the magic command to turn on/off the interactive mode.
It also has a mention below but not sure what it exactly means.
on and ioff set interactive mode, determining if the plot is updated after each plotting command (interactive mode on) or it waits plt.show() (if interactive mode is off). This is independent on inline, that decides if the notebook or the console should display the graphics (this happens any time the plot is updated, meaning at every plotting command if interactive mode is set with ion, or after plt.plot if unset with ioff).
Quibbler might be of some help here:
https://github.com/Technion-Kishony-lab/quibbler
It helps create interactive graphics in matplotlib.
for transparency: I am one of the developers of Quibbler.

matplotlib automatically shows figure

I am using matplotlib version 3.0.3 in Eclipse Oxygen 3 on Windows 10.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, ncols=4) # Figure is shown!?
plt.show() # expected to see figure only here
It seems that whenever a plt method gets called matplotlib already opens the UI.
That is useful while debugging but I usually want to just savefig and not stop to look at the generated image ...
This used to work fine for me on my previous Window 7 laptop (not sure what matplotlib version).
Has something changed in the default behavior which I cannot find documented?
Stepping into the matplotlib code I've discovered that interactive mode was set -- probably by default as I don't have a matplotlibrc file anywhere.
I added matplotlib.interactive(False) at the beginning of main() to solve this.
Apparently I still get the interactive mode when debugging even with the above line, so this is good enough for me (for now).

Jupyter Notebook / Python - Enabling matplotlib?

Within a Jupyter Notebook, why is it necessary to include this line:
%matplotlib inline
This seems to be required for matplotlib to work.
It is not necessary to include the line %matplotlib inline in a jupyter notebook.
Introducing this line selects the backend for matplotlib plotting to be the inline backend. This simplifies a lot of things, such as showing figures. If this backend is selected, a figure will automatically appear in the output once it is meantionned in a cell.
Without having selected the %matplotlib inline backend, you would need to call plt.show() to show the matplotlib output.

Spyder interactive plot : wait for the plot to be closed to continue

I'm working with windows using Spyder, I plot with matplotlib. My problem is that I want to do interactive plot (or sometimes plotting a lot of things) and I want spyder to wait that I close the figure to continue the code (same way as a traditional terminal would).
I tried
plt.ion(),
%mpl TkAgg
before loading matplotlib, Ipython and python console... And I can't find any solution.
If you want an example, the goal is that the "hello" prints only when I close the figure, with Spyder on windows 10.
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()
print("hello")
You need to deactivate Spyder's Matplotlib support by going to
Tools > Preferences > IPython console > Graphics
and deselecting the option called
Activate support
Then you need to change your code like this
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()
print("hello")
to set your backend (TkAgg in this case) by hand before creating your plot.
When I run the code, the desired behaviour of the plot window blocking subsequent code from being executed is already there. So I guess there are some other settings involved. For that reason I also cannot test the following, but I would suppose that you need to turn the interactive mode off before calling show.
import matplotlib.pyplot as plt
plt.ion() # turn interactive on, if it isn't already
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.draw()
# possibly do other stuff that would make the use of interactive mode useful
plt.ioff() # turn interactive off
plt.show()
print("hello")

ipython & matplotlib:chart doesn't show in ipython

Any help with the issue is appreciated. As the following photo shows, I am trying to get an horizontal bar chart and always get instead. I don't know what to do with it.
Reposting as an answer:
If you run the %matplotlib magic in IPython before creating plots, the plots will appear automatically, and won't block the prompt.
We're working with matplotlib so that this won't be necessary in matplotlib 2.0.