Description
Bug summary
When xtick.labelsize
or ytick.labelsize
is small, using savefig
with backend='pgf'
causes the size to render too big. There seems to be a minimum size that these labels can have, but other types of labels do not have this restriction. This bug also requires setting text.usetex=True
.
Code for reproduction
import matplotlib.pyplot as plt
plt.rcParams.update({
'figure.dpi': 400,
'text.usetex': True,
'ytick.labelsize': 1,
})
# Create the figure and axis objects
fig, ax = plt.subplots(figsize=(2, 1.5))
# Plot the data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
ax.plot(x, y)
# Set the title and axis labels
ax.set_title('My Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# Save the plot to a file
plt.savefig('my_plot_good.png')
plt.savefig('my_plot_bad.png', backend='pgf')
Actual outcome
The my_plot_good.png
file shows the y-axis labels with the correct 1pt font size while the my_plot_bad.png
file shows the same labels with a much bigger font size.
Expected outcome
Both files may look slightly different since two different backends are used, but both files should still show the y-axis label font size as 1pt.
Additional information
There seems to be a discrepancy with how the backend is set. When setting it in rcParam, it produces the incorrect large version, but when setting it as a flag to savefig
, it produces the correct smaller version.
The above example is for easy reproduction, but my actual original use case was to create a small figure for use in LaTeX. When creating a my_plot.pgf
file that is embedded in a LaTeX document, this bug seems to cause the position of the label to be determined based on the large size, but it will actually render using the small size. This is because the pgf file still sets \fontsize
correctly. When creating a png or pdf file, however, both the position and rendering use the incorrect larger size.
Operating system
Ubuntu
Matplotlib Version
3.7.5
Matplotlib Backend
pgf
Python version
3.8.10
Jupyter version
No response
Installation
pip