【问题标题】:Different color points from an array in matplotlib animationmatplotlib动画中数组的不同色点
【发布时间】: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


【解决方案1】:

matplotlib 中的线图plt.plot() 只有一种颜色。您不能为其段设置不同的颜色。

您需要的是散点图plt.scatter。您可以使用

更新散点图
sc = plt.scatter(x,y, c=.., s =.., cmap="hsv")
sc.set_offsets(np.c_[x,y]) # set positions
sc.set_array(color) # set color

在这种情况下,color 将是一个 0 到 1 之间值的一维数组。这些值将使用 hsv 颜色映射映射到 hsv 颜色,该颜色映射在 scatter 的 cmap 参数中指定。

完整的代码如下所示:

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

n = 140

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()
    def iterate(self):
        self.state += (np.random.random((n, 2))-0.5)/3.
        self.state[self.state > 10.] = 10
        self.state[self.state < -10] = -10
        self.color += (np.random.random(n)-0.5)/89.
        self.color[self.color>1.] = 1.
        self.color[self.color<0] = 0.


i_state = -5 + 10 * np.random.random((n, 2))
c_state = np.random.random(n)

pbox = ParticleBox(i_state, c_state)

fig = plt.figure()
ax =  fig.add_subplot(111, xlim=(-10,10), ylim=(-10,10))

particles = ax.scatter([], [], c=[],s=25, cmap="hsv", vmin=0, vmax=1)

def animate(i):
    pbox.iterate()
    particles.set_offsets(pbox.state)
    particles.set_array(pbox.color)
    return particles,

ani = anim.FuncAnimation(fig, animate, frames = 140, 
                         interval = 10, blit=True)

plt.show()

【讨论】:

  • 太好了,这正是我想要的!谢谢!
  • 我将如何使粒子的大小也动态化?我尝试过传递另一个“set_array()”,但这会使所有粒子都非常小。
  • .set_array() 确定颜色。对于大小使用.set_sizes()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 2017-07-19
  • 2018-10-29
  • 1970-01-01
相关资源
最近更新 更多