Recenter plot after set_xdata and set_ydata - python

I can use the set_xdata and set_ydata functions to update an existing matplotlib plot. But after updating I want to recenter the plot so that all the points fall into the "view" of the plot.
In the below example, the y data keeps getting bigger but the zoom level of the plot remains same so the data points quickly get out of the scope.
import time
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.ion()
figure, ax = plt.subplots(figsize=(10, 8))
(line1,) = ax.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
for i in range(1000):
new_y = np.sin(x - 0.5 * i) * i
line1.set_xdata(x)
line1.set_ydata(new_y)
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(0.1)

Adding ax.relim() and ax.autoscale() fixes the issue
import time
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.ion()
ax: plt.Axes
figure, ax = plt.subplots(figsize=(10, 8))
(line1,) = ax.plot(x, y)
ax.autoscale(True)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
for i in range(1000):
new_y = np.sin(x - 0.5 * i) * i
line1.set_xdata(x)
line1.set_ydata(new_y)
# Rescale axes limits
ax.relim()
ax.autoscale()
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(0.1)

np.sin(x - 0.5 * i) has multiplied by i, which can be 1000. One alternative is to make the y-axis have a limit greater than 1000. So, you can include plt.ylim([-1100,1100]):
import time
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.ion()
figure, ax = plt.subplots(figsize=(10, 8))
(line1,) = ax.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.ylim([-1100,1100])
for i in range(1000):
new_y = np.sin(x - 0.5 * i) * i
line1.set_xdata(x)
line1.set_ydata(new_y)
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(0.1)

Related

Moving sphere animation

I want to create an animation of a moving sphere in matplotlib. For some reason it isnt working:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50
def init():
ax.plot_surface(x0,y0,z0)
return fig,
def animate(i):
ax.plot_surface(x0 + 1, y0 + 1, z0 + 1)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)
plt.show()
Here, I have attempted to move the sphere by (1,1,1) in each new iteration, but it fails to do so.
There are a couple of mistakes with your approach:
In your animate function you are adding a sphere at each iteration. Unfortunately, Poly3DCollection objects (created by ax.plot_surface) cannot be modified after they have been created, hence to animate a surface we need to remove the surface of the previous iteration and add a new one.
In your animation the sphere didn't move because at each iteration you were adding a new sphere at the same location as the previous one.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50
surface_color = "tab:blue"
def init():
ax.plot_surface(x0, y0, z0, color=surface_color)
return fig,
def animate(i):
# remove previous collections
ax.collections.clear()
# add the new sphere
ax.plot_surface(x0 + i, y0 + i, z0 + i, color=surface_color)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)
plt.show()

How to normalize colorbar in Python?

I want to adjust colobar scale from my current figure1 to the desired figure2 !!
My colorbar scale range is -1 to 1, but I want it in exponential form and for that I tried levels = np.linspace(-100e-2,100e-2) as well, but it also doesn't give the desired scale2
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
ds = xr.open_dataset('PL_Era_Tkt.nc')
wp = ds.w.mean(dim=['longitude','latitude']).plot.contourf(x='time',cmap='RdBu',add_colorbar=False,extend='both')
wpcb = plt.colorbar(wp)
wpcb.set_label(label='Pa.s${^{-1}}$',size=13)
plt.gca().invert_yaxis()
plt.title('Vertical Velocity',size=15)
My current scale
My desired scale
Since the data is not presented, I added normalized color bars with the data from the graph sample here. I think the color bar scales will also be in log format with this setup. Please note that the data used is not large, so I have not been able to confirm this.
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.ticker as ticker
import numpy as np
plt.style.use('seaborn-white')
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig, ax = plt.subplots()
ax.contourf(X, Y, Z, 20, cmap='RdGy')
cmap = mpl.cm.RdGy
norm = mpl.colors.Normalize(vmin=-1, vmax=1.0)
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
ax=ax, orientation='vertical', label='Some Units', extend='both', ticks=ticker.LogLocator())
plt.show()

Function animation with plot_surface not drawing , just giving first picture

I have written the following code with function animation with plot_surface which is not drawing, just giving the first picture
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = np.outer(np.linspace(-2, 2, 50), np.ones(50))
#print(x)
y = x.copy().T # transpose
fig = plt.figure()
ax = plt.axes(projection='3d')
def animation_frame(i):
z = np.cos(x ** 2 + y ** 2) + np.cos(x ** (2*i) + y ** (2*i))
# print (z)
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
# return ax,
animation = FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 10, 1), interval=1000, blit=False)
#plt.show()
animation
You should call the plt.show() method at the end. Moreover, you should erase the previous plot with ax.cla() at the beginning of the animation_frame.
Whole code
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = np.outer(np.linspace(-2, 2, 50), np.ones(50))
y = x.copy().T
fig = plt.figure()
ax = plt.axes(projection = '3d')
def animation_frame(i):
ax.cla()
z = np.cos(x**2 + y**2) + np.cos(x**(2*i) + y**(2*i))
ax.plot_surface(x, y, z, cmap = 'viridis', edgecolor = 'none')
animation = FuncAnimation(fig, func = animation_frame, frames = np.arange(0, 10, 1), interval = 250, blit = False)
plt.show()

How to automatically set the scale for x-axis to be equal for all subplots in matplotlib?

In this example:
import numpy as np
import matplotlib.pyplot as plt
t1 = np.linspace(0, 1, 1000)
t2 = np.linspace(0, 0.5, 1000)
plt.figure(figsize=(10,5))
plt.subplot(121)
plt.plot(t1, np.sin(t1 * np.pi))
plt.subplot(122)
plt.plot(t2, np.sin(t2 * np.pi))
plt.show()
How can I squeeze the size of the second plot so that the x-axis for both subplots would have the same scale?, so it would look something like this:
I am looking for a simple and automatic way to do this because I have more than 30 subplots and would want them all have the same x-axis scale.
You could approximate the same unit length on both x-axes by specifying the gridspec_kw parameter that defines the ratio of the subplots.
import numpy as np
from matplotlib import pyplot as plt
t1 = np.linspace(0, 1, 1000)
t2 = np.linspace(0, 0.5, 1000)
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {"width_ratios": [np.max(t1)-np.min(t1), np.max(t2)-np.min(t2)]})
ax1.plot(t1, np.sin(t1 * np.pi))
ax2.plot(t2, np.sin(t2 * np.pi))
plt.show()
Sample output:
You can use plt.xlim(xmin, xmax) to set the domain of the graph. Using plt.xlim() without giving it parameters returns the current xmin/xmax of the plot. The same applies for plt.ylim().
A presumably not very proper way of doing so but in my opinion useful for a work around would be the use of subplot2grid:
plt.subplot2grid((ROWS, COLS), (START_ROW, START_COL), rowspan=ROWSPAN, colspan=COLSPAN)
using this, you could create two subplots which add up to the desired length and passing the colspan accordingly to the length of your x axis like for example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5)
y = np.linspace(0, 10)
plt.figure(figsize=(10,5)) # as specified from your code
# x.max() + y.max() is the total length of your x axis
# this can then be split in the parts of the length x.max() and y.max()
# the parts then should have the correct aspect ratio
ax1 = plt.subplot2grid((1, int(x.max()+y.max()), (0, 0), colspan=int(x.max()))
ax2 = plt.subplot2grid((1, int(x.max()+y.max()), (0, int(x.max())), colspan=int(y.max()))
ax1.plot(x, np.sin(x))
ax2.plot(y, np.sin(y))
plt.show()
The scales seem same for me, you would still have to adjust the xticklabels in case those are supposed to be same as well
You can achieve it by changing the aspect ratio:
import numpy as np
import matplotlib.pyplot as plt
t1 = np.linspace(0, 1, 1000)
t2 = np.linspace(0, 0.5, 1000)
plt.figure(figsize=(10,5))
fig,ax = plt.subplots(nrows = 1,ncols = 2)
ax[0].plot(t1, np.sin(t1 * np.pi))
x1,x2 =ax[1].get_xlim()
x_diff = x2-x1
y1,y2 = ax[1].get_ylim()
y_diff = y2-y1
#plt.subplot(122)
ax[1].plot(t2, np.sin(t2 * np.pi))
ax[1].set_aspect(y_diff/x_diff)
Output:

Python animate contour plot for function generated in for loops

I have a three-variable function myfunc that is generated inside three for loops. I want to draw a contour plot of y vs x and animate this for different times t. However, I've looked at the various matplotlib examples on the webpage, and am still unsure of how to do this.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import animation
def myfunc(x,y,t):
w = 0.5*x + y + 4*np.sin(1.8*t)
return w
xlist = np.linspace(0,10,10)
ylist = np.linspace(-1,1,10)
tlist = np.linspace(0,50,50)
plt.figure()
for t in tlist:
for x in xlist:
for y in ylist:
w = myfunc(x,y,t)
w_vec = np.array(w)
w_contour = w_vec.reshape((xlist.size, ylist.size))
w_plot = plt.contourf(ylist,xlist,w_contour)
plt.xlabel('x', fontsize=16)
plt.ylabel('y', fontsize=16)
plt.show()
Edit: I quite like the look of dynamic_image2.py in this tutorial. This seems to get things moving, but the axes are wrong:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def f(x,y,t):
return 0.5*x + np.sin(y) + 4*np.sin(1.8*t)
x = np.linspace(0, 10, 10)
y = np.linspace(-1, 1, 10).reshape(-1, 1)
tlist = np.linspace(0,50,50)
ims = []
for t in tlist:
x += np.pi / 15.0
y += np.pi / 20.0
im = plt.imshow(f(x,y,t))
ims.append([im])
ani = animation.ArtistAnimation(fig, ims, interval=20, blit=True,
repeat_delay=1000)
plt.show()

Categories