我试图在此动画中为每个点获取不同的颜色。我想传递数组c_state作为每个点的hsv元组的hue值。到目前为止,我尝试过的一切都失败我在animate函数尝试使用这样的:matplotlib动画中的数组的不同颜色点
particles.set_color(pbox.color[:,0],1.0,1.0)
但我得到警告说,只有长度为1的阵列可以转换为标量。 我也尝试使用np.random制作长度为3的数组,并试图将它们转换为rgb-tuples,但那也不起作用。我很难找到正确的数据结构传递给ax.plot的颜色变量。 颜色只需设置一次,在动画过程中不需要更改。
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np
import colorsys
from random import random
n = 250
class ParticleBox:
def __init__(self,i_state,c_state):
self.i_state = np.asarray(i_state, dtype=float)
self.c_state = np.asarray(c_state, dtype=float)
self.state = self.i_state.copy()
self.color = self.c_state.copy()
i_state = -5 + 10 * np.random.random((n, 2))
c_state = np.random.random((n, 1))
pbox = ParticleBox(i_state, c_state)
fig = plt.figure()
ax = fig.add_subplot(111, xlim=(-10,10), ylim=(-10,10))
particles, = ax.plot([], [], 'o', ms=5)
def init():
global pbox
particles.set_data([],[])
return particles,
def animate(i):
global pbox, ax, fig
particles.set_data(pbox.state[:,0],pbox.state[:,1])
return particles,
ani = anim.FuncAnimation(fig, animate, frames = 500,
interval = 10, blit=True,
init_func=init)
plt.show()
*如何*已经失败了,那么远? – SiHa
我编辑了这个问题。 – Ebo