Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Line plot markers of different color

I want to mark certain indices in the line plot. I have referred to the following question on stack overflow and written the following:

plt.plot(range(len(y)), y, '-bD', markevery=rare_cases, label='%s' % target_var_name)

However, this produces the following: enter image description here

How can I keep the line plot in blue but make the markers in red ?

like image 486
Perl Avatar asked Mar 15 '20 09:03

Perl


Video Answer


1 Answers

From the documentation of plt.plot:

matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

The function takes **kwargs:

**kwargsLine2D properties, optional kwargs are used to specify properties like a line label (for auto legends), line-width, antialiasing, marker face color [...] Here is a list of available Line2D properties: [...]

markeredgecolor or mec color
markeredgewidth or mew float
markerfacecolor or mfc color
markerfacecoloralt or mfcalt color

So, you can feed markerfacecolor and markeredgecolor as keyword arguments:

x = np.random.randint(0,10,5)
y = np.random.randint(0,10,5)
    
plt.plot(x, y, '-bD',  c='blue', mfc='red', mec='k')

enter image description here

like image 161
warped Avatar answered Sep 28 '22 09:09

warped