Is it possible to query the current state of the matplotlib color cycle? In other words is there a function get_cycle_state that will behave in the following way?
>>> plot(x1, y1)
>>> plot(x2, y2)
>>> state = get_cycle_state()
>>> print state
2
Where I expect the state to be the index of the next color that will be used in a plot. Alternatively, if it returned the next color (“r” for the default cycle in the example above), that would be fine too.
Accessing the color cycle iterator
There’s no “user-facing” (a.k.a. “public”) method to access the underlying iterator, but you can access it through “private” (by convention) methods. However, you’d can’t get the state of an
iteratorwithout changing it.Setting the color cycle
Quick aside: You can set the color/property cycle in a variety of ways (e.g.
ax.set_color_cyclein versions <1.5 orax.set_prop_cyclerin >=1.5). Have a look at the example here for version 1.5 or greater, or the previous style here.Accessing the underlying iterator
However, while there’s no public-facing method to access the iterable, you can access it for a given axes object (
ax) through the_get_lineshelper class instance.ax._get_linesis a touch confusingly named, but it’s the behind-the-scenes machinery that allows theplotcommand to process all of the odd and varied ways thatplotcan be called. Among other things, it’s what keeps track of what colors to automatically assign. Similarly, there’sax._get_patches_for_fillto control cycling through default fill colors and patch properties.At any rate, the color cycle iterable is
ax._get_lines.color_cyclefor lines andax._get_patches_for_fill.color_cyclefor patches. On matplotlib >=1.5, this has changed to use thecyclerlibrary, and the iterable is calledprop_cyclerinstead ofcolor_cycleand yields adictof properties instead of only a color.All in all, you’d do something like:
You can’t view the state of an
iteratorHowever, this object is a “bare”
iterator. We can easily get the next item (e.g.next_color = next(color_cycle), but that means that the next color after that is what will be plotted. By design, there’s no way to get the current state of an iterator without changing it.In
v1.5or greater, it would be nice to get thecyclerobject that’s used, as we could infer its current state. However, thecyclerobject itself isn’t accessible (publicly or privately) anywhere. Instead, only theitertools.cycleinstance created from thecyclerobject is accessible. Either way, there’s no way to get to the underlying state of the color/property cycler.Match the color of the previously plotted item instead
In your case, it sounds like you’re wanting to match the color of something that was just plotted. Instead of trying to determine what the color/property will be, set the color/etc of your new item based on the properties of what’s plotted.
For example, in the case you described, I’d do something like this:
It’s not the only way, but its cleaner than trying to get the color of the plotted line before-hand, in this case.