I have the following code:
JavaScript
x
10
10
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
x = np.random.rand(5)
5
y = np.random.rand(5)
6
7
markerfacecolors = ['k','k','none','k','k']
8
9
plt.plot(x,y,'--o',markerfacecolor=markerfacecolors)
10
I tried the above and got the following error-
JavaScript
1
2
1
ValueError: RGBA sequence should have length 3 or 4
2
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:
JavaScript
1
8
1
marker: marker style string
2
markeredgecolor or mec: color
3
markeredgewidth or mew: float
4
markerfacecolor or mfc: color
5
markerfacecoloralt or mfcalt: color
6
markersize or ms: float
7
markevery: None or int or (int, int) or slice or list[int] or float or (float, float) or list[bool]
8
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:
JavaScript
1
20
20
1
import numpy as np
2
import matplotlib.pyplot as plt
3
import matplotlib.cm as cm
4
5
x = np.random.rand(5)
6
y = np.random.rand(5)
7
8
mask1 = np.ones_like(x, dtype=bool)
9
mask1[3] = False
10
mask2 = np.logical_not(mask1)
11
12
# Matplotlib uses Tab10 as default colorloop.
13
# Need to use the same color for each plot command
14
color = cm.tab10.colors[0]
15
16
plt.figure()
17
plt.plot(x,y,'--')
18
plt.plot(np.ma.masked_array(x, mask2),y,'o', color=color)
19
plt.plot(np.ma.masked_array(x, mask1),y,'o', color=color, markerfacecolor='none')
20