Skip to content
Advertisement

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

I have the following code:

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)

I tried the above and got the following error-

ValueError: RGBA sequence should have length 3 or 4

How do I resolve this ? or is there an alternative solution to the problem ( rather than splitting into multiple figures)?

Advertisement

Answer

If you read the documentation associated to plt.plot, if you will see that the only properties affecting markers are:

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]

By looking at them, it appears that there are no way to set custom style to a single or a few selected markers. Note one of your errors, where you used markerfacecolor=markerfacecolors. However, markerfacecolor needs to be a color (either a string, or an tuple (R, G, B)), not multiple colors.

One way to get around this design choice is using multiple plot commands, masking the interested value/s. For example:

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')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement