'How to draw a marker with dashed/dotted edge in matplotlib?
I want to draw a marker (a star) with dashed border lines instead of a solid border. Is there a way to do this in matplotlib? Here is a simple code that draws a marker with a solid border:
plot([1],[1],marker=(5,1,0),markersize=30,mfc='gold',mec='k',mew=1)
Thanks for your help in advance!
Solution 1:[1]
Well, I had the exactly opposite problem, i.e., I wanted to plot markers using solid lines (not dashed, dotted, etc...).
If you pass a linestyle to plot it should (unfortunately for me) plot the marker edge in the style of the passed linestyle.
Minimal working example:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5];
y = [0, 1, 4, 9, 16, 25];
plt.plot(x, y,
label='y = x^2',
linestyle=':',
marker='s',
color='r',
mec='r',
markersize=10,
mfc=(1,1,1,0.0)
);
plt.legend(loc='best', ncol=1, fontsize=13);
plt.savefig('/tmp/test.eps', format='eps', dpi=400, bbox_inches='tight');
Yeah, I know, no semicolons in python. :)
This is funny, but the markers are not visible after plt.show() -- saving the whole plot is required.
If you want to get the solid marker back just change last value in mfc (alpha channel I suppose) to, e.g., 0.0001. This allows keeping the markers solid but still 'almost' transparent.
Solution 2:[2]
A 'DIY' method.
Plot a star.
Plot some circle without with white markeredgecolor. It will cover the star border. So the star looks like dashed line.
Fill in the star with gold color, but without border.
The code looks like this:
import matplotlib.pyplot as plt
plt.plot([1], [1], marker=(5, 1, 0), markersize=30, mfc='none', mec='k', mew=1)
for i in range(4):
plt.plot([1], [1], marker='o', markersize=30-5*i,
mfc='none', mec='w', mew=1)
plt.plot([1], [1], marker=(5, 1, 0), markersize=30, mfc='gold', mec='k', mew=0)
plt.show()
The result figure is this:

A little weird. But it works!
Solution 3:[3]
I use scatter to do that
import matplotlib.pyplot as plt
plt.gcf().set_size_inches(6,4)
plt.scatter(0.5, 0, marker='*', s=1000, facecolors='y', edgecolors='b')
plt.scatter(1.5, 0, marker='*',s=1000, facecolors='y', edgecolors='b',linestyle='--')
plt.xlim(0,2)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | shycha |
| Solution 2 | Donald Duck |
| Solution 3 | John Chen |

