20

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:

  • '--.': dash dash dot
  • '-..': dash dot dot
  • '...': dot dot dot (space)
  • 'xxx': x's in a line
  • '\/': Zig zags ie '\/\/\/\/'
  • '::': parrallel dots, ie :::::

These are just some ideas to expand the range of line styles.

Lee
  • 640
  • 7
  • 10

2 Answers2

21

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')

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
4

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: Two differently dashed lines

For more info check the matplotlib documentation.

DaveL17
  • 1,673
  • 7
  • 24
  • 38