Skip to content
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

Saving figures as PDF miss aligns rotated labels #8411

Closed
felixekn opened this issue Mar 30, 2017 · 6 comments

Comments

Projects
None yet
4 participants
@felixekn
Copy link

commented Mar 30, 2017

Bug report

Bug summary
In Matplotlib 2.0.0, saving figures as PDFs causes long, rotated xtick labels to become miss aligned. When the classic styling (Matplotlib 1.5.3 styling) is used, the rotated labels correctly align in the saved PDF.

This bug is not observed when saving figures as PNG.

# used for reverting back to 1.5.3 styling
# mpl.style.use('classic')
mpl.rcParams['font.sans-serif'] = 'Arial'

x = range(0,4)
y = [i**2 + 1 for i in x]

xticklabels = ['short', 'medddddiummmmm', 'lllllllllloooooooooonnnnnnnngggggggggg', 'short']

fig, ax = pp.subplots()
ax.plot(x,y)
ax.set_xticks(x)
ax.set_xticklabels(xticklabels, ha = "right", rotation = 45)
pp.tight_layout()
fig.savefig('MPL2LabelRotation.png')

Below is a pieced together figure showing the differences
mpl_text_rotation_comparison

Matplotlib version
Matplotlib 2.0.0, Python 2.7.13 (Anaconda 4.3.1 (x86_64)), Mac OS 10.12.4.
Matplotlib was installed through Anaconda.

@felixekn

This comment has been minimized.

Copy link
Author

commented Mar 30, 2017

To add to my original bug report, figures saved as SVG have correctly rotated labels in Matplotlib 2.0.0.

@QuLogic

This comment has been minimized.

Copy link
Member

commented Mar 31, 2017

The example doesn't run as is, but fixing that, I can't reproduce the problem. However, I don't have Arial available. Does it still fail without changing the font?

@kahnchana

This comment has been minimized.

Copy link
Contributor

commented Apr 1, 2017

I tried replicating the code as well and I did not get that error. Used same font and Matplotlib version installed through anaconda. I didn't seem to get the error.

@felixekn

This comment has been minimized.

Copy link
Author

commented Apr 3, 2017

Looks like the bug is coming from setting rcParams through mpl.rcParams['font.sans-serif'] and saving as a PDF. See the following code for replication of the bug:

from matplotlib import pyplot as pp
import matplotlib as mpl

# ----------------------------------------------------------------------------------- #
# Setting rcParams font will miss aligning rotated labels and increase letter kerning #
# ----------------------------------------------------------------------------------- #

title = ["MPL 2.0.0 - Arial", "MPL 2.0.0 - Default Font"]
font = ['Arial', 'DejaVu Sans']

x = range(0,4)
y = [i**2 + 1 for i in x]
xticklabels = ['short', 'medddddiummmmm', 'lllllllllloooooooooonnnnnnnngggggggggg', 'short']

fig = pp.figure()
for i in range(len(title)):
	ax = pp.subplot(1,2,i+1)
	ax.plot(x,y)
	ax.set_xticks(x)
	ax.set_xticklabels(xticklabels, ha = "right", rotation = 45, fontname = font[i])
	ax.set_title(title[i], fontname = font[i])

pp.tight_layout()
fig.text(0.5, 0.98, "No rcParams font change",  va='center', ha='center')
t = fig.text(0.03, 0.7, "PNG",  va='center', ha='center', rotation='vertical')
fig.savefig('MPL2LabelRotation.png')
fig.texts.remove(t)

t = fig.text(0.03, 0.7, "PDF",  va='center', ha='center',rotation='vertical')
fig.savefig('MPL2LabelRotation.pdf')

# Font set through rcParams
mpl.rcParams['font.sans-serif'] = 'DejaVu Sans'
fig = pp.figure()
for i in range(len(title)):
	ax = pp.subplot(1,2,i+1)
	ax.plot(x,y)
	ax.set_xticks(x)
	ax.set_xticklabels(xticklabels, ha = "right", rotation = 45, fontname = font[i])
	ax.set_title(title[i], fontname = font[i])

pp.tight_layout()
fig.text(0.5, 0.98, "rcParams font change",  va='center', ha='center')
t = fig.text(0.03, 0.7, "PNG",  va='center', ha='center', rotation='vertical')
fig.savefig('MPL2LabelRotation-rcParams.png')
fig.texts.remove(t)
t = fig.text(0.03, 0.7, "PDF",  va='center', ha='center', rotation='vertical')
fig.savefig('MPL2LabelRotation-rcParams.pdf')

MPL2LabelRotation.pdf
mpl2labelrotation
MPL2LabelRotation-rcParams.pdf
mpl2labelrotation-rcparams

@afvincent

This comment has been minimized.

Copy link
Contributor

commented Oct 17, 2017

@felixekn Is this issue still present on Matplotlib 2.1? From my own experiments on my workstation (Python 3.6.3 and Matplotlib 2.1 from conda on Fedora 26), I wonder if it may have been fixed. Running a slightly adapted version of your example

from matplotlib import pyplot as plt
import matplotlib as mpl

# ----------------------------------------------------------------------------------- #
# Setting rcParams font will miss aligning rotated labels and increase letter kerning #
# ----------------------------------------------------------------------------------- #

mplv = mpl.__version__
title = ["MPL {} - Source Sans Pro".format(mplv), "MPL {} - Default Font".format(mplv)]
font = ['Source Sans Pro', 'DejaVu Sans']

x = range(0,4)
y = [i**2 + 1 for i in x]
xticklabels = ['short', 'medddddiummmmm', 'lllllllllloooooooooonnnnnnnngggggggggg', 'short']

fig = plt.figure()
for i in range(len(title)):
	ax = plt.subplot(1,2,i+1)
	ax.plot(x,y)
	ax.set_xticks(x)
	ax.set_xticklabels(xticklabels, ha="right", rotation=45, fontname=font[i])
	ax.set_title(title[i], fontname=font[i])

plt.tight_layout()
fig.text(0.5, 0.98, "No rcParams font change",  va='center', ha='center')
t = fig.text(0.03, 0.7, "PNG",  va='center', ha='center', rotation='vertical')
fig.savefig('MPL2LabelRotation.png')
fig.texts.remove(t)

t = fig.text(0.03, 0.7, "PDF",  va='center', ha='center',rotation='vertical')
fig.savefig('MPL2LabelRotation.pdf')

# Font set through rcParams
mpl.rcParams['font.sans-serif'] = 'DejaVu Sans'
fig = plt.figure()
for i in range(len(title)):
    ax = plt.subplot(1,2,i+1)
    ax.plot(x,y)
    ax.set_xticks(x)
    ax.set_xticklabels(xticklabels, ha="right", rotation=45, fontname=font[i])
    ax.set_title(title[i], fontname=font[i])

plt.tight_layout()
fig.text(0.5, 0.98, "rcParams font change",  va='center', ha='center')
t = fig.text(0.03, 0.7, "PNG",  va='center', ha='center', rotation='vertical')
fig.savefig('MPL2LabelRotation-rcParams.png')
fig.texts.remove(t)
t = fig.text(0.03, 0.7, "PDF",  va='center', ha='center', rotation='vertical')
fig.savefig('MPL2LabelRotation-rcParams.pdf')

produces

Without rcParams font change

MPL2LabelRotation.pdf
mpl2labelrotation

With rcParams font change

MPL2LabelRotation-rcParams.pdf
mpl2labelrotation-rcparams

where things look fine to me (at least when comparing PNG and PDF outputs).

@felixekn

This comment has been minimized.

Copy link
Author

commented Oct 17, 2017

@afvincent as of 2.0.2 this issue was resolved for me.

@felixekn felixekn closed this Oct 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.