在Matplotlib动画中更改RGB颜色 [英] Change RGB color in matplotlib animation

查看:212
本文介绍了在Matplotlib动画中更改RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法通过RGB定义更改Matplotlib散点图的颜色。我错了吗?

I seems that it is not possible to change colors of a Matplotlib scatter plot through a RGB definition. Am I wrong?

这是一个代码(已经在堆栈溢出中给出),可以处理以float索引的颜色:

Here is a code (already given in stack overflow) which work with colors indexed in float:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

def main():
    numframes = 100
    numpoints = 10

    color_data = np.random.random((numframes, numpoints))
    x, y, c = np.random.random((3, numpoints))

    fig = plt.figure()
    scat = plt.scatter(x, y, c=c, s=100)

    ani = animation.FuncAnimation(fig, update_plot, frames=range(numframes),
                                  fargs=(color_data, scat))
    plt.show()

def update_plot(i, data, scat):
    scat.set_array(data[i])
    return scat,

main()

但是如果 color_data 是通过RGB颜色定义的,则会出现错误:

But if color_data is defined through RGB colors, I get an error:


ValueError:集合只能映射r约1个数组

ValueError: Collections can only map rank 1 arrays

相关代码如下(在此代码中,我每次仅更改一个样本的颜色):

The related code is the following (in this code, I just change the color of one sample each time):

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

def main():
    numframes = 100
    numpoints = 10

    rgb_color_data = np.random.random((numpoints, 3))
    x, y = np.random.random((2, numpoints))

    fig = plt.figure()
    scat = plt.scatter(x, y, c=rgb_color_data, s=100) #this work well at this level

    ani = animation.FuncAnimation(fig, update_plot2, frames=range(numframes),
                                  fargs=(rgb_color_data, scat))

    plt.show()

def update_plot2(i,data,scat):
    data[ i%10 ] = np.random.random((3))
    scat.set_array(data) # this fails 
    return scat,

main()

是否有使用方法 set_array 具有RGB颜色阵列?

Is there a means to use set_array with RGB color array?

推荐答案

不确定您要尝试什么达到。但是,如果要更改颜色,为什么不使用 Collection set_color()函数呢?

Not sure what you are trying to achieve. But if you are trying to change the color, why not use the set_color() function of Collection?

def update_plot2(i,data,scat):
    data[ i%10 ] = np.random.random((3))
    scat.set_color(data) # <<<<<<<<<<<<<<<<<<<
    return scat,

这篇关于在Matplotlib动画中更改RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆