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

Ticklabel alignment issue with Arial #4796

Closed
asteppke opened this issue Jul 26, 2015 · 8 comments
Closed

Ticklabel alignment issue with Arial #4796

asteppke opened this issue Jul 26, 2015 · 8 comments

Comments

@asteppke
Copy link

While changing the styling of my plots I also tried to change the font used for the tick labels. Upon closer inspection you can notice that the alignment is somehow off for the y tick label "1" in this example:

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

rcParams = matplotlib.rcParams

rcParams['font.serif'] = ['Times New Roman']
rcParams['font.sans-serif'] = ['Arial']
rcParams['font.family'] = 'sans-serif'


fig = plt.figure(figsize=(6,3))
ax = fig.add_subplot(1,1,1)

t = np.arange(0.0, 1.0, 0.01)
s = 5*t**2+1+np.sin(2*np.pi*t)

line, = ax.plot(t, s, color='blue', lw=2)

ax.tick_params(axis='y', pad=14)        

plt.plot([0,1],[0,2])
plt.savefig('test.png', dpi=300, bbox_inches='tight')
plt.show()

Which results in the following plot:
test

For some reason the alignment of the tick labels is not consistent, the "1" is clearly closer to the axis lines than the other labels. This does not happen with the default font, so I assume this is due to some interaction between both. Unfortunately Arial or Helvetica is the standard in many publications so I would like to stick to this font.

This happens with matplotlib 1.4.3 on Windows 7 using Python 3.4.1.

@tacaswell
Copy link
Member

It looks like the numbers are correctly right alligned. Does

[t.set_ha('center') for t in ax.yaxis.get_ticklabels()]

fix the alignment? It will probably also move the tick labels which I don't remember off the top of my head how to move (but I am sure you can).

@asteppke
Copy link
Author

The issue is that the numbers are not correctly right aligned. It might be more obvious in the following picture where I zoomed in and added addtionally digits before the labels:
image

It seems that matplotlib is not using the correct bounding box for the character but something like a visual bounding box where the glyph "1" has a right margin of almost zero.

This is how right alignment should look like in other plotting packages:
image

Centering the labels only helps as a workaround in few cases if not more than one digit ever appears in the labels.

@EdwinChan
Copy link

I'm having a similar issue here. The code

import matplotlib

# slightly noticeable
matplotlib.rcParams['font.family'] = 'DejaVu Sans'
# slightly noticeable
matplotlib.rcParams['font.family'] = 'Liberation Serif'
# extremely noticeable
matplotlib.rcParams['font.family'] = 'Ubuntu'

matplotlib.rcParams['font.weight'] = 200
matplotlib.rcParams['font.size'] = 48
from matplotlib import pyplot
pyplot.plot([0, 5], [0, 5])
for i in range(3):
  pyplot.annotate(i+1, (0, 0), (1, i+2), 'data', 'data', ha='right')
pyplot.savefig('test.png')

generates the plot

test

The figure shows that both axis labels and annotations are affected by this problem. It appears that only the Agg backend exhibits this incorrect behavior, not the pgf backend, but I haven't tried other backends yet…

@tacaswell
Copy link
Member

Does the same happen on master? @mdboom did some work on this recently.

On Wed, Jul 29, 2015, 9:12 PM Edwin Chan notifications@github.com wrote:

I'm having a similar issue. The misalignment is extremely noticeable using
the Ubuntu font:

[image: test]
https://cloud.githubusercontent.com/assets/906137/8973703/093e4dae-3636-11e5-999a-4401f58e9d0e.png

It appears the Agg backend suffers from this problem but not the pgf
backend. I haven't tried the other backends though…


Reply to this email directly or view it on GitHub
#4796 (comment)
.

@EdwinChan
Copy link

I've performed two little tests. The first test

import matplotlib
matplotlib.rcParams['font.family'] = 'Ubuntu'
matplotlib.rcParams['font.weight'] = 200
matplotlib.rcParams['font.size'] = 48
from matplotlib import pyplot
pyplot.plot([0, 5], [0, 5])
for i in range(3):
  pyplot.annotate(i+1, (0, 0), (1, i+2), 'data', 'data', ha='right')
pyplot.annotate(matplotlib.__version__, (0, 0), (2, 1), 'data', 'data',
  ha='left')
pyplot.savefig('test.png')

produces

test

The second test

import matplotlib
matplotlib.rcParams['font.family'] = 'Ubuntu'
matplotlib.rcParams['font.weight'] = 200
matplotlib.rcParams['font.size'] = 48
from matplotlib import pyplot
for i in range(10):
  pyplot.plot([0, 5], [0, 5])
  pyplot.annotate('{:.2f}'.format(i/100), (0, 0), (4, 1), 'data', 'data',
    ha='right')
  pyplot.savefig('test{}.png'.format(i))
  pyplot.clf()

produces a chain of images. When the images are flipped through with your favorite image viewer, the 'timer' in the lower right doesn't jitter around like it did in matplotlib 1.4.3.

@mdboom
Copy link
Member

mdboom commented Jul 30, 2015

So, it appears this is fixed in git, then?

@EdwinChan
Copy link

It seems so.

@tacaswell
Copy link
Member

Great, closing as fixed on master.

@asteppke Please ping to have this re-opened if you can reproduce this on the current master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants