Skip to content
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

Cairo + plot_date = misaligned x-axis labels #1810

Merged
merged 5 commits into from Apr 19, 2013

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented Apr 15, 2013

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).

bug

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

@jakevdp
Copy link
Contributor

jakevdp commented Mar 20, 2013

I can reproduce on MacOSX backend. No idea what would be causing this

@ivanov
Copy link
Member

ivanov commented Mar 20, 2013

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

@ivanov
Copy link
Member

ivanov commented Mar 20, 2013

partial improvement: [l.set_va('center') for l in ax.get_xticklabels()]

@ivanov
Copy link
Member

ivanov commented Mar 22, 2013

pinging @mdboom and @efiring who did some of the work in axis.py related to ticklabeling and may have deeper understanding of this problem

@mdboom
Copy link
Member

mdboom commented Apr 12, 2013

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.

@mdboom
Copy link
Member

mdboom commented Apr 12, 2013

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?

@jsundram
Copy link
Author

@mdboom, I'll be happy to test.

mdboom added a commit to mdboom/matplotlib that referenced this pull request Apr 15, 2013
…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.
@mdboom
Copy link
Member

mdboom commented Apr 15, 2013

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))
Copy link
Member

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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Forgot about that...

@pelson
Copy link
Member

pelson commented Apr 16, 2013

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!

@pwuertz
Copy link
Contributor

pwuertz commented Apr 16, 2013

The change to the PGF backend is looking fine to me and the alignment test script reproduces the results from AGG 👍.

@mdboom
Copy link
Member

mdboom commented Apr 19, 2013

@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).

@jsundram
Copy link
Author

@mdboom, sure. I'm taking a look at it right now.

@jsundram
Copy link
Author

@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:

  1. Ran alignment.py on my existing matplotlib with agg, macosx, and cairo, and stored the output in a folder called "before".
  2. Manually delete matplotlib from site-packages (found the directory via python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib()))
  3. Confirm that matplotlib doesn't exist on my system by attempting to rerun alignment.py. I get the expected ImportError: No module named matplotlib
  4. git clone https://github.com/mdboom/matplotlib.git 1810/
  5. cd 1810
  6. git checkout -b text-baseline-api
  7. python setup.py install
  8. rerun alignment.py with agg, macosx, cairo as backend arguments, store the output in a folder called "after".

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.

cairo

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.

@mdboom
Copy link
Member

mdboom commented Apr 19, 2013

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 git checkout text-baseline-api.

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.

@jsundram
Copy link
Author

@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!

cairo

@mdboom
Copy link
Member

mdboom commented Apr 19, 2013

Great! I'm glad it was that simple.

Thanks for reporting and your help on this.

mdboom added a commit that referenced this pull request Apr 19, 2013
Cairo + plot_date = misaligned x-axis labels
@mdboom mdboom merged commit eab2a9e into matplotlib:master Apr 19, 2013
mdboom added a commit to mdboom/matplotlib that referenced this pull request May 15, 2013
… 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).
@mdboom mdboom deleted the text-baseline-api branch August 7, 2014 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants