I recently updated my matplotlib python package to version 2.2.0, and now some previously working code to save an animation does not work. Instead of saving an animation, the code freezes at a certain iteration number. It becomes unresponsive to interrupts and throws a PyEval_RestoreThread fatal error when I manage to close the command window.
I am using Enthought Canopy. The code still works as normal with other versions of python and matplotlib.
I can replicate the problem with this:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
SIZE = 128
fig, ax = plt.subplots()
ims = ax.imshow(np.random.rand(SIZE, SIZE))
i = 0
def update_animation(*args):
global i
i = i + 1
image = np.random.rand(SIZE, SIZE)
ims.set_data(image)
print "Iteration "+str(i)
F_NAME = "anim_test.mp4"
NUM_ITERS = 1000
FFwriter = animation.FFMpegWriter(fps=6, bitrate=500)
anim=animation.FuncAnimation(fig,update_animation,frames=NUM_ITERS,blit=False, repeat=False, interval=200)
anim.save(F_NAME, writer=FFwriter)
print "saved animation to " + F_NAME
Changing the bitrate parameter changed the number of the frame the program halts at. For bitrate=500, it halts around frame 46.
How can I stop pyplot freezing before all frames can be saved?
EDIT
My system details:
Python 2.7.6 64bit Enthought Canopy
Windows 8
8GB RAM
ffmpeg version N-79075-ga7b8a6e
Related
I have this simple program here
import numpy as np
import matplotlib.pyplot as plt
num_of_intervals = 2000
x = np.linspace(-10,10,num=num_of_intervals)
y_inputs = 1/(1+np.exp(-x)) # SIGMOID FUNCTION
plt.figure(figsize = (15,9))
plt.plot(x,y_inputs,label = 'Sigmoid Function')
plt.vlines(x=0, ymin = min(y_inputs), ymax=max(y_inputs), linestyles='dashed')
plt.title('Sigmoid Function')
plt.show()
When the above program is ran in the vscode terminal. The plot cannot be seen (usually a pop-up window appears showing the plot).
But when the program is ran in the Ubuntu terminal, the plot can be seen as a pop-up window.
Any idea how I can solve this issue with vscode.
OS : Ubuntu 20.04
Visual Studio Code 1.54.3
Python : 3.8.5
Double check that this option is turned on in Settings: terminal.integrated.inheritEnv
Disclaimer: I am new to Python and I have the latest Anaconda-Python (3.8.3) installed in my computer. My code is
if __name__ == '__main__':
import imageio
import matplotlib.pyplot as plt
plt.clf()
pic = imageio.imread('Photo_Vikash_pandey.jpg')
plt.figure(figsize = (15,15))
plt.imshow(pic)
Every time I run the code I see images piled up in the adjoining window of Spyder IDE. I want that every time I run the code, it has no history of its previous execution. I do not wish to see image display from previous runs of the code.
I have a program to scatter plot eigenvalues and from the user click to retrieve the selected number/coordinate and use that input for further analysis. The program is working fine in Spyder console ( I am using python 2.7.16 and matplotlib 2.2.3). But when I execute the same code from the mac terminal, it is showing the plot but not recognizing the user click. (I have checked the same code from windows command terminal and it is also not working there even though it is working in spyder installed in my windows system). The program is also not showing any error. A sample program is provided here for reference.
import matplotlib.pyplot as plt
import numpy as np
n = 10
real_part = np.random.normal(size=n)
imag_part = np.random.normal(size=n)
z = np.array(real_part, dtype=complex)
z.imag = imag_part
fig,ax = plt.subplots(figsize=(8,5))
ax.scatter(z.real,z.imag,marker='x',color='r')
plt.grid(which='both',axis='both')
plt.title('Complex plot')
plt.show()
userInput=plt.ginput(n=1, timeout=30, show_clicks=True)
print("User Input is %s"%userInput)
Any help on this would be highly appreciated.
Thanks in advance.
I'm trying to display a video from some arrays in an notebook in jupyter-lab. The arrays are produced at runtime. What method to display the images can deliver a (relatively) high framerate? Using matplotlib and imshow is a bit slow. The pictures are around 1.8 megapixel large.
Above some very small example to visualize what I want to achieve.
while(True): #should run at least 30 times per second
array=get_image() #returns RGBA numpy array
show_frame(array) #function I search for
The fastest way (to be used for debug purpose for instance) is to use matplotlib inline and the matplotlib animation package. Something like this worked for me
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import animation
from IPython.display import HTML
# np array with shape (frames, height, width, channels)
video = np.array([...])
fig = plt.figure()
im = plt.imshow(video[0,:,:,:])
plt.close() # this is required to not display the generated image
def init():
im.set_data(video[0,:,:,:])
def animate(i):
im.set_data(video[i,:,:,:])
return im
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=video.shape[0],
interval=50)
HTML(anim.to_html5_video())
The video will be reproduced in a loop with a specified framerate (in the code above I set the interval to 50 ms, i.e., 20 fps).
Please note that this is a quick workaround and that IPython.display has a Video package (you can find the documentation here) that allows you to reproduce a video from file or from an URL (e.g., from YouTube).
So you might also consider storing your data locally and leverage the built-in Jupyter video player.
I want to display an animation in Jupyter using Matplotlib. Here is some basic example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)
def update(data):
line.set_ydata(data)
return line,
def data_gen():
while True:
yield np.random.rand(10)
ani = animation.FuncAnimation(fig, update, data_gen, interval=100);
from IPython.display import HTML
HTML(ani.to_jshtml())
When I run the code for the first time (or after restarting the kernel) I get what I want:
However, when I run the very same code for the second time I get a leftover in the left bottom:
I noticed that when I add %matplotlib inline at the top, then I got the bad output even after restarting the kernel. Thus my guess is that I have to set the magic command %matplotlib to default at the top each time I create an animation, but I can't even find if %matplotlib have a default value.
I use Anaconda. Here are my versions:
Conda version: 4.4.10
Python version: Python 3.6.4 :: Anaconda, Inc.
IPython version: 6.2.1
Jupyter version: 5.4.0
I used plt.close() to stop the first (unwanted) plot, and have not seen issues running the animation in a separate cell. I believe the issue is similar to those linked in the comments, jupyter is automatically displaying an unwanted plot for the first two lines - fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10)). I tired suggestions such as using semicolons at end of lines and a few different magic attempts, but no joy. A more concrete solution will no doubt appear, but for now....