如何在 matplotlib 绘图中只使一个标记为空心?

How to make just one marker as hollow in matplotlib plot?

我有以下代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(5)
y = np.random.rand(5)

markerfacecolors = ['k','k','none','k','k']

plt.plot(x,y,'--o',markerfacecolor=markerfacecolors)

我尝试了上面的方法并得到了以下错误-

ValueError: RGBA sequence should have length 3 or 4

我该如何解决这个问题?或者是否有解决问题的替代方案(而不是分成多个数字)?

如果您阅读与 plt.plot 相关的文档,您会发现影响标记的唯一属性是:

marker: marker style string
markeredgecolor or mec: color
markeredgewidth or mew: float
markerfacecolor or mfc: color
markerfacecoloralt or mfcalt: color
markersize or ms: float
markevery: None or int or (int, int) or slice or list[int] or float or (float, float) or list[bool]

通过查看它们,似乎无法为单个或几个选定的标记设置自定义样式。请记下您使用 markerfacecolor=markerfacecolors 的错误之一。但是,markerfacecolor 需要是一种颜色(字符串或元组 (R, G, B)),而不是多种颜色。

绕过这种设计选择的一种方法是使用多个绘图命令,掩盖感兴趣的 value/s。例如:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

x = np.random.rand(5)
y = np.random.rand(5)

mask1 = np.ones_like(x, dtype=bool)
mask1[3] = False
mask2 = np.logical_not(mask1)

# Matplotlib uses Tab10 as default colorloop.
# Need to use the same color for each plot command
color = cm.tab10.colors[0]

plt.figure()
plt.plot(x,y,'--')
plt.plot(np.ma.masked_array(x, mask2),y,'o', color=color)
plt.plot(np.ma.masked_array(x, mask1),y,'o', color=color, markerfacecolor='none')