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

Angled text not placed correctly with usetex in EPS #9158

Closed
DTHaydon opened this issue Sep 5, 2017 · 3 comments · Fixed by #25784
Closed

Angled text not placed correctly with usetex in EPS #9158

DTHaydon opened this issue Sep 5, 2017 · 3 comments · Fixed by #25784

Comments

@DTHaydon
Copy link

DTHaydon commented Sep 5, 2017

Bug report

Bug summary
The text alignment of .text(..., ha="center", va="center", ...) whilst using matplotlib.rc("text", usetex=True) and saving as .eps is not centred.
However the output is centred for .pdf and .png

Code for reproduction

import numpy

from matplotlib import pyplot
from matplotlib import gridspec

import matplotlib
matplotlib.rc("text", usetex=True)

def plot(file_path):
    gs = gridspec.GridSpec(
            3, 1,
            wspace=0.0, hspace=0.0
            )
    fig = pyplot.figure()

    # Add plots
    for sub_plot in gs:
        ax = fig.add_subplot(sub_plot)

    # position text based on location of subplots
    fig.tight_layout()
    row_bottom, row_top, col_left, col_right = gs.get_grid_positions(fig)

    # Finding the vertical centre like this because the axis label for
    # the plots I create  should be shared with only some of the subplots
    vertical_centre = numpy.mean([row_bottom[-1], row_top[0]])
    fig.text(
            0.01, vertical_centre,
            r"$\mathrm{Response \, \left(Normalised\right)}$",
            ha="center", va="center",
            rotation="vertical",
            transform=fig.transFigure,
            fontsize=35.  # lage size to emphasize error
            )

    fig.savefig(file_path, bbox_inches="tight")
    pyplot.close(fig)

plot("./test_eps.eps")
plot("./test_pdf.pdf")
plot("./test_png.png")

Actual outcome
./test_eps.eps
test_eps

./test_pdf.pdf
test_pdf

./test_png.png
test_png

Expected outcome
Ideally the output of ./test_eps.eps should match the other two figures.

System

  • Operating System: macOS 10.12.6
  • Matplotlib Version: 2.0.2 (Conda: build string: np112py27_0, channel: defaults)
  • Python Version: 2.7.13
  • Conda Version: 4.3.24
@ghost
Copy link

ghost commented Sep 6, 2017

Hi DTHaydon,

I'm not a developer but a user (without much knowledge of the underlying programming). Perhaps I had a similar problem recently:
#8067
Perhaps the answer will help you.

Regards

@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Apr 18, 2023
@QuLogic
Copy link
Member

QuLogic commented Apr 27, 2023

This is still a problem. But I think the problem has nothing to do with alignment directly, but with the angle:

import matplotlib.pyplot as plt

plt.rc("text", usetex=True)

fig = plt.figure()

for i, angle in enumerate([0, 90, 180, 270]):
    fig.text(0.1 + i / 5, 0.1 + i / 5, f"$\\mathrm{{Response {angle} }}$",
             rotation=angle, fontsize=24)

for fmt in ['eps', 'pdf', 'png']:
    fig.savefig(f"test_{fmt}.{fmt}")

produces inconsistent results for everything but 0 angle.

Results

PDF:
image

EPS:
image

We currently place usetex using the bottom-left corner, but perhaps a different anchor should be used:

# Stick to the bottom alignment.
pos = _nums_to_str(x, y-bl)
self.psfrag.append(
r'\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}' % (
thetext, angle, fontsize, fontsize*1.25, tex))

At least, always subtracting text depth from the y position seems wrong, if the text is vertical.

@QuLogic QuLogic changed the title Text not centred when using tex and saving as .eps Angled text not placed correctly with usetex in EPS Apr 27, 2023
@QuLogic QuLogic removed the status: inactive Marked by the “Stale” Github Action label Apr 27, 2023
@QuLogic QuLogic self-assigned this Apr 27, 2023
@QuLogic QuLogic added this to the v3.8.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants