【发布时间】:2017-08-20 01:05:23
【问题描述】:
我正在尝试为动画中的每个点设置不同的颜色。我想将数组 c_state 作为每个点的 hsv 元组的色调值传递。到目前为止,我尝试的一切都失败了。我试过在动画函数中使用它:
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()
【问题讨论】:
-
如何到目前为止失败了?
-
我已经编辑了问题。
标签: python arrays matplotlib colors