I've been looking for new line styles in matplotlib, and the only line styles available are ["-", "--", "-.", ":",]. (The style options ['', ' ', 'None',] don't count because they just hide the lines.)
Are there really only 4 line styles in Matplotlib pyplot? Are there any extensions that add further line styles? Is there a way to customise line styles? How about some three character line styles like:
These are just some ideas to expand the range of line styles.
Matplotlib in Python is a package that is used for displaying the 2D graphics. The Matplotlib can be used in python scripts, shell, web application servers and other GUI toolkits. Python provides different types of plots such as Bar Graph, Histogram, Scatterplot, Area plot, Pie plot for viewing the data.
In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.
You can use the dashes
kwarg to set custom dash styles.
From the docs:
Set the dash sequence, sequence of dashes with on off ink in points. If seq is empty or if seq = (None, None), the linestyle will be set to solid.
Here's some examples based on a few of your suggestions. Obviously there are many more ways you could customise this.
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)
# 3 dots then space
ax.plot(range(10), range(10), dashes=[3,6,3,6,3,18], lw=3,c='b')
# dash dash dot
ax.plot(range(10), range(0,20,2), dashes=[12,6,12,6,3,6], lw=3,c='r')
# dash dot dot
ax.plot(range(10), range(0,30,3), dashes=[12,6,3,6,3,6], lw=3,c='g')
I would like to add some additional info from 2021. In matplotlib ver. 3.3.4 the dashing options are a little different. You first cycle through the type of dash, and then add the ratio.
import matplotlib.pyplot as plt
plt.figure()
# dashed with dash length 10, space length 5
plt.hlines(3, 0, 5, linestyle="--", dashes=(0,(10,5)))
# dashed with dash length 5, space length 10
plt.hlines(6, 0, 5, linestyle="--", dashes=(0,(5,10)))
plt.ylim(0,10)
plt.show()
The result:
For more info check the matplotlib documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With