Reputation: 31
I seems that it is not possible to change colors of a Matplotlib scatter plot through a RGB definition. Am I wrong?
Here is a code (already given in stack overflow) which work with colors indexed in 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()
But if color_data
is defined through RGB colors, I get an error:
ValueError: Collections can only map rank 1 arrays
The related code is the following (in this code, I just change the color of one sample each time):
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()
Is there a means to use set_array
with RGB color array?
Upvotes: 3
Views: 3551
Reputation: 40747
Not sure what you are trying to achieve. But if you are trying to change the color, why not use the set_color()
function of Collection
?
def update_plot2(i,data,scat):
data[ i%10 ] = np.random.random((3))
scat.set_color(data) # <<<<<<<<<<<<<<<<<<<
return scat,
Upvotes: 6