Answering the famous question "How to get different colored lines for different plots in a single figure?" I stumbled in a behavior that puzzled me...
The problem is to get different line colors in different subplots, like this
In [29]: import numpy as np
...: import matplotlib.pyplot as plt
In [30]: fig, axes = plt.subplots(2,1)
In [31]: for ax in axes.flatten():
...: ax.plot((0,1), (0,1))
As you can see, both lines are Blue — I can understand that this happens because each ax
has its own prop_cycle
.
I can remedy the problem using an explicit color name
In [44]: fig, axes = plt.subplots(2,1)
In [45]: for ax, short_color_name in zip(axes.flatten(), 'brgkyc'):
...: ax.plot((0,1), (0,1), short_color_name)
but if I try to reuse the same cycler
object...
In [47]: my_cy = plt.rcParams['axes.prop_cycle']
In [48]: for ax in axes.flatten():
...: ax.set_prop_cycle(my_cy)
...: ax.plot((0,1), (0,1))
I get two subplots with two Blue lines...
In my understanding, what I'd like to do is impossible because the ax
calls the cycler that returns an itertools.cycle
that in turn actually produces the kwargs
as needed, but I ask anyway because
There are more things in Matplotlib than are dreamt of in my philosophy.