Matplotlib动画中的配色栏

发布于 2025-01-30 19:07:04 字数 1003 浏览 2 评论 0 原文

我想在一些数据中添加一个配色栏以制作动画。

但是,我继续在图中创建新的颜色条,不知道如何删除它们。

可再现的例子是:

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)
                           )   

如何在动画中只保留一个配色杆?

亲切的问候

I would like to add a colorbar to some data to make an animation.

However, I keep on creating new color bars in the figure and don't know how to remove them.

A reproducible example is:

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)
                           )   

How can I keep only one colorbar in the animation?

Kind regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2025-02-06 19:07:04

您必须为配色栏创建一个轴:

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 animate(i, ax, data):
    ax.cla()
    data[0] += 0.1
    myPlot(ax, data)


animation = FuncAnimation(fig,
                          animate,
                          frames = range(len(X)),
                          fargs = (ax, data))

plt.show()

动画

You have to create an axis for the colorbar:

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])

and pass it as a parameter in the colorbar definition:

fig.colorbar(surf, shrink = 0.5, aspect = 5, cax = cbar_ax)

Complete Code

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 animate(i, ax, data):
    ax.cla()
    data[0] += 0.1
    myPlot(ax, data)


animation = FuncAnimation(fig,
                          animate,
                          frames = range(len(X)),
                          fargs = (ax, data))

plt.show()

Animation

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文