Skip to content

[Bug]: savefig() behaves strangely with subplots and colorbar when saving as eps #25867

Open
@punyidea

Description

@punyidea

Bug summary

pyplot is adding an extra white rectangle to the image, which overlaps with the colorbar. Found when the image has multiple subplots, and only when .eps is used as the format and we use usetex.

Code for reproduction

import matplotlib.pyplot as plt
import os
import numpy as np

import matplotlib
def save_plot_out(f:plt.Figure,fig_name,fmt=".eps"):

    fig_full_name = fig_name+fmt
    f.savefig(fig_full_name)

def setup_latex():
    '''
    from https://web.archive.org/web/20230124103712/https://stackoverflow.com/questions/36671901/matplotlib-savefig-with-usetex-true
    '''

    # LaTeX setup
    # plt.rcParams['text.latex.preamble'] = r'\usepackage{mathptmx}'  # load times roman font
    # plt.rcParams['font.family'] = 'serif'  # use serif font as default
    plt.rcParams['text.usetex'] = True  # enable LaTeX rendering globally

# modified from stackoverflow: https://stackoverflow.com/a/39938019
def make_fig():
    # from mpl_toolkits.axes_grid1 import make_axes_locatable
    def make_subplot(ax):
        #divider = make_axes_locatable(ax)
        #cax = divider.append_axes('right', size='5%', pad=0.05)

        im = ax.imshow(data, cmap='bone')
        plt.colorbar(im)
        #fig.colorbar(im, cax=cax, orientation='vertical')

    data = np.arange(100, 0, -1).reshape(10, 10)

    fig, axs = plt.subplots(1,2)
    make_subplot(axs[0])
    data = 101-data
    make_subplot(axs[1])

    return fig

setup_latex()
fig_made = make_fig()
save_plot_out(fig_made,'good_fig',fmt='.png')
save_plot_out(fig_made,'bad_fig',fmt='.eps')

Actual outcome

The figure when saved to png

good_fig

The figure when saved to eps (converted to pdf for upload)

bad_fig.pdf

Expected outcome

The colorbars in both figures are identical and appear entirely.

Additional information

No response

Operating system

Ubuntu

Matplotlib Version

3.5.2 and 3.7.1

Matplotlib Backend

Tkagg

Python version

3.8.10

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions