GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Superscripts in axis label cut when saving .eps with bbox_inches="tight" · Issue #7075 · matplotlib/matplotlib

Superscripts in axis label cut when saving .eps with bbox_inches="tight" #7075

New issue
Copy link
New issue
Copy link
Closed
Closed
Superscripts in axis label cut when saving .eps with bbox_inches="tight"#7075
Copy link
Labels
topic: texttopic: text/usetex
Milestone
v3.6.0

Description

@DTHaydon
DTHaydon
opened on Sep 9, 2016
Issue body actions

I have been saving some .eps plots using:
matplotlib.pyplot.savefig(file.eps, bbox_inches="tight")
and this works as expected; however, when using:
matplotlib.rcParams['text.usetex'] = True
matplotlib.pyplot.savefig(file.eps, bbox_inches="tight")
the y axis label is cut.

It has been noted before that there has been some issues with .eps fies and usetex (Bounding box no longer works for EPS files if usetex=True #85) but I don't know in what way the bounding box was wrong and I am also using a matplotlib version that is 5 years on.

Here is a somewhat forced example and the results.

Example

import numpy
from matplotlib import pyplot
from matplotlib import gridspec
from matplotlib.collections import LineCollection

def plotLines(file_name):

    # -- Misc ----------------------------------------------------------------
    axis_font_size = 50.
    line_thickness = 3.

    # -- Data ----------------------------------------------------------------
    angles = numpy.linspace(-180., 180, num=201)
    amp_values = [1., 2., 3.] 

    line_values = []     
    for amp in reversed(amp_values):
        sine = amp * (numpy.sin(angles * (numpy.pi/180.)))**-1
        line_values.append(zip(angles, sine))

    amp_values = numpy.array(amp_values)
    lines = LineCollection(
        line_values, 
        array=amp_values, 
        cmap=pyplot.cm.viridis,
        linewidth=line_thickness
        )

    # -- Init Plot -----------------------------------------------------------
    pyplot.clf()  # Clear plot area
    fig = pyplot.figure()
    ax_grid = gridspec.GridSpec(
        1, 2, wspace=0.05, hspace=0.00,
        width_ratios=[20, 1]
        )

    # -- Plot ----------------------------------------------------------------
    plot = pyplot.subplot(ax_grid[0])

    plot.add_collection(lines)

    # Axis limits
    plot.set_xlim([numpy.min(angles), numpy.max(angles)])
    plot.set_ylim([-numpy.max(amp_values), numpy.max(amp_values)])

    # Axis labels
    plot.set_xlabel(
        "$\\mathrm{Angle} \\, \\left[^{\\circ}\\right]$", 
        fontsize=axis_font_size
        )
    plot.set_ylabel(
        "$\\mathrm{Amp} \\,"
        "\\times \\, \\left[\\mathrm{sine} \\,"
        "\\left(\\mathrm{Angle}\\right)\\right]^{-1}$",
        fontsize=axis_font_size,
        )

    # -- Colour Bar ----------------------------------------------------------
    cbar_axis = pyplot.subplot(ax_grid[1])

    cbar = pyplot.colorbar(lines, ticks=amp_values, cax=cbar_axis)

    # CBar Label
    cbar.ax.set_ylabel(
        "$\\mathrm{Amp}$",
        fontsize=axis_font_size,
        )

    # -- Save ----------------------------------------------------------------
    pyplot.savefig(
        "{:s}.eps".format(file_name),
        bbox_inches="tight",
        dpi=600
        )
    pyplot.close()


if __name__ == "__main__":
    plotLines("Without")
    import matplotlib as mpl
    mpl.rcParams['text.usetex'] = True  # Process all text with TeX
    plotLines("With")

Results

Without

As expected
without

With

Y axis label cut
with

System

Matplotlib 1.5.1
Python 2.7.12 (Anaconda install)
CentOS Linux release 7.2.1511
GPL Ghostscript 9.07 (2013-02-14)

Activity

Next
  1. tacaswell
    added this to the This milestone has been deleted milestone on Sep 9, 2016
  2. tacaswell
    added
    topic: text
    on Sep 9, 2016
  3. tohojo commented on Feb 1, 2017

    @tohojo
    tohojo
    on Feb 1, 2017
    More actions

    I've been having some issues with text bounding boxes being wrong for axis labels on 2.0 (when saving PDFs in my case). Will investigate if this is the same bug...

  4. tohojo commented on Feb 21, 2017

    @tohojo
    tohojo
    on Feb 21, 2017
    More actions

    And to follow up on this, my issue is unrelated to usetex, so I don't think it's the same issue...

    Reacted by Chris Lovell
  5. QuLogic
    modified the milestones: This milestone has been deleted, This milestone has been deleted on May 3, 2017
  6. tacaswell
    modified the milestones: This milestone has been deleted, This milestone has been deleted on Oct 9, 2017
  7. jkseppan
    added
    topic: text/usetex
    on Jan 19, 2018
  8. Warbo commented on Jul 11, 2018

    @Warbo
    Warbo
    on Jul 11, 2018
    More actions

    I was having trouble with y-axis labels being cut off: it was fine for pgf output, but broke when using EPS. Googling brought me here, so I tried removing 'usetex = True' and it solved the problem.

  9. Warbo
    added a commit that references this issue on Jul 11, 2018

    Don't render chart text with TeX

    e5ab840
  10. pyZerrenner commented on Apr 30, 2019

    @pyZerrenner
    pyZerrenner
    on Apr 30, 2019
    More actions

    For me this bug is DPI dependent. With the default DPI of 100 the following code gives a good result:

    import matplotlib.pyplot as plt
    
    plt.rcParams['figure.dpi'] = 100
    
    fig, ax = plt.subplots(1, 1, figsize=(2,1))
    ax.set_ylabel(r'$X^{SUPERSCRIPT}$')
    
    fig.savefig('test_superscript.png', bbox_inches='tight', pad_inches=0)

    test_superscript_DPI100

    However, when I increase the DPI, the superscript of the y-label is cut off. With plt.rcParams['figure.dpi'] = 300 the result is:

    test_superscript_DPI300

    The same happens for saving as pdf or when setting 'savefig.dpi' instead of 'figure.dpi'. My current solution is to increase the value of pad_inches until everything is visible. This increases the padding on all sides, which is not too bad, but still annoying.

    When using TeX for text rendering, the issue appears for all DPI, like in the comments above. With plt.rcParams['figure.dpi'] = 100 and plt.rcParams['text.usetex'] = True I get:
    test_superscript
    Maybe the DPI and TeX issues are somewhat unrelated, but I didn't want to open a new issue unnecessarily.

    I am using matplotlib 2.2.4 on Python 3.6 myself. Unfortunately, I cannot verify this with the latest version (matplotlib 3.0.3) right now, so I hope this has not been resolved yet. (Or rather I do hope it has been resolved, then this issue could be closed.)

  11. jklymak
    removed this from the needs sorting milestone on Apr 30, 2019
  12. 3 remaining items

  13. jklymak commented on Apr 30, 2019

    @jklymak
    jklymak
    on Apr 30, 2019
    Member
    More actions

    This is still a bug on master... Its obviously the superscript that is causing the problem, with the text bounding box not surrounding the superscript.

    Reacted by Chris Lovell
  14. jklymak commented on Apr 30, 2019

    @jklymak
    jklymak
    on Apr 30, 2019
    Member
    More actions

    ... same thing happens for a title.

  15. jklymak
    changed the title [-]Axis label cut when saving .eps with bbox_inches="tight" but only when usetex=True[/-] [+]Superscripts in axis label cut when saving .eps with bbox_inches="tight"[/+] on Apr 30, 2019
  16. tacaswell
    modified the milestones: v3.1.1, v3.2.0 on Jun 10, 2019
  17. timhoffm
    modified the milestones: v3.2.0, v3.3.0 on Aug 15, 2019
  18. QuLogic
    modified the milestones: v3.3.0, v3.4.0 on May 27, 2020
  19. QuLogic
    modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  20. QuLogic
    modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  21. jklymak commented on Nov 16, 2021

    @jklymak
    jklymak
    on Nov 16, 2021
    Member
    More actions

    I'll close this in favour of #21653, which is the same thing, but perhaps easier to follow (bug doesn't depend on clipping or eps, etc)

  22. jklymak
    closed this as completedon Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: texttopic: text/usetex

    Type

    No type

    Projects

    No projects

      Milestone

      • v3.6.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.