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

Skip to content

Alignment of labels incorrect if fontsize<1pt PDF backend #9963

Closed
@ImportanceOfBeingErnest

Description

@ImportanceOfBeingErnest

Label padding around the axes depends on the length of the label when figure is saved as pdf.

The below code creates a figure and adds labels of different length. Those labels are not well aligned in the saved pdf version. Instead their padding towards the axes is depending on how many characters the label has. This issue is the more pronounced the larger the dpi (at constant figsize*dpi product).

The png files saved do not show this unexpected behaviour; all labels are aligned correctly. The expected output when saving the pdf file is of course the same as the png.

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

a = np.random.rand(5,5)

for i in [1,1.25,1.5]:

    plt.rcParams["axes.linewidth"] = i*0.1
    fig = plt.figure(figsize=(i,i), dpi=int(500./i))
    fig.subplots_adjust(0.44,0.44,0.56,0.56)
    ax = fig.add_subplot(111)
    ax.imshow(a)
    ax.set_title("figsize {}, dpi {}".format((i,i), int(500./i)), size=i)
    
    ax.set_yticklabels(["",1,10,100, "ABCDEFGH", "X"])
    ax.set_xticklabels(["",1,10,100, "long long label", "short"])
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
    
    ax.tick_params(axis='both', which='both', labelsize=i*0.5, length=i*0.5, width=i*0.05)
    ax.tick_params(axis='x',which='both', rotation=90)

    plt.savefig("fig_f{}.pdf".format(i))
    plt.savefig("fig_f{}.png".format(i))
plt.show()

so_savepdflargelabels

[reproduced with python 2.7, matplotlib 2.1, Qt4Agg backend as well as TkAgg backend]

This issue was initially brought up in this Stackoverflow question.

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