Is there a simple way to increment the matplotlib color cycle without digging into axes internals?
When plotting interactively a common pattern I use is:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(x,y1)
plt.twinx()
plt.plot(x,y2)
The plt.twinx()
in necessary to get different y-scales for y1 and y2 but both plots are drawn with the first color in the default colorcycle making it necessary to manually declare the color for each plot.
There must be a shorthand way to instruct the second plot to increment the color cycle rather than explicitly giving the color. It is easy of course to set color='b'
or color='r'
for the two plots but when using a custom style like ggplot
you would need need to lookup the color codes from the current colorcycle which is cumbersome for interactive use.
Similar to the other answers but using matplotlib color cycler:
There are several colour schemes available in Pyplot. You can read more on the matplotlib tutorial Specifying Colors.
From these docs:
You can cycle through the colour scheme as follows:
This could be improved by cycling over
matplotlib.rcParams['axes.prop_cycle']
directly.You could call
to advance the color cycler on color. Unfortunately, this accesses the private attribute
._get_lines
, so this is not part of the official public API and not guaranteed to work in future versions of matplotlib.A safer but less direct way of advance the color cycler would be to plot a null plot: