Skip to content

Angled text not placed correctly with usetex in EPS #9158

Closed

Description

@DTHaydon

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

Activity

  1. github-actions commented on Apr 18, 2023

    @github-actions

    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!

  2. QuLogic commented on Apr 27, 2023

    @QuLogic
    Member

    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.

  3. changed the title [-]Text not centred when using tex and saving as .eps[/-] [+]Angled text not placed correctly with usetex in EPS[/+] on Apr 27, 2023
  4. self-assigned this
    on Apr 27, 2023
  5. added this to the v3.8.0 milestone on Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions