if I have a scatter plot in matplotlib that returns a CircleCollection:
coll = plt.scatter(rand(5), rand(5), c="k")
how can I reset the colors of just specific points? I notice that coll is not iterable. I want to just alter the face/edge colors of already plotted points, although they already have a color set from the initial plt.scatter call. How can this be done?
For example: just change the color of the second point plotted, e.g.:
coll[1].set_color("r") # this does not work, coll not indexable this way
I know I can pass a vector of colors to c= in scatter but I’d like to intentionally reset the points later, since the colors are not known for all the points at the time when plt.scatter is initially called.
edit: further explanation. I am looking for the simplest way to color points in a scatter based on different conditions. if points is a two-d array and you plot it with scatter(points[:, 0], points[:, 1], c-"k"), it’s convenient to later on based on certain conditions, e.g.
# replot certain points in red with alpha
selected = points[:, 0] > 0.5
plt.scatter(selected[:, 0], selected[:, 1], c="r", alpha=0.5)
here I replot over the old points but this is messy since the new points are plotted with an alpha, so it will not give the desired effect. The various conditions according to which points have to be re-colored might be complex and happen later than when the initial scatter is made, so it’s convenient to just be able to change the color of an existing point, rather than split the points up based on conditions and plot them all separately.
This works for me. Probably you need to call
plt.drawbefore (or instead of)fig.show.Updated
This is how to partially modify colors. You need to extend
colorsarray explicitly if you use the single color when callingscatter.