10000 Coordinates of text not properly set in pgf files · Issue #8067 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Coordinates of text not properly set in pgf files #8067

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

Closed
ghost opened this issue Feb 12, 2017 · 4 comments
Closed

Coordinates of text not properly set in pgf files #8067

ghost opened this issue Feb 12, 2017 · 4 comments
Milestone

Comments

@ghost
Copy link
ghost commented Feb 12, 2017

Bug summary
The coordinates of text (legends, titles) are not properly set in pgf files.

Code for reproduction
Producing the pgf figure with a slightly altered pylab_examples "tex_demo.py":

import numpy as np
import matplotlib.pyplot as plt

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.figure(1, figsize=(6, 4))
ax = plt.axes([0.1, 0.1, 0.8, 0.7])
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
plt.plot(t, s)
plt.xlabel(r'\textbf{time (s)}')
plt.ylabel(r'\textit{voltage (mV)}', fontsize=16)
plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty"
          r"\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='r')
plt.grid(True)
plt.savefig('tex_demo.pgf')    # altered line
plt.show()

Taking this pgf file ("tex_demo.pgf") in my latex file:

\documentclass{article}
\usepackage{pgf}
\begin{document}
\input{tex_demo.pgf}
\end{document}

Actual outcome
This gives the output "Bugreport.pdf".
Bugreport.pdf

Expected outcome
The graphic should look like in the example:
http://matplotlib.org/examples/pylab_examples/tex_demo.html

Matplotlib version

  • Matplotlib 1.5.2rc2, Python 3.5.2 and Fedora 25 (Workstation Edition)
  • All components were installed via the Fedora specific system tools (dnf) from Fedora repositories
@tacaswell
Copy link
Member

attn @pwuertz

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Feb 12, 2017
@pwuertz
Copy link
Contributor
pwuertz commented Feb 21, 2017

I assume what is happening here is that matplotlib uses a fixed position for titles and legends and aligns them by their baseline. As far as I can remember the traditional matplotlib backends never took baseline offset of LaTeX elements into account. This means that text rendered via traditional LaTeX backend is simply shifted upwards so the bottom of the text box sits on the baseline, which is actually incorrect but wouldn't result in this overlap you are seeing now (hence nobody identifies this as incorrect behaviour).

I'm pretty sure the font metrics returned from the PGF backend are correct, especially the alignment of the baselines. So with backend PGF matplotlib baseline-alignes the text element at the fixed title position, the large formula goes way below the baseline and overlaps with the figure. Matplotlib won't automatically adapt the figure layout for you, even if text elements are larger/overlapping.

Perhaps tight_layout is able to clean up such situations, but its magic is limited. You could also try to align such large texts by bottom instead of base alignment, which should get rid of the overlap.

@ghost
Copy link
Author
ghost commented Feb 23, 2017

Thanks for the response. Using the modification

plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty"
r"\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='r', verticalalignment='bottom')

helps with this issue.

@ghost ghost closed this as completed Feb 23, 2017
@ghost ghost reopened this Feb 23, 2017
@ghost
Copy link
Author
ghost commented Feb 23, 2017

Sorry, I accidently closed the issue without your consent. If you consider it closed, close it again.

Regards Jakob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
0