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

Some short lines added with plot() are missing in the PNG output depending on DPI #20192

Open
tskir opened this issue May 9, 2021 · 4 comments

Comments

@tskir
Copy link

tskir commented May 9, 2021

Bug report

Depending on DPI, a figure saved as PNG is missing some short lines.

Code for reproduction

import matplotlib
from matplotlib import pyplot as plt
import numpy as np

matplotlib.use('agg')

# Generate a set of 50 random points.
np.random.seed(0)
all_x, all_y = (np.random.rand(50) for _ in range(2))

# Scatter dots in blue.
plt.scatter(all_x, all_y, c='blue', s=2)

# Plot very short lines (length 0.001) in red with a small offset on Y.
for x, y in zip(all_x, all_y):
    plt.plot((x, x + 0.001), (y + 0.01, y + 0.01), c='red', linewidth=2)

# Save with different resolutions
plt.savefig('bug_96.png', dpi=96)
plt.savefig('bug_300.png', dpi=300)

Actual outcome

Depending on export DPI, some short segments (in red, added by plot()) are missing, while all dots (in blue, added by scatter() are always present).

At 96 DPI:
image

At 300 DPI:
image

Expected outcome

The segments should be always present, regardless of the resolution.

Matplotlib version

  • Operating system: Ubuntu 20.04.2 LTS
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2
  • Matplotlib backend (print(matplotlib.get_backend())): agg
  • Python version: 3.8.5

Python is installed via apt-get, Matplotlib installed system-wide using PIP.

@tskir
Copy link
Author

tskir commented May 9, 2021

Interestingly enough, the bug disappears with matplotlib.use('cairo').

At 96 DPI with cairo:
image

At 300 DPI with cairo:
image

@jklymak
Copy link
Member

jklymak commented May 9, 2021

If you turn snapping off this is fixed. plot(....snap=False)

@jklymak
Copy link
Member

jklymak commented May 9, 2021

FWIW, I think snapping for ticks makes some sense. I don't think there are many situations where having snapping on for other artists is a good idea at all. I'm not familiar enough with matplotlib's history wrt snapping, but it seems a bad idea in general.

I also recommend strongly getting around any bad ideas matplotlib may have had by saving at relatively high dpi and then using a graphics package to reduce the size optically. In my opinion we try to do too many "smart" things with low resolution images that really should be done in post-processing, not while we compose the images.

@tskir
Copy link
Author

tskir commented May 9, 2021

@jklymak Indeed, thank you very much for the pointer and for such a quick reply!

I'll apply this fix, and will leave this issue up for any related discussions.

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

No branches or pull requests

2 participants