我似乎无法通过RGB定义更改Matplotlib散点图的颜色。我错了吗?
这是一个代码(已经在堆栈溢出中给出),它使用以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()
但如果通过RGB颜色定义color_data
,我会收到错误:
ValueError:集合只能映射排名1数组
相关代码如下(在此代码中,我每次只更改一个样本的颜色):
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颜色数组一起使用?
答案 0 :(得分:3)
不确定您要实现的目标。但是,如果您尝试更改颜色,为什么不使用set_color()
的{{1}}函数?
Collection