Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there really only 4 Matplotlib Line Styles?

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.

like image 899
Lee Avatar asked Nov 26 '15 10:11

Lee


People also ask

How many types of graphs are there in matplotlib?

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.

What happens if I dont use %Matplotlib inline?

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.


Video Answer


2 Answers

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

like image 50
tmdavison Avatar answered Oct 11 '22 23:10

tmdavison


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.

like image 40
Andrej Kružliak Avatar answered Oct 11 '22 23:10

Andrej Kružliak