如何使用python和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
安德里亚·布伦吉诺(Andrea Blengino):

在animate函数中,您应该用以下代码替换代码行:

self.rect = self.ax.fill_between(x = (0, 5), y1 = 0, y2 = self.y[i], color = 'red')

我将文档链接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.

在你的代码的问题是,您所指定xint,而应该是一个数组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用matplotlib.animation对python中的数据图进行动画处理

如何对倾斜矩形的高度进行动画处理?

使用CSS Transition对边距和填充进行动画处理会导致跳动的动画

使用jquery颜色插件对Div的颜色进行动画处理

在画布上对矩形进行动画处理

以特定模式对矩形进行动画处理

在matplotlib中对散点和线进行动画处理

使用matplotlib使用colorbar对散点图进行动画处理

如何使用角度动画对单击进行动画处理

如何使用R的动画包对散点图进行动画处理?

仅对按钮的颜色进行动画处理

Python-使用matplotlib对大型数据集进行动画处理

使用Matplotlib在Python中对兰登的蚂蚁进行动画处理

在不使用图像的情况下对背景颜色进行动画处理-如何为退出设置动画

使用mouseover和mouseout对Vue组件进行动画处理

使用NumberAnimation和Behavior进行动画处理

使用CGPaths和CAShapeLayers对字母绘图进行动画处理

如何使用animate.css和Jquery对内容进行动画处理

Python:使用mplot3d和animation对向量进行动画处理

如何使用jQuery对导航栏进行动画处理

如何使用重力沿弯曲路径进行动画处理

Android:如何使用矩阵对视图进行动画处理?

如何使用jQuery对图像进行动画处理

在Python中对图像进行动画处理

使用PyPlot对ScatterPlot进行动画处理

使用Jquery对徽标进行动画处理

使用CSS对SVG进行动画处理:首先是笔触,然后是填充

在WPF中使用情节提要对网格背景颜色进行动画处理

在Android上对视图背景颜色进行动画处理