So I've been testing PyPy a while (PyPy3.9 with matplotlib 3.6.3 using conda), and it seems satisfactory so far. Now, I tried using it along with matplotlib on jupyter, because that's what I do every day, and it seems that PyPy handles the matplotlib, or jupyter, far worse than CPython (CPython3.9 with matplotlib 3.6.3). I wrote a short notebook with following three cells,
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5, num=1024)
for i in range(20):
ybase = (np.random.randn(1024)+1j*np.random.randn(1024))*(1/(1+(np.arange(1024)/10)**(4)))
y = np.real(np.fft.fft(ybase))
ydots = [y+0.5*np.random.randn(1024), y*0+0.5]
fig, ax = plt.subplots()
ax.plot(x,y)
ax.errorbar(x, ydots[0], yerr=ydots[1], color='k', ls='', marker='o',
elinewidth=0.3, markersize=3)
ax.set_title(f"{i}")
fig.tight_layout()
This should display 20 instances of matplotlib figures with a line and a scatter. First of all, in both CPython and PyPy, they run without errors within reasonable time, and created 20 instances of empty matplotlib figure. Over time, although it is slow, the jupyter eventually renders the figures I draw when I was using CPython kernel.
However, if I use PyPy kernel, it draws up to ~6 figures yet other 14 figures still blank, and after that, although the loop has been technically finished, kernel is still busy and fails to run any subsequent commands.
The results of this depends on a lot of factors, for instance, when I refreshed the browser tab that displays the notebook may affect how many figures would be drawn in PyPy, but in CPython, although time it takes may vary, it eventually draws all figures regardless of these conditions. This gave me the impression that the PyPy, or the matplotlib on PyPy, does not handle the memory very well. Also, while sometimes it helps if I put gc.collect() at the end of each loop, it does not always work. And even if it succeeds, the controls of matplotlib figures are all dead, so this is not desirable.
So, why does this happen? Can it be resolved by updating a couple of packages?
Related
I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.
I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.
when I use inline plots in iPython (QtConsole), the first plot looks (more or less) fine, but then it gets weirder and weirder. When I plot something several times (so plot, see it displayed, plot again, see output etc.), it looks like it is being overlaid with the skewed previous picture. So after plotting a diagonal line (x=y) 4 times in a row I get something like this
If i right click and export it as svg everything looks good
(Exported PNG picture remains wrecked as the first one).
I guess the problem is similar to https://github.com/ipython/ipython/issues/1866, but I didn't got the upshot of the discussion (it got too technical and complicated for me to follow).
Is there any solution or work around for this issue?
I'm using
python 2.7
matplotlib 1.4.1
IPython 2.1.0
Here is a working example:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
ax.plot(a,a)
ax.axis('off')
if you remove plt.axis('off') line, weird things happen only outside of the axis box.
P.S. Originally I encountered this problem in connection with drawing graphs with networkx. If I use draw from networkx this problem does not occur. If I use draw_networkx, same as described above happens. That might point to the core of the problem... I'm trying to figure out what line of code makes one work better than the other...
After tinkering around with the draw and draw_networkx functions from networkx module, I found the workaround which makes the difference between draw and draw_networkx in this case.
Adding fig.set_facecolor('w') overlays whatever is in the background, so the new plots are started with a white sheet (but not a blank one, I guess).
So new working example is:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
fig.set_facecolor('w')
ax.plot(a,a)
ax.axis('off')
I was wondering why some people put a plt.draw() into their code before the plt.show(). For my code, the behavior of the plt.draw() didn't seem to change anything about the output. I did a search on the internet but couldn't find anything useful.
(assuming we imported pyplot as from matplotlib import pyplot as plt)
plt.show() will display the current figure that you are working on.
plt.draw() will re-draw the figure. This allows you to work in interactive mode and, should you have changed your data or formatting, allow the graph itself to change.
The plt.draw docs state:
This is used in interactive mode to update a figure that has been altered using one or more plot object method calls; it is not needed if figure modification is done entirely with pyplot functions, if a sequence of modifications ends with a pyplot function, or if matplotlib is in non-interactive mode and the sequence of modifications ends with show() or savefig().
This seems to suggest that using plt.draw() before plt.show() when not in interactive mode will be redundant the vast majority of the time. The only time you may need it is if you are doing some very strange modifications that don't involve using pyplot functions.
Refer to the Matplotlib doc, "Interactive figures" for more information.
I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.