Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default line style cycler in matplotlib

I can set a default color cycle for lines in matplotlib using matplotlib.rcParams['axes.color_cycle'] = my_color_list, but I can't figure out how to do the same thing with line styles (i.e. '-','--','-.',':'). I know I can set this using something like

linecycler = itertools.cycle(lines)
for i in range(n):
    plt.plot(x[i],y[i],next(linecycler))

but I'd like to be able to do something more like the color cycle thing so I don't have to make a new cycler every time I want to plot. How can I do this?

like image 616
Ben Lindsay Avatar asked Nov 20 '15 05:11

Ben Lindsay


1 Answers

If you're running matplotlib 1.5 or greater, then you can introduce cyclers for all plot properties in rcParam using axes.prop_cycle (and axes.color_cycle was deprecated in favor of axes.prop_cycle). In short, you should be able to do something along these lines:

import matplotlib.pyplot as plt
from cycler import cycler
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
                           cycler('linestyle', ['-', '--', ':', '-.'])))

See this example and the docs for details.

like image 141
Andrey Sobolev Avatar answered Sep 28 '22 08:09

Andrey Sobolev