Skip to content

Saving plot as PNG file prunes tick labels #12564

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
JackCaster opened this issue Oct 19, 2018 · 6 comments
Closed

Saving plot as PNG file prunes tick labels #12564

JackCaster opened this issue Oct 19, 2018 · 6 comments

Comments

@JackCaster
Copy link

JackCaster commented Oct 19, 2018

Bug report

Bug summary
When I export a plot to PNG, some tick labels are pruned. This does not happen if I use another format, such as PDF or SVG.
Code for reproduction
I failed to recreate the problem with a dummy dataset. It seems that the problem is very much an edge case probably. This is what I see on my Jupyter notebook:
image

The plot is exported nicely in PDF: test.pdf

But, if I use PNG, the bottom and upper y-tick labels are pruned:
test

I tried to play around with the padding in plt.tight_layout() and the options in plt.savefig() without success.

Matplotlib version

  • Operating system: Windows 10 Pro N
  • Matplotlib version: 3.0.0 (from Conda)
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: 3.6.6 (from Conda)
  • Jupyter version (if applicable): 1.0.0
@ImportanceOfBeingErnest
Copy link
Member

Feel free to provide a minimal verifiable example, which allows to reproduce the probem. Else, I see little chance to help here.

@jklymak
Copy link
Member

jklymak commented Oct 19, 2018

Try increasing your dpi on the png.

There is some weird dpi-dependent code for adding or dropping ticks. One of my prs fixes it though I’m not sure if it’s been merged yet. EDIT: Not merged: #12158

@JackCaster
Copy link
Author

Try increasing your dpi on the png.

There is some weird dpi-dependent code for adding or dropping ticks. One of my prs fixes it though I’m not sure if it’s been merged yet. EDIT: Not merged: #12158

It is indeed related to the DPI I set. But it is the other way around you suggested. Up to 80 DPI everything is fine. Above that, the tick labels are pruned. Weird. I would try to come up with a reproducible example.

@jklymak
Copy link
Member

jklymak commented Oct 19, 2018

You probably don't need to - its a known issue... From my testing, I think its an issue of your upper ylimit limit being close, but less than, 1.25. When you decrese the dpi, your upper limit becomes within 1/2 a pixel of 1.25 and includes the 1.25; when you increase the dpi your upper limit is no longer within 1/2 a pixel, and it gets trimmed.

#12158 will always trim that tick because your upper y-limit < 1.25. Of course, if you manually specify set_ylim([0, 1.25]) it will not be trimmed. I think that is the correct and consistent behaviour.

@jklymak
Copy link
Member

jklymak commented Oct 19, 2018

A minimal example is:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set_ylim(0, 1.199)

fig.savefig('boo100.png', dpi=100)
fig.savefig('boo200.png', dpi=200)

boo100

boo200

@JackCaster
Copy link
Author

You probably don't need to - its a known issue... From my testing, I think its an issue of your upper ylimit limit being close, but less than, 1.25. When you decrese the dpi, your upper limit becomes within 1/2 a pixel of 1.25 and includes the 1.25; when you increase the dpi your upper limit is no longer within 1/2 a pixel, and it gets trimmed.

#12158 will always trim that tick because your upper y-limit < 1.25. Of course, if you manually specify set_ylim([0, 1.25]) it will not be trimmed. I think that is the correct and consistent behaviour.

Thank you. That worked.

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

3 participants