-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Axhline doesn’t cycle through colors #16714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Pretty sure this has always been the case. So, at the very least, this
isn't a regression (unless you can show us an earlier version of matplotlib
that does cycle colors for axhline()). Related to this question is whether
it *should* utilize the style cycle system like plot() does, and how should
it interact with plot()/bar()/hist()/etc. using the style-cycler? In other
words, when using a combination of plot() and axhline() calls, should each
of them use a separate instance of the cycler, or should they use the same
one that gets advanced by each call to plot() and axhline()? At the moment,
I am inclined to say that axhline() should *not* utilize the cycler, but
usually my use-case is for at most one axhline() call, if I use it at all.
…On Mon, Mar 9, 2020 at 2:02 PM 01baftb ***@***.***> wrote:
As requested by @story645 <https://github.com/story645>, I am posting a
possible bug I reported in the forum
https://discourse.matplotlib.org/t/axhline-doesnt-cycle-through-colors/20938
When I plot horizontal lines using axhline, there resulting lines are all
the same color. It doesn’t cycle automatically. Is this behavior expected?
Do I need to manually setup the color cycling? If so how can I do this
easily? Why does matplotlib not automatically cycle through the colors for
axhline?
matplotlib v3.2.0
%matplotlib widget
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
count = 5
for n in range(count):
ax.axhline(y=n**2, label=n)
ax.legend()
plt.show()
[image: image]
<https://user-images.githubusercontent.com/43527304/76243175-4b437e00-620e-11ea-8258-8a49d15bf2bf.png>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#16714?email_source=notifications&email_token=AACHF6E6CGNA3GMPLTTNTBLRGU4N3A5CNFSM4LEOKOXKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4ITU7AGQ>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACHF6A5GFV5PRYVVAAK2V3RGU4N3ANCNFSM4LEOKOXA>
.
|
I don’t think axhline should cycle. |
I think it should since it's a line plotting method and those usually cycle |
Given that the lines are typically used for special values not data, always intercept the perpendicular axes, and hence you know the value of the line, and the lines do not cross one another, having them cycle color doesn't seem to add any information, and just serves to complicate the plot needlessly. |
FWIW I don't think these need to or should cycle. But 100% of the time I use this function, I set them to a gray color. So, a vote in favor of an (eventual) move away from |
Closing as this is tracked in #14331. But there‘s a clear tendency that we want a single color, possibly the grid color. |
I agree with @story645 that axhline should cycle. Below is an example use case where color cycling would be helpful. I would want the color of the axhline to match the corresponding ax.plot() line. In the example below, you can see that the color on the axhline doesn't cycle. This burdens the end user to keep track of the current plot color and manually specify the color for the axhline. Alternately there are cases where user wouldn't want the color to cycle, in such case they can specify the color and keep it constant every time as @mwaskom mentioned where he sets them to a gray color. Thus, if axhline color is cycled for default, it is much easier to keep it constant color when desired, as opposed to if it didn't cycle by default, the end user is burdened to keep track of the color cycling in each loop.
|
FWIW all you need to do here is ...
line, = ax.plot(x,decay(C,K,x), marker='o', linestyle='--', label=C)
# Plot the limit of decay on right y axis
twin_ax.axhline(y=C, color=line.get_color())
... |
Note, if Instead, the bottom curve would be blue, the line at 5 would be orange, the next curve would be green, the line at 6 would be red, etc. |
Yes, I agree. I will use this approach in my current end use case. However, I created this issue as an opinion that might be looked into. I understand the end implementations might be complicated or may not make sense as a standard approach, in which case my opinion can be ignored.
Wouldn't axhline have its own instance of the cycler as @WeatherGod previously mentioned? For example, updating my example code to include both ax.scatter() and ax.plot(), you can see each of them have their own instance of the cycler, thus the colors match up. # Plot the exponential delcay on left y axis
ax.scatter(x,decay(C,K,x), marker='o', )
ax.plot(x,decay(C,K,x), marker='', linestyle='--', label=C)
# Plot the limit of decay on right y axis
twin_ax.axhline(y=C) |
Ah true, I guess that depends on whether it's considered a line like a plot or not. But that discussion can continue on #14331. |
.... but that is still fragile in that you must keep the cycler instances in sync as you do the plotting. Even worse than having just one color would be to have the wrong color. Is there any precedence for using two cyclers? I'm not aware of us doing that anywhere. |
internally, matplotlib has multiple copies of the cycler for each plot
type, specifically, line and bar, if I remember correctly. To have an
independently cycling axhline(), you would need a third one in the same
vein.
…On Tue, Mar 10, 2020 at 4:21 PM Jody Klymak ***@***.***> wrote:
Wouldn't axhline have its own instance of the cycler as @WeatherGod
<https://github.com/WeatherGod> previously mentioned?
.... but that is still fragile in that you must keep the cycler instances
in sync as you do the plotting. Even worse than having just one color would
be to have the wrong color.
Is there any precedence for using two cyclers? I'm not aware of us doing
that anywhere.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#16714?email_source=notifications&email_token=AACHF6A3P37AHMDN2BCZYWLRG2OMXA5CNFSM4LEOKOXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOM77WY#issuecomment-597295067>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACHF6D4NNXMDDDPLVP434DRG2OMXANCNFSM4LEOKOXA>
.
|
This is yet another case where having aspect of being both an application and a library causes us pain on the edges. On the application-like side of things On the other hand, when used as a library (which you are doing @01baftb ) to write your plotting tools, this global state can start to fight back / be more trouble than it's worth. If we were to start to letting In these cases where you have many things that you want to cycle together, I suggest explicitly using a cycler for sty in my_cycler:
ax.plot(..., **sty)
ax.axhline(...., **sty) and so on. See https://matplotlib.org/cycler/ for more details.
We do not want to ignore your opinion, feed back from users is very important. However, everything is a trade off, making one use case easier almost always another harder and in a worst case impossible. |
Funnily, |
Probably nobody ever noticed. Who would put a scatter plot on top of a bar plot? |
Our user base is both very diverse and very clever, I'm sure someone has a good reason for this (bars for averages, scatter for outliers like a roll-your-own boxplot?). |
This is probably the most common style of plot in Nature journals these days, which has mandated a “show the observations” rule. |
As requested by @story645, I am posting a possible bug I reported in the forum https://discourse.matplotlib.org/t/axhline-doesnt-cycle-through-colors/20938
When I plot horizontal lines using
axhline
, there resulting lines are all the same color. It doesn’t cycle automatically. Is this behavior expected? Do I need to manually setup the color cycling? If so how can I do this easily? Why does matplotlib not automatically cycle through the colors foraxhline
?matplotlib v3.2.0
The text was updated successfully, but these errors were encountered: