-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: better handle margins on ax.bar #7471
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
Conversation
Cancel margin on the proper sides depending on if the bar is positive, negative, or mixed.
Hmm, I think the self.use_*_margin should be an attribute of the class so that it persists, but I dunno if all(h =< 0 for h in height):
self.use_bottom_margin = True
elif all (h>=0 for h in height):
self.use_top_margin = True
else:
self.use_bottom_margin = True
self.use_top_margin = True |
The margin information is persisted through the |
OK, but my point is that if it persists and you use exclusionary logic, that should fix the multiple bars weirdness while still hold for the other cases. (because there should never actually be a false/false) |
I do not follow 😞 . I think the problem is that the bars don't know about each other so one asks for the bottom to be sans-margin and the other for the top to be sans-margin. Still not sure what the best approach here is. One thought is to make the 'margin' values to me much more expressive (ex @anntzer suggested that we should only consider artists that are on the edge (ex, maybe by computing the bounding box for everything but one artist and find the artists the change the bbox size and ask them what to do about margins). That might be expensive (the artists probably need to do a better job at caching their data and (maybe) screen size (which would also help with hit-testing?) the Sorry for very rambly post. |
Hmm, I think we're talking past each other a bit then. I'm thinking that since the margins are an axis property, that solves the boxes unaware problem. Like they start off being turned off (False) and then turned on as required by the graphs (the sample if statements I threw out). Essentially, bars can only turn on margins, they don't have the power to turn them off. But @anntzer's solution might be cleaner. |
See the logic in https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_base.py#L2182 which allows artists to 'cancel' the default margins. |
I think that the "conflicting case" that @tacaswell noted indicates that the margin cancellation system is clearly not designed to handle the case of multiple artists. I think I have a better idea. Let's drop the margins cancellation system, and instead let each artist maintain two lists of "sticky" values (one for the x axis and one for the y axis). For example, a vertical bar plot would register a y-sticky value at y=0; an image would register two x-sticky and two y-sticky values (at -0.5 and nx-0.5, and -0.5 and ny-0.5). During auto-scaling, first compute the union of the bounding boxes, as is done right now. Then, if an edge of the union is at a "sticky" value, don't apply the margins to that edge. Note that this allows handling bar plots (including multiple of them) without any special checking of positive vs negative values. |
@anntzer I like that idea (if that is what you suggested before, sorry for not understanding), it is much more elegant than what @mdboom and I came up with. Have a slight concern about float equality, might have to do 'if sticky value is "close to" edge' Can you take a crack at implementing it? All of the margins property / cancellation related stuff can be ripped out with no deprecation (it has only been released in betas). |
No, that was not what I thought of before. I think I had proposed two other strategies before:
I will try implementing my proposal. |
As a side note, setting |
I'm going to close this on the assumption it is superseded by #7476, which will be merged eventually, I hope. |
Cancel margin on the proper sides depending on if the bar is positive,
negative, or mixed.
This mostly works, some pathological issues with multiple bar plots