我想為一些資料添加一個顏色條來制作影片。
但是,我繼續在圖中創建新的彩條,不知道如何洗掉它們。
一個可重現的例子是:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import cm
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111, projection='3d', proj_type='ortho')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 Y**2)
Z = np.sin(R)
data = [X, Y, Z]
def myPlot(ax, data):
surf = ax.plot_surface(*data, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
def anime(i, ax, data):
ax.cla()
data[0] = 0.1
myPlot(ax, data)
animation = FuncAnimation(
fig,
anime,
frames=range(len(X)),
fargs=(ax, data)
)
如何在影片中只保留一個顏色條?
親切的問候
uj5u.com熱心網友回復:
您必須為顏色條創建一個軸:
gs = GridSpec(1, 2, width_ratios = [0.9, 0.05])
fig = plt.figure(figsize = (7, 7))
ax = fig.add_subplot(gs[0], projection = '3d', proj_type = 'ortho')
cbar_ax = fig.add_subplot(gs[1])
并將其作為顏色條定義中的引數傳遞:
fig.colorbar(surf, shrink = 0.5, aspect = 5, cax = cbar_ax)
完整代碼
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import cm
from matplotlib.gridspec import GridSpec
gs = GridSpec(1, 2, width_ratios = [0.9, 0.05])
fig = plt.figure(figsize = (7, 7))
ax = fig.add_subplot(gs[0], projection = '3d', proj_type = 'ortho')
cbar_ax = fig.add_subplot(gs[1])
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 Y**2)
Z = np.sin(R)
data = [X, Y, Z]
def myPlot(ax, data):
surf = ax.plot_surface(*data, cmap = cm.coolwarm,
linewidth = 0, antialiased = False)
fig.colorbar(surf, shrink = 0.5, aspect = 5, cax = cbar_ax)
def anime(i, ax, data):
ax.cla()
data[0] = 0.1
myPlot(ax, data)
animation = FuncAnimation(
fig,
anime,
frames = range(len(X)),
fargs = (ax, data)
)
plt.show()
影片片

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480120.html
