Note
Go to the endto download the full example code.
Styling with cycler#
Demo of custom property-cycle settings to control colors and other styleproperties for multi-line plots.
Note
More complete documentation of thecycler API can be foundhere.
This example demonstrates two different APIs:
Setting the rc parameter specifying the default property cycle.This affects all subsequent Axes (but not Axes already created).
Setting the property cycle for a single pair of Axes.
fromcyclerimportcyclerimportmatplotlib.pyplotaspltimportnumpyasnp
First we'll generate some sample data, in this case, four offset sinecurves.
x=np.linspace(0,2*np.pi,50)offsets=np.linspace(0,2*np.pi,4,endpoint=False)yy=np.transpose([np.sin(x+phi)forphiinoffsets])
Nowyy has shape
print(yy.shape)
(50, 4)
Soyy[:,i] will give you thei-th offset sine curve. Let's set thedefaultprop_cycle usingmatplotlib.pyplot.rc(). We'll combine acolor cycler and a linestyle cycler by adding (+) twocycler'stogether. See the bottom of this tutorial for more information aboutcombining different cyclers.
default_cycler=(cycler(color=['r','g','b','y'])+cycler(linestyle=['-','--',':','-.']))plt.rc('lines',linewidth=4)plt.rc('axes',prop_cycle=default_cycler)
Now we'll generate a figure with two Axes, one on top of the other. On thefirst axes, we'll plot with the default cycler. On the second axes, we'llset theprop_cycle usingmatplotlib.axes.Axes.set_prop_cycle(),which will only set theprop_cycle for thismatplotlib.axes.Axesinstance. We'll use a secondcycler that combines a color cycler and alinewidth cycler.
custom_cycler=(cycler(color=['c','m','y','k'])+cycler(lw=[1,2,3,4]))fig,(ax0,ax1)=plt.subplots(nrows=2)ax0.plot(yy)ax0.set_title('Set default color cycle to rgby')ax1.set_prop_cycle(custom_cycler)ax1.plot(yy)ax1.set_title('Set axes color cycle to cmyk')# Add a bit more space between the two plots.fig.subplots_adjust(hspace=0.3)plt.show()

Settingprop_cycle in thematplotlibrc file or style files#
Remember, a custom cycler can be set in yourmatplotlibrcfile or a style file (style.mplstyle) underaxes.prop_cycle, e.g.
axes.prop_cycle : cycler(color=['red', 'royalblue', 'gray'])
For colors, a single string may be used either for one of theNamed color sequences
axes.prop_cycle : cycler(color='Accent')
or if each color has a single character name:
axes.prop_cycle : cycler(color='bgrcmyk')
Cycling through multiple properties#
You can add cyclers:
Results in:
{'color':'r','linestyle':'-'}{'color':'g','linestyle':'--'}{'color':'b','linestyle':'-.'}
You can multiply cyclers:
Results in:
{'color':'r','linestyle':'-'}{'color':'r','linestyle':'--'}{'color':'r','linestyle':'-.'}{'color':'g','linestyle':'-'}{'color':'g','linestyle':'--'}{'color':'g','linestyle':'-.'}{'color':'b','linestyle':'-'}{'color':'b','linestyle':'--'}{'color':'b','linestyle':'-.'}