Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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
DTHaydon opened this issue Sep 9, 2016 · 7 comments
Closed

Comments

@DTHaydon
Copy link

DTHaydon commented Sep 9, 2016

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)

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Sep 9, 2016
@tohojo
Copy link

tohojo commented Feb 1, 2017

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

@tohojo
Copy link

tohojo commented Feb 21, 2017

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

@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@tacaswell tacaswell modified the milestones: 2.1.1 (next bug fix release), 2.2 (next feature release) Oct 9, 2017
@Warbo
Copy link

Warbo commented Jul 11, 2018

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.

Warbo added a commit to Warbo/writing that referenced this issue Jul 11, 2018
It causes labels to be chopped off e.g. see
matplotlib/matplotlib#7075
@pyZerrenner
Copy link

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

@jklymak jklymak modified the milestones: needs sorting, v3.2.0, v3.1.1 Apr 30, 2019
@jklymak
Copy link
Member

jklymak commented Apr 30, 2019

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.

@jklymak
Copy link
Member

jklymak commented Apr 30, 2019

... same thing happens for a title.

@jklymak 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" Apr 30, 2019
@tacaswell tacaswell modified the milestones: v3.1.1, v3.2.0 Jun 10, 2019
@timhoffm timhoffm modified the milestones: v3.2.0, v3.3.0 Aug 15, 2019
@QuLogic QuLogic modified the milestones: v3.3.0, v3.4.0 May 27, 2020
@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 27, 2021
@QuLogic QuLogic removed this from the v3.5.0 milestone Sep 25, 2021
@jklymak
Copy link
Member

jklymak commented Nov 16, 2021

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)

@jklymak jklymak closed this as completed Nov 16, 2021
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

9 participants