I am making a stacked histogram where one must enter the desired colors together in a list/array when the histogram is called. For certain objects in my code, it’s helpful to assign a color to them, so that they are immediately identified across various plots. Therefore, I essentially want to take the color cycle, swap out a few entries for which colors have been assigned by the user, and otherwise keep the cycle in tact. For example, if the first object is to be orange, but no other colors are assigned, I want something like:
colors= [‘orange’, default_cycle[1::]]
However, according to some threads, the only way to access the color cycle that I’m aware of is through a generator stored in:
axes._get_lines.color_cycle()
I don’t like this approach because iterating through the color cycle will cause the next plot to start at a different point in the cycle. I’m sure I can hack something up that gets around this, but there seems to be a canonical way to just list all of the default colors in a list once and be done with it. Is this the case?
I am making a stacked histogram where one must enter the desired colors
together in a list/array when the histogram is called. For certain
objects in my code, it's helpful to assign a color to them, so that they
are immediately identified across various plots. Therefore, I
essentially want to take the color cycle, swap out a few entries for
which colors have been assigned by the user, and otherwise keep the
cycle in tact. For example, if the first object is to be orange, but no
other colors are assigned, I want something like:
colors= ['orange', default_cycle[1::]]
However, according to some threads, the only way to access the color
cycle that I'm aware of is through a generator stored in:
axes._get_lines.color_cycle()
If I'm not mistaken, you should be able to set the appropriate rcParam,
i.e.,
I don't like this approach because iterating through the color cycle
will cause the next plot to start at a different point in the cycle.
I'm sure I can hack something up that gets around this, but there seems
to be a canonical way to just list all of the default colors in a list
once and be done with it. Is this the case?
Well, the hack wasn’t as messy as I thought. Still feel like there’s a better way…
def show_colors_default():
fig, axfoo = plt.subplots()
clist =
c = axfoo._get_lines.color_cycle.next()
Iterate until duplicate is found
while c not in clist:
clist.append(c)
c = axfoo._get_lines.color_cycle.next()
Reset colorcycle
for i in range(len(clist) -1):
axfoo._get_lines.color_cycle.next()
return clist
···
On Wed, Mar 5, 2014 at 2:56 PM, Adam Hughes <hughesadam87@…287…> wrote:
Hi,
I am making a stacked histogram where one must enter the desired colors together in a list/array when the histogram is called. For certain objects in my code, it’s helpful to assign a color to them, so that they are immediately identified across various plots. Therefore, I essentially want to take the color cycle, swap out a few entries for which colors have been assigned by the user, and otherwise keep the cycle in tact. For example, if the first object is to be orange, but no other colors are assigned, I want something like:
colors= [‘orange’, default_cycle[1::]]
However, according to some threads, the only way to access the color cycle that I’m aware of is through a generator stored in:
axes._get_lines.color_cycle()
I don’t like this approach because iterating through the color cycle will cause the next plot to start at a different point in the cycle. I’m sure I can hack something up that gets around this, but there seems to be a canonical way to just list all of the default colors in a list once and be done with it. Is this the case?
Thanks Andreas. That is correct; however, I’d rather not make this change global. I only want a subset of my plots to have this behavior. I feel like changing the rcparams would change this globally and probably confuse users who don’t know this is being called.
I should have realized this before posting, but by putting this into a function as I’ve shown above, the foo plot is destroyed, so it doesn’t actually appear in notebooks (which is why I was hesitant to use it in the first place). Therefore, reseting the color cycle is not necessary and this is all that is requried.
def show_colors_default():
axfoo = plt.subplots()[1]
c = axfoo._get_lines.color_cycle.next()
Iterate until duplicate is found
while c not in clist:
clist.append(c)
return clist
My histogram would merely access this and be done with it.
···
On Wed, Mar 5, 2014 at 3:07 PM, Andreas Hilboll <lists@…3067…> wrote:
On 05.03.2014 20:56, Adam Hughes wrote:> Hi,
I am making a stacked histogram where one must enter the desired colors
together in a list/array when the histogram is called. For certain
objects in my code, it’s helpful to assign a color to them, so that they
are immediately identified across various plots. Therefore, I
essentially want to take the color cycle, swap out a few entries for
which colors have been assigned by the user, and otherwise keep the
cycle in tact. For example, if the first object is to be orange, but no
other colors are assigned, I want something like:
colors= [‘orange’, default_cycle[1::]]
However, according to some threads, the only way to access the color
cycle that I’m aware of is through a generator stored in:
axes._get_lines.color_cycle()
If I’m not mistaken, you should be able to set the appropriate rcParam,
2014-03-05 21:13 GMT+01:00 Adam Hughes <hughesadam87@...287...>:
Thanks Andreas. That is correct; however, I'd rather not make this change
global. I only want a subset of my plots to have this behavior. I feel
like changing the rcparams would change this globally and probably confuse
users who don't know this is being called.
Try using rc_context:
with plt.rc_context(rc={'axes.color_cycle': ['orange', default_cycle[1::]]}):
plt.plot(...)
This should change the color cycle only within the scope of the with
clause (not tested with this particular rcparam).