Skip to content

tight_layout and font hinting cuts text near the edge #11551

Open

Description

@prekageo

Bug report

I plot a figure with default configuration. I use 8 point fonts and tight_layout. In the resulting PDF, the x and y labels are cut.

Code for reproduction

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

matplotlib.rcParams['font.size'] = 8
# matplotlib.rcParams['text.hinting'] = 'none' # uncomment to fix problem

plt.figure(figsize=(.5,.5))
plt.xlabel('qyg')
plt.ylabel('ETZ')
plt.yticks([])
plt.xticks([])
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.tight_layout(pad=0)
plt.savefig('output.pdf')

Actual outcome

bad.pdf

Expected outcome

good.pdf

Matplotlib version

  • Operating system: Ubuntu 16.04.2 LTS
  • Matplotlib version: commit 0f05cf4
  • Matplotlib backend (print(matplotlib.get_backend())): agg
  • Python version: 3.5.2
  • Jupyter version (if applicable):
  • Other libraries:

The font used is the default matplotlib font:

$ md5sum DejaVuSans.ttf
49c0f03ec2fa354df7002bcb6331e106  DejaVuSans.ttf

I believe that the problem is here:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_agg.py#L232
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_pdf.py#L2144

As you can see the PDF backend always uses LOAD_NO_HINTING while the Agg backend uses whatever the user specifies. The only workaround that I've found is enabling the commented out line in the testcase above.

Activity

  1. prekageo commented on Dec 17, 2019

    @prekageo
    Author

    For anyone who might try to resolve this issue, the relevant lines of code have changed. For the current master 303d515, the lines are:

    font.set_text(s, 0.0, flags=ft2font.LOAD_NO_HINTING)

    font.set_text(s, 0, flags=flags)

  2. oscargus commented on Jun 3, 2022

    @oscargus
    Member

    This seems to have been solved. The current output is:
    image

    (The black border is to make it more clear how much padding there is, indeed a bit more than in the "good.pdf" in the OP.)

  3. prekageo commented on Jun 10, 2022

    @prekageo
    Author

    I just tested with the latest main (8001579) and I still see the same problem. Can you share your reproduction script? Maybe the border fixes the original problem?

    And it looks like I do not have access to reopen this issue so tagging @oscargus if he could reopen it. Thanks!

  4. jklymak commented on Jun 13, 2022

    @jklymak
    Member

    I can reproduce the problem with tight_layout. If you used constrained_layout, things seem better?

    fig = plt.figure(figsize=(.5,.5), layout='constrained')
    fig.get_layout_engine().set(h_pad=0, w_pad=0)
    # don't call `plt.tight_layout`

    pdf

    output.pdf

    which makes it a bit mysterious why tight_layout is getting the wrong bbox, as the code should be the same.

  5. oscargus commented on Jun 13, 2022

    @oscargus
    Member

    Can you share your reproduction script? Maybe the border fixes the original problem?

    Sorry, didn't see this until now. I used exactly the same script as here (without anything in the matplotlibrc file IIRC). I was a bit unclear about the border though. That is the background from the PDF viewer (Drawboard PDF, hence the black background.) Seems to work for me with Acrobat reader, Firefox, Chrome and Edge as well.

  6. prekageo commented on Jul 3, 2022

    @prekageo
    Author

    Sorry for the delay.

    @jklymak Using constrained, the problem goes away.

    @oscargus I've opened the attached PDF (https://github.com/matplotlib/matplotlib/files/2155253/bad.pdf) with Acrobat, Firefox and Chrome and it renders in the same way in all 3. The letters are cut near the edge. Can you share your generated PDF? I also opened it with Drawboard online (https://pdf.drawboard.com/documents/5d847711eca34829aabf472a0db7b759) and the text is still cut.

  7. github-actions commented on Jul 5, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  8. added
    status: inactive
    on Jul 5, 2023
  9. george-enf commented on Jul 5, 2023

    @george-enf

    Tested with 3.7.1. The problem is still here.

  10. removed
    status: inactive
    on Jul 7, 2023
  11. github-actions commented on Jul 24, 2024

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  12. added
    status: inactive
    on Jul 24, 2024
  13. prekageo commented on Jul 25, 2024

    @prekageo
    Author

    Tested with latest main (bdbbb2d). The problem is still here. Note that a possible solution would be the following patch:

    diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc
    index d419ed6..da30232 100644
    --- a/lib/matplotlib/mpl-data/matplotlibrc
    +++ b/lib/matplotlib/mpl-data/matplotlibrc
    @@ -290,7 +290,7 @@
     ##                ("native" is a synonym.)
     ## - force_autohint: Use FreeType's auto-hinter.  ("auto" is a synonym.)
     ## - no_hinting: Disable hinting.  ("none" is a synonym.)
    -#text.hinting: force_autohint
    +#text.hinting: default
    
     #text.hinting_factor: 8  # Specifies the amount of softness for hinting in the
                              # horizontal direction.  A value of 1 will hint to full

    It's worth noting that the change from LOAD_DEFAULT to LOAD_FORCE_AUTOHINT happened in commit 3299a39 by @mdboom.

  14. removed
    status: inactive
    on Jul 26, 2024
  15. linked a pull request that will close this issueUpdate FreeType to 2.13.3 #29816on Jul 1, 2025
  16. QuLogic commented on Jul 1, 2025

    @QuLogic
    Member

    #29816 will change hinting to default.

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

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