Jupyter Lab: Matplotlib live plot is scaled awkwardly - python

I want to implement some live plotting in Jupyter lab using Jupyter-Matplotlib. Unfortunately the plot is scaled awkwardly during live plotting. You can see the result in the GIF below. Only the upper left corner of the plot is visible.
The code I used is as follows:
%matplotlib notebook
from matplotlib import pyplot as plt
import time
fig = plt.figure()
ax = fig.gca()
canvas = fig.canvas
a = []
for i in range(10):
a.append(i)
ax.clear()
ax.plot(a)
canvas.draw()
time.sleep(0.5)
Does anyone have experience with this kind of issue and knows how to fix it?

Related

Matplotlib Animation not rendering

I am trying to render an animation of rotating a 3d graph, but the animation is not working, even if I try to use the code given here: https://matplotlib.org/3.5.0/gallery/mplot3d/rotate_axes3d_sgskip.html
The output I get is one static image and <Figure size 432x288 with 0 Axes> repeated multipe times over and over again.
I am using anaconda, jupyter notebooks and also tried using google colab... what is going wrong and how can I fix this? Thank you very much!
I ran into a similar issue before, replacing "%matplotlib inline" with "%matplotlib notebook" in the given code solved it for me.
Since you are using Jupyter Notebook, you need to install ipympl and execute the following command on a cell at the top of your notebook: %matplotlib widget. This will enable an interactive frame which is going to wrap the matplotlib figure in the output cell.
Then, you need to use matplotlib's FuncAnimation:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
def animate(angle):
ax.view_init(30, angle)
ani = FuncAnimation(fig, animate, frames=range(360))
Add %matplotlib notebook in the cell of Jupyter notebook for an interactive backend.
Execute the below cell, it should work
%matplotlib notebook
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
# rotate the axes and update
for angle in range(0, 360):
ax.view_init(30, angle)
plt.draw()
plt.pause(.001)

Viewing Matplotlib funcAnimation live plots in browser

I am generating a live plot using matplotlib's funcAnimation function such as in the example below. To be clear I am plotting data that is dynamically updating in real-time and I want to view these updates in the browser rather than in its own local plotting window.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import mpld3
def animate(i):
plt.clf()
#generate random data
x = np.array([i for i in range(100)])
y = np.random.normal(loc=0,scale=1, size = 100)
plt.scatter(x,y)
def main():
fig = plt.figure()
ani = animation.FuncAnimation(fig, animate, interval=1000)
ani.save('animation.gif', fps=10)
plt.show()
#mpld3.show()
if __name__ == '__main__':
main()
It opens the plot in its own little window.
I was wondering what is the best/quickest way to open this in browser instead? I tried using mpld3 as recommended in some places (see the commented bits of code in my code snippet), but all I get is a static page that does not update. I would like the plot to be redrawn in the browser during each redraw done by the animate fuction as it does when the plot is generated in it's own window instead as in the first image.
Thanks in advance for any help anyone has to offer!

Suppress display of final frame in matplotlib animation in jupyter

I am working on a project that involves generating a matplotlib animation using pyplot.imshow for the frames. I am doing this in a jupyter notebook. I have managed to get it working, but there is one annoying bug (or feature?) left. After the animation is created, Jupyter shows the last frame of the animation in the output cell. I would like the output to include the animation, captured as html, but not this final frame. Here is a simple example:
import numpy as np
from matplotlib import animation
from IPython.display import HTML
grid = np.zeros((10,10),dtype=int)
fig1 = plt.figure(figsize=(8,8))
ax1 = fig1.add_subplot(1,1,1)
def animate(i):
grid[i,i]=1
ax1.imshow(grid)
return
ani = animation.FuncAnimation(fig1, animate,frames=10);
html = HTML(ani.to_jshtml())
display(html)
I can use the capture magic, but that suppresses everything. This would be OK, but my final goal is to make this public, via binder, and make it as simple as possible for students to use.
I have seen matplotlib animations on the web that don't seem to have this problems, but those used plot, rather than imshow, which might be an issue.
Any suggestions would be greatly appreciated.
Thanks,
David
That's the answer I got from the same thing I was looking for in 'jupyter lab'. Just add plt.close().
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
grid = np.zeros((10,10),dtype=int)
fig1 = plt.figure(figsize=(8,8))
ax1 = fig1.add_subplot(1,1,1)
def animate(i):
grid[i,i]=1
ax1.imshow(grid)
return
ani = animation.FuncAnimation(fig1, animate,frames=10);
html = HTML(ani.to_jshtml())
display(html)
plt.close() # update

Update ellipse position interactive matplotlib figure jupyter notebook

I managed to generate an interactive matplotlib figure that changes the position of an ellipse using some ipywidget slider.
However the plot is displayed in a new window, I could not find a working solution with the plot in the notebook.
I could generate other interactive figures within a jupyter notebook, like line plots, but with the ellipse, no way..
%matplotlib
from matplotlib import patches
import matplotlib.pyplot as plt
from ipywidgets import interact
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = patches.Ellipse((0,0), width=50, height=25, angle=15)
ax.add_patch(ellipse)
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
#interact
def update(xcenter=(-50,50), ycenter=(-50,50)):
ellipse.set_center((xcenter, ycenter))
#fig.canvas.draw()
plt.plot()
Using % matplotlib notebook fixed the issue in most cases.

Converting a single dynamic plot into dynamic plot with subplots in jupyter notebook

I have the code below, which works great with a single plot, but I'm trying to create a new plot with 1x2 subplots. The second plot will be identical to the first, just in another subplot.
# This code works fine as a single plot
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
pl.plot(pl.randn(100))
display.display(pl.gcf())
display.clear_output(wait=True)
time.sleep(1.0)
I'm not familar with pylab, but the above plot runs so smoothly compared to the pyplot code I found on the nex, that I'm trying to figure out how to implement this code with subplots.
#can't implement it to a plot with subplots
%matplotlib inline
import time
import pylab as pl
from IPython import display
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, sharex = True, figsize = (10,5))
for i in range(10):
pl.clf()
ax1.plot(pl.randn(100),)
ax2.plot(pl.randn(50))
display.display(pl.show())
display.clear_output(wait=True)
time.sleep(1.0)
However, no graph is being outputted with my attempt.
I'm played around with this code, but I can't seem to make it work cleanly.
thank you.
To visualize the plot with subplots, you should know the differences between Figure and Axes in matplotlib. Basically, axes belong to the figure, and you want to plot your data in the axes, but display the figure. Both Figure and Axes instances can be obtained with a single call to pl.subplots(nrow, ncol). See if the code below does what you want:
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
f, ax = pl.subplots(1, 2)
ax[0].plot(pl.randn(100))
ax[1].plot(pl.randn(100))
display.display(f)
display.clear_output(wait=True)
time.sleep(1.0)

Categories