In the past I was able to do simple animations with matplotlib with a for loop, but this hasn't worked for some time now.
The standard answer is that you have to turn interactive mode on and/or force a redraw with matplotlib.pyplot.draw(). Here is my minimal working example:
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as mplot
mplot.ion()
fig = mplot.figure(1)
ax = fig.add_subplot(111)
for ii in np.arange(0,10):
x = 200*np.random.rand(30)
ax.plot(x)
mplot.draw()
filename = ("img_%d.png" % ii)
mplot.savefig(filename)
When I run this in Interactive Python Editor, I get one figure at the very end with all the plots in it (this also happens with mplot.show())
When I run this in IPython 3.1 (with Python 3.3.5) from the command line, I get nothing at all.
The mplot.savefig(filename) line does seem to work, as the images are generated.
(It's possible this is a bug in the Qt4 backend.)
Try deleting the line matplotlib.use('Qt4Agg'). Works for me. Also works with matplotlib.use('TkAgg'). So it is a backend problem. There is another way to do animations.
Related
I'm using Spyder v5 within Anaconda and having issues with any styles being applied on charts. Basic chart below (deliberately commented out the sns import to see if it makes any difference -it doesn't)
Basic code below - gives a normal plot as expected
import matplotlib.pyplot as plt
#import seaborn as sns
x_values = [0,1,2,3,4,5,6,7,8,9,10]
x_squared = [x ** 2 for x in x_values]
plt.plot(x_values,x_squared, label = "X-Squared")
plt.plot(x_values,x_cubed, label = "X-Cubed")
plt.show()
If i then try
plt.style.use("seaborn-deep")
plt.plot(x_values,x_squared, label = "X-Squared")
plt.plot(x_values,x_cubed, label = "X-Cubed")
plt.show()
Nothing changes? The style is avaialble (from plt.styles.available) and there is no error when applying the style so something is amiss here.
If i import seaborn then nothing changes
If i try any or all of the following from other solutions in various places in my plt code then nothing much changes (all of the below lines of code work with no errors
%matplotlib inline
plt.rcParams.update(plt.rcParamsDefault)
sns.set()
One time somehow this did seem to force it to change to a seaborn-paper style but that's then the only one i can use and i can't use any other seaborn or other styles at all?? Not sure what forced it to change, think it was sns.set() so I suspect the one it then shows is the default seaborn style, but then why won't it let me use any other styles?
As i'm using SPyder in anaconda the packages are installed to do this plot
I do have Python installed on my base desktop as well... but documentation insists this won't cause a problem and i've not had this problem on any other code ever of having another installation
any tips?
I am using Matplotlib on MacOS with Sulime Text.
I use Python 3.5 and Matplotlib 2.0.
When I work on a figure, I usually have a script that plot the data, and save the figure in a .pdf file with plt.savefig(). Then I use Skim (a pdf viewer) in order to refresh the file each time I modify and run the script. This allows me to set my working layout as clean as: there is one window for the script, and one window for the figure which is automatically refreshing.
I would like to do keep the same layout, but using the Matplotlib figures (because they are interactive). I am looking for a way to use plt.show() but always in the same figure that has been created the first time I've run the script.
For instance:
1. First run
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.figure()
noise = np.random.rand(1, 100)
ax(noise)
plt.show()
2. Following runs
import matplotlib.pyplot as plt
import numpy as np
# This is the super command I am looking for
fig = plt.get_previous_run_figure()
ax = fig.axes
noise = np.random.rand(1, 100)
ax.plot(noise)
plt.draw()
In that case of course, I would have to do a first-run script separately from the main script. Does anyone know if it is possible ?
You want to have multiple consecutive python sessions share a common Matplotlib window. I see no way to share this windows from separate processes, especially when the original owner may terminate at any point in time.
However, you could do something similar to your current workflow in which you have an external pdf viewer to view a output file which you update from multiple python instances.
See this question/answer on how to pickle a matplotlib figure:
Store and reload matplotlib.pyplot object
In every script, output your matplotlib figure as a pickled object, rather than calling plt.show():
import matplotlib.pyplot as plt
import numpy as np
import pickle
ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
pickle.dump(ax, file('myplot.pickle', 'w'))
Then, start a dedicated python session which loads this pickled object and calls plt.show(). Have this script run in a loop, checking for updates of the pickled file on disk, and reloading when necessary:
import matplotlib.pyplot as plt
import pickle
while True:
ax = pickle.load(file('myplot.pickle'))
plt.show()
Alternative
Instead of having separate python sessions, I usually have a single Ipython session in which I run different script. By selecting the same figure windows, I end up with a mostly similar setup as you describe, in which the same figure window is reused throughout the day.
import matplotlib.pyplot as plt
fig = plt.figure(0)
fig.clf()
plt.show()
In principle establishing a connection between two different scripts could be done using a system-wide clipboard. (As far as I know the clipboard in windows and macos are system-wide.)
So the idea can be to set up an application using tk or pyqt, and implement a generic FigureCanvas. This application could have an event listener for changes in the clipboard.
The other main workflow script would then call some function that wraps the current figure into a pickle object and sends it to the clipboard, from where it gets caught by the GUI application, is unpickled and shown in the canvas.
This sounds like a little bit of work, but should meet the very restrictive requirements from the question.
The alternative from Daan worked for me. Here's a bit more code. I used this in a Tkinter interactive GUI for reusing/updating a matplotlib figure window:
fig1 = None
if fig1:
#if exists, clear figure 1
plt.figure(1).clf()
plt.suptitle("New Fig Title", fontsize=18, fontweight='bold')
#reuse window of figure 1 for new figure
fig1 = plt.scatter(points.T[0], points.T[1], color='red', **plot_kwds)
else:
#initialize
fig1 = plt.figure(num=1,figsize=(7, int(7*imgRatio)), dpi=80)
plt.tick_params(axis='both', which='major', labelsize=14)
plt.tick_params(axis='both', which='minor', labelsize=14)
plt.suptitle("Title", fontsize=18, fontweight='bold')
fig1 = plt.scatter(points.T[0], points.T[1], color='red', **plot_kwds)
The figure is reusing the (interactive) plt window. For this to work, I had to set interactive : True in the matplotlibrc file (see my comment here)
My plots in matplotlib don't show more than one letter/digit.
I do need to use matplotlib from the anaconda folder which is why I'm including sys.path at the beginning. (When I don't include the first 2 lines everything works fine since I have a different matplotlib on my computer. Both are up to date.) Any ideas what I can do to make it work? I'm using Ubuntu and Sublime Text.
Here's my simple code:
import sys
sys.path.insert(0, "/home/mariusz/anaconda/lib/python2.7/site-packages")
import matplotlib.pyplot as plt
grade_fast = [1,7,3,5,6,7,10,11,12]
bumpy_fast = [0,2,3,5,6,7,10,11,12]
grade_slow = [0,2,3,5,6,7,10,11,12]
bumpy_slow = [0,2,3,5,6,7,10,11,12]
plt.scatter(bumpy_fast, grade_fast, label="f")
plt.scatter(grade_slow, bumpy_slow, label="slow")
plt.legend()
plt.xlabel("bumpiness")
plt.ylabel("g")
plt.show()
I re-installed matplotlib, numpy, scikit-learn and scipy and it worked :)
I don't know why though.
I have a simple script to test a plot in matplotlib but no window showing the figure appears. On reading other questions on stackoverflow, I've done the following to resolve this:
installed PySide using these instructions.
edited matplotlibrc file with these two lines:
backend : Qt4Agg
#backend.qt4 : PySide # PyQt4 | PySide
so that the command python -c 'import matplotlib; import matplotlib.pyplot; print(matplotlib.backends.backend)' now yields Qt4Agg whereas before it gave agg
included the pylab.show() command. So the set of commands that I now tried in the python interpreter after installing Pyside, and editing the matplotlibrc file look like this:
import pylab
pylab.ion()
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x7fcef627cdd0>]
pylab.show()
However, the plot still doesn't show. Could anyone please help me with this? I am using Ubuntu 14.04 in VirtualBox with python2.7.
When I use your code the plot actually flashes on the screen, but closes immediately. Placing an input() function at the end might help you with debugging it:
import pylab
import matplotlib.pyplot as plt
import numpy as np
pylab.ion()
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
pylab.show()
tin = input("Test Input: ")
And removing the pylab.ion() actually keep the plot on the screen. This gives you another hint. There are already some good answers why this is happening. E.g.:
Matplotlib ion() function fails to be interactive
EDIT:
Bah, finally found a discussion on the Runtime Error, although it focuses on using PythonWin, which I did not have installed at the time. After installing PythonWin and setting up GTK (as per an earlier question), I was still encountering the error. The solution from the discussion board here was to append plt.close() after the for loop. This seems to work.
However:
From the command line, window is still unmovable while plotting. When exiting, PyEval_RestoreThread no longer runs into NULL tstate. It would be nice to allow the window to move while plotting.
Original Post:
Note: All problems described are encountered when running from command line. Similar quirks are encountered when running from IDLE Shell (-n), as noted in the "Additional, possibly irrelevant information" section.
My code plots a line correctly, and immediately after plotting I get:
"Fatal Python error: PyEval_RestoreThread: NULL tstate
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."
The code follows:
import matplotlib as mpl
mpl.use("TKAgg")
import pylab as plt
import numpy as np
Line = mpl.lines.Line2D
x = np.linspace(-1,1,50)
y=np.sin(x*np.pi)
plt.ion()
fig = plt.gcf()
ax = fig.gca()
ax.add_line(Line([x],[y]))
plt.draw()
The code is fine when commenting out plt.ion(), but then nothing is shown.
While plt.show() would work in this example, the goal is to use interactive to create a crude animation through the following:
import matplotlib as mpl
mpl.use("TKAgg")
import pylab as plt
import numpy as np
plt.ion()
Line = mpl.lines.Line2D
x = np.linspace(-1,1,50)
for i in xrange(10):
y = (1.0+i/9.0) * np.sin(x*np.pi)
plt.clf()
fig = plt.gcf()
ax = fig.gca()
ax.add_line(Line([x],[y]))
plt.draw()
Each iteration correctly plots its respective line, and any code after the loop runs before the fatal error, as can be demonstrated by adding the following immediately following the for loop:
raw_input("no error yet: ")
print "here it comes"
I realize that destroying the figure and then creating a new figure and new axes may not be efficient or even good practice. However, the problem still seems to be on plt.ion(), as commenting it out produces no errors.
If the solution is well-documented and I've passed it by in my searches, please feel free to angrily point this out and perhaps provide a link to such. This would be much preferred, if the alternative is having run into a new problem.
If the answer is to manage plotting more directly than using pylab, I am more than willing to explore this option.
Additional, possibly irrelevant information:
When not using raw_input() after for loop, window is unmovable while running second code.
If using raw_input(), window can be moved after plotting, while program is waiting for raw_input()
Issue is the same when running from IDLE Shell (-no subprocess):
Window cannot be moved while plotting, but does not encounter fatal error.
Window can be moved after plotting, even without using raw_input()
From either command line or IDLE Shell, each plot is correctly displayed while window is unmovable
Thanks in advance for any suggestions/advice.