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?
%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.
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.
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.
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:
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"
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