如何使用 python 和 matplotlib 为矩形内的颜色填充设置动画?
How to animate color filling inside rectangle using python and matplotlib?
我想在 y 轴上为矩形内的颜色设置动画,颜色填充应该像峰值计条的行为一样上下移动。此代码中存在我无法解决的错误。请帮我解决这个问题。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
import numpy
class AnimRect(object):
'''Animate a rectangle'''
def __init__(self):
self.fig = plt.figure(figsize = (5,5))
# create the axes
self.ax = plt.axes(xlim=(0,100), ylim=(0,100), aspect='equal')
# create rectangle
self.rect = plt.Rectangle((0,0), 5, 50,
fill=True, color='gold', ec='blue')
self.ax.add_patch(self.rect)
self.y=[]
for i in range(100):
x = random.randint(0, 30)
self.y.append(x)
self.y = numpy.array(self.y)
# print(self.y,len(self.y))
self.call_animation()
plt.show()
# animation function
def animate(self, i):
self.rect = self.ax.fill_between(0, self.y[i],0, color = 'red')
return self.rect,
def call_animation(self):
# call the animator function
self.anim = animation.FuncAnimation(self.fig, self.animate, frames=len(self.y), interval=0.1, blit=True, repeat=False)
def main():
rect = AnimRect()
main()
它在控制台中给出这样的错误
idx, = np.nonzero(mask[:-1] != mask[1:]) IndexError: too many indices for array
在 animate 函数中,您应该用这行代码替换您的代码行:
self.rect = self.ax.fill_between(x = (0, 5), y1 = 0, y2 = self.y[i], color = 'red')
我linkdocumentation到fill_between
方法:
x: array (length N)
The x coordinates of the nodes defining the curves. y1array (length N) or scalar
y1: array (length N) or scalar
The y coordinates of the nodes defining the first curve. y2array (length N) or scalar, optional, default: 0
y2: array (length N) or scalar, optional, default: 0
The y coordinates of the nodes defining the second curve.
您的代码的问题是您将 x
指定为 int
而它应该是一个数组。
我想在 y 轴上为矩形内的颜色设置动画,颜色填充应该像峰值计条的行为一样上下移动。此代码中存在我无法解决的错误。请帮我解决这个问题。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
import numpy
class AnimRect(object):
'''Animate a rectangle'''
def __init__(self):
self.fig = plt.figure(figsize = (5,5))
# create the axes
self.ax = plt.axes(xlim=(0,100), ylim=(0,100), aspect='equal')
# create rectangle
self.rect = plt.Rectangle((0,0), 5, 50,
fill=True, color='gold', ec='blue')
self.ax.add_patch(self.rect)
self.y=[]
for i in range(100):
x = random.randint(0, 30)
self.y.append(x)
self.y = numpy.array(self.y)
# print(self.y,len(self.y))
self.call_animation()
plt.show()
# animation function
def animate(self, i):
self.rect = self.ax.fill_between(0, self.y[i],0, color = 'red')
return self.rect,
def call_animation(self):
# call the animator function
self.anim = animation.FuncAnimation(self.fig, self.animate, frames=len(self.y), interval=0.1, blit=True, repeat=False)
def main():
rect = AnimRect()
main()
它在控制台中给出这样的错误
idx, = np.nonzero(mask[:-1] != mask[1:]) IndexError: too many indices for array
在 animate 函数中,您应该用这行代码替换您的代码行:
self.rect = self.ax.fill_between(x = (0, 5), y1 = 0, y2 = self.y[i], color = 'red')
我linkdocumentation到fill_between
方法:
x: array (length N) The x coordinates of the nodes defining the curves. y1array (length N) or scalar y1: array (length N) or scalar The y coordinates of the nodes defining the first curve. y2array (length N) or scalar, optional, default: 0 y2: array (length N) or scalar, optional, default: 0 The y coordinates of the nodes defining the second curve.
您的代码的问题是您将 x
指定为 int
而它应该是一个数组。