我看起来好像不能通过RGB定义更改Matplotlib散点图的颜色。 我错了吗?
以下是已经在stackoverflow中提供的使用浮点数索引颜色的代码:
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: Collections can only map rank 1 arrays
相关代码如下(在这段代码中,我只是每次更改一个样本的颜色):
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()
有没有一种方法可以使用RGB颜色数组来使用
set_array
函数?
scat.set_array()
更好,因为scat.set_color()
可以使用RGB值! - Maghoumi