Skip to content

Clipped tick labels #8609

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

Closed
a113n opened this issue May 11, 2017 · 7 comments · Fixed by #14705
Closed

Clipped tick labels #8609

a113n opened this issue May 11, 2017 · 7 comments · Fixed by #14705

Comments

@a113n
Copy link

a113n commented May 11, 2017

Bug report

Bug summary
Clipped tick labels if emojis were used

Code for reproduction

from __future__ import unicode_literals

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
tick_labels = ['😃', '😎', '😴', '😲', '❤️']
y = [1, 4, 9, 16, 25]
x = range(5)
ax.bar(x, y, tick_label=tick_labels, align='center')
ax.xaxis.set_tick_params(labelsize=20)

ax.set_title('Диаграмма со смайликами')

Actual outcome
bug

Expected outcome
The tick labels are not clipped

Matplotlib version

  • Operating System: Windows 10 1610
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6.1
  • Jupyter Version (if applicable):
  • Other Libraries:
@tacaswell tacaswell added this to the 2.1 (next point release) milestone May 14, 2017
@tacaswell
Copy link
Member

http://matplotlib.org/users/dflt_style_changes.html#fonts renders correctly (which was rendered on the computer I am using right now) as does http://matplotlib.org/devdocs/users/dflt_style_changes.html#fonts which renders on travis (both linux).

How did you install Matplotlib and which version of freetype are you using?

Does it work if you make the font size smaller? Do you have anything about fonts in your rcparams?

My knee-jerk guess is that something is wrong with the font-metrics and we are copy too-small of a buffer. No good guess why this is happening on windows and not linux.

@a113n
Copy link
Author

a113n commented May 15, 2017

Thanks for looking into the issue!

I think if you change one of the symbols in http://matplotlib.org/users/dflt_style_changes.html#fonts to smaller symbols such as '❤️' or any alphanumeric characters, the larger ones would be clipped as well.

This behavior can be observed on both Ubuntu 16.04 and Windows 10. My rcparams are not modified.

@tacaswell tacaswell modified the milestones: 2.1.1 (next bug fix release), 2.1 (next point release) Aug 14, 2017
@tacaswell tacaswell modified the milestones: 2.1.1 (next bug fix release), 2.2 (next feature release) Oct 9, 2017
@drammock
Copy link

FWIW, I get a similar error when I use IPA glyphs in axis labels (ʃ, ʒ, ð etc), but it only happens in interactive plotting (Qt5Agg) and when generating PNGs, but not when saving to PDF, EPS, or SVG.
(mpl 2.0.2, python 3.6.1, linux)

@anntzer
Copy link
Contributor

anntzer commented Jan 25, 2018

I can repro too (default rcparams, 2.1.2 or master, archlinux).
#9763 fixes the issue, so it's not clearly (not "clearly not" :-)) a font metrics thing...

@vincentzlt
Copy link

still got similar problem with chinese characters in 3.0.3

@vincentzlt
Copy link

still got similar problem with chinese characters in 3.1.1

@anntzer
Copy link
Contributor

anntzer commented Jul 6, 2019

#8609 (comment) was the important hint: looks like

diff --git i/lib/matplotlib/backends/backend_agg.py w/lib/matplotlib/backends/backend_agg.py
index c5545ae80..30689f403 100644
--- i/lib/matplotlib/backends/backend_agg.py
+++ w/lib/matplotlib/backends/backend_agg.py
@@ -178,12 +178,9 @@ class RendererAgg(RendererBase):
 
         if font is None:
             return None
-        if len(s) == 1 and ord(s) > 127:
-            font.load_char(ord(s), flags=flags)
-        else:
-            # We pass '0' for angle here, since it will be rotated (in raster
-            # space) in the following call to draw_text_image).
-            font.set_text(s, 0, flags=flags)
+        # We pass '0' for angle here, since it will be rotated (in raster
+        # space) in the following call to draw_text_image).
+        font.set_text(s, 0, flags=flags)
         font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased'])
         d = font.get_descent() / 64.0
         # The descent needs to be adjusted for the angle.

fixes the issue. Indeed, the problem is that load_char does not resize the font's internal buffer, which gets sizes by the smaller "heart" character (which is measured while we prepare for aligning the labels -- we measure all labels before rendering all of them).

The special handling of single-non-ASCII characters was added a long time ago in 29371f9; tbh it's not clear to me why it's necessary (for example it isn't used for longer non-ascii strings...).

Now it would be nice to have a way to test this without a baseline image...

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

Successfully merging a pull request may close this issue.

6 participants