-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Cairo + plot_date = misaligned x-axis labels #1810
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
I can reproduce on MacOSX backend. No idea what would be causing this |
we've been trying to figure this out with @jsundram and have concluded that the issue goes back to at least 1.0.0, which means this has probably always been the behavior. The likely culprit is the fact that some months have various combinations of ascenders and descenders (some have one but not the other, some have both, etc). This is also not limited to the Cairo backend - I'm seeing it on Agg as well, and not only on OS X - I'm running Debian |
partial improvement: |
Yes -- this problem has existed at least as long as I've been on the scene. The solutions are all rather tricky given how text is laid out in matplotlib, but maybe it's time to look at this anew. |
Ok -- I think I was confused -- the rendering is fine on Agg as far as I can tell. @ivanov: can you attach a PNG here showing the problematic output you're seeing with Agg? There is a legitimate bug in Cairo -- it is not taking the descent into account when placing the text. I suspect the Mac OSX backend is doing the same thing, based on looking at the source code. I think the correct way to fix this is to update all of the backends to assume the text position is always the baseline, not the bottom of the text's bounding box. That's the natural way that most of the backend formats want to work anyway -- the only exception there is Agg, but it should be easy enough to reverse its behavior. Expect a PR shortly. Can I get a volunteer to test Mac OS-X for me? |
@mdboom, I'll be happy to test. |
…X backends. The renderer backends' draw_text method is now passed the location of the text baseline, not the bottom of the text bounding box.
I've attached a fix to this PR. Rather than taking the easy way out, I took the opportunity to fix this once and for all. Backends are now passed the location of the baseline of the text, rather than the bottom of the bounding box. The only backend that really "natively" wants the latter is the Agg backend, and it now has code to compensate. All other backends have been simplified as a result. To all: I've created a script that will help diagnose alignment problems (it's also included in this PR as a test). If I'm asking for help testing, please make sure that the output of Agg matches the output of the backend in question. https://gist.github.com/mdboom/5391801 @jsundram: Would you mind testing the OS-X backend? It should require no changes to the backend itself, as the text layout engine should now be passing it what it wants. This should also address the original issue with the Cairo backend, which I have verified on Linux. @pwuertz: Can you confirm that I didn't hose something with the PGF backend? @ivanov: Are you still seeing misaligned text in the Agg backend? |
@@ -154,8 +154,10 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): | |||
ox, oy, width, height, descent, font_image, used_characters = \ | |||
self.mathtext_parser.parse(s, self.dpi, prop) | |||
|
|||
x = np.round(x + ox) | |||
y = np.round(y - oy) | |||
xd = descent * np.sin(angle / (180.0 * np.pi)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if np.deg2rad
is preferable here... it might even be quicker (untested).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Forgot about that...
In terms of code, this is a really good simplification. If the others find this improves the situation on all of the other backends, I'm 👍 . Nice work @mdboom! |
The change to the PGF backend is looking fine to me and the alignment test script reproduces the results from AGG 👍. |
…X backends. The renderer backends' draw_text method is now passed the location of the text baseline, not the bottom of the text bounding box.
@jsundram: Would you mind confirming this branch fixes things in the macosx backend as well as in the originally reported Cairo backend? (I've confirmed the latter on Linux, but more confirmation is never a bad thing). |
@mdboom, sure. I'm taking a look at it right now. |
@mdboom, it's possible I did something wrong, but this doesn't work for me. I modified alignment.py (I forked your gist; my version is here: https://gist.github.com/jsundram/5422685) and then:
Cairo still looks wrong. There are two problems: The summations signs (capital sigmas) show up as Xs, which is unrelated. But if you look at the baseline-aligned text, it shows up underneath the baseline. I also tested with https://gist.github.com/jsundram/5096973 to see if the behavior I had originally reported has changed. It hasn't. I did test that script with macosx and agg backends, and they look fine. In conclusion: something is still broken in Cairo on OSX unless I did something wrong. |
It looks like your step 6. may be incorrect above. That will create a new branch by that name and give it the same contents as master. I think you instead just want to do As you note, the Cairo backend has never had working mathtext support, so you can completely ignore the top line in the image for Cairo. |
@mdboom, whoops. I reran alignment.py this time with your changes, and all looks good with macosx and cairo backends. This also fixes my original failing test. Thank you! |
Great! I'm glad it was that simple. Thanks for reporting and your help on this. |
Cairo + plot_date = misaligned x-axis labels
… baseline handling was broken for text.usetex = True. This brings it inline with everything else (where the assumption is that y == 0 is the baseline and not the bottom).
On OSX 10.8.2, using the cairo backend along with plot_date leads to misaligned x-axis labels. No combination I found of setting the verticalalignment of the label (top, bottom, baseline, etc), solved this problem.
Here's a screenshot of the issue (Notice the vertical alignment of April & May).
Here's a gist with minimal code to reproduce the issue. It contains all the info about my matplotlib version, setup, etc. that should be needed.
https://gist.github.com/jsundram/5096973