简体   繁体   English

在matplotlib中重置已绘制散点的点

[英]resetting points of an already plotted scatter in matplotlib

if I have a scatter plot in matplotlib that returns a CircleCollection: 如果我在matplotlib中有一个散点图,它返回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. 我注意到coll不是可迭代的。 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. 我只想更改已经绘制的点的面/边颜色,尽管它们已经从初始plt.scatter调用中设置了颜色。 How can this be done? 如何才能做到这一点?

For example: just change the color of the second point plotted, eg: 例如:只需更改绘制的第二个点的颜色,例如:

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. 我知道我可以将颜色向量scatter传递给c= ,但是我想稍后有意重置这些点,因为在最初调用plt.scatterplt.scatter所有点都知道颜色。

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, eg 如果points是二维数组,并且使用scatter(points[:, 0], points[:, 1], c-"k")对其进行绘制,则以后根据某些条件(例如,

# 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. 在这里我重新绘制旧点,但这很麻烦,因为新点是用alpha绘制的,因此不会产生所需的效果。 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.draw before (or instead of) fig.show . 可能您需要在plt.draw之前(或代替它) fig.show

coll = plt.scatter(rand(5), rand(5), c="k")
fig = plt.gcf()
fig.show()  # or fig.savefig("a.png")

coll.set_color(['b', 'g', 'r', 'y', 'k'])
fig.show()  # or fig.savefig("b.png")

Updated 更新

This is how to partially modify colors. 这是部分修改颜色的方法。 You need to extend colors array explicitly if you use the single color when calling scatter . 如果在调用scatter时使用单一颜色,则需要显式扩展colors数组。

num = 5
coll = plt.scatter(rand(num), rand(num), c='k')
# coll = plt.scatter(rand(num), rand(num), c=['b', 'g', 'r', 'y', 'k'])
fig = plt.gcf()
fig.show()

colors = coll.get_facecolor()
if colors.shape[0] == 1:
    newcolors = np.tile(colors, (num, 1))
else:
    newcolors = colors
newcolors[0] = [0, 0.75, 0.75, 1]
coll.set_color(newcolors)

You need to use separate lists: 您需要使用单独的列表:

plt.scatter(x1, y1, c='b')
plt.scatter(x2, y2, c='k')

Look at this . 这个

You can also have a list in c of the same length as the x and y. 您还可以在c中拥有与x和y长度相同的列表。

When you call scatter , pass every point a color, then you can change the _facecolors ndarray directly. 调用scatter ,为每个点传递一种颜色,然后可以直接更改_facecolors ndarray。

from matplotlib import pyplot as plt
from matplotlib.collections import PathCollection
from numpy.random import rand
x = rand(5)
y = rand(5)
coll = plt.scatter(x, y, c=["k"]*len(x)) # must set color for every point
coll._facecolors[2,:] = (1, 0, 0, 1)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM