color cycler in matplotlib - how to repeat each color once before cycling

Viewed 137

if I have 4 curves on a plot, and I want to present them in a sequential color scheme (i.e. Blues colormap), I would add the following piece of code:


num_plots=4

plt.gca().set_prop_cycle(plt.cycler('color', plt.cm.Blues(np.linspace(0.2, 1, num_plots))))

Each curve will be a different color of blue.

What if I want the colors to repeat themselves once in the sequence? So instead of "light blue, blue, dark blue, dark dark blue" as the order of color of the curves, instead it becomes "light blue, light blue, dark blue, dark blue". The color only changes after two sequential curves, as opposed to just one. How can I modify my code to do this?

1 Answers

You can achieve this by modifying the array passing to plt.cm.Blues. For example

num_plots=4
color_source = np.linspace(0.2,1,num_plots)
color_repeat = np.repeat(color_source,2)

plt.gca().set_prop_cycle(plt.cycler('color',plt.cm.Blues(color_repeat)))
for ind in range(20):
    plt.plot(np.linspace(1,10,10)+ind)

output

Related