Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i cycle through line styles in matplotlib

I know how to cycle through a list of colors in matplotlib. But is it possible to do something similar with line styles (plain, dotted, dashed, etc.)? I'd need to do that so my graphs would be easier to read when printed. Any suggestions how to do that?

like image 265
Charles Brunet Avatar asked Oct 17 '11 20:10

Charles Brunet


People also ask

What is %Matplotlib inline?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.

What is the difference between %Matplotlib inline and %Matplotlib notebook?

Matplotlib plots do not display in Pycharm. The % notation is for using the magic functions available in python, and %matplotlib inline, represents the magic function %matplotlib, which specifies the backend for matplotlib, and with the argument inline you can display the graph and make the plot interactive.

What does %Matplotlib do in Python?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.


1 Answers

Something like this might do the trick:

import matplotlib.pyplot as plt from itertools import cycle lines = ["-","--","-.",":"] linecycler = cycle(lines) plt.figure() for i in range(10):     x = range(i,i+10)     plt.plot(range(10),x,next(linecycler)) plt.show() 

Result: enter image description here

Edit for newer version (v2.22)

import matplotlib.pyplot as plt from cycler import cycler # plt.figure() for i in range(5):     x = range(i,i+5)     linestyle_cycler = cycler('linestyle',['-','--',':','-.'])     plt.rc('axes', prop_cycle=linestyle_cycler)     plt.plot(range(5),x)     plt.legend(['first','second','third','fourth','fifth'], loc='upper left', fancybox=True, shadow=True) plt.show() 

For more detailed information consult the matplotlib tutorial on "Styling with cycler"
To see the output click "show figure"

like image 97
Avaris Avatar answered Oct 13 '22 11:10

Avaris