Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Overlaying imshow with transparancy / alpha yields different results in pdf and png when using savefig #18871

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
TAdeJong opened this issue Nov 2, 2020 · 4 comments
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: color/alpha

Comments

@TAdeJong
Copy link

TAdeJong commented Nov 2, 2020

Bug report

Bug summary

When using multiple calls to plt.imshow() with transparancy/alpha on the top image, the .pdf and .png versions are inconsistent

Code for reproduction

Code for reproduction below, also available as a binder at https://mybinder.org/v2/gist/TAdeJong/8cb84dc74bc6d2c2a1744ef465bf85d8/HEAD .
Effect can be even more noticable when overlaying actual images, but for simplicity this example keeps it a single colors:

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

print(matplotlib.__version__, matplotlib.get_backend())

fig,ax = plt.subplots(ncols=11, figsize=[12,3])
for i in range(11):
    ax[i].imshow(np.full((100,100), 0.1), cmap='gray', vmin=0, vmax=1)
    ax[i].imshow(np.full((100,100), 0.9), cmap='inferno', vmin=0, vmax=1, alpha=0.1*i)
    ax[i].set_title(f"$\\alpha=${0.1*i:.1f}")
plt.tight_layout()
plt.savefig('test.png')
plt.savefig('test.pdf')

Actual outcome

Output files:
image
Colors in output files seem consistent between different OS and viewers.

# Mybinder version info:
3.3.2 module://ipykernel.pylab.backend_inline
# Local version info:
3.3.2 TkAgg

Expected outcome

Produced colors being consistent between different output formats, i.e. a equal color for each of the respective squares between the PNG and PDF output.
OR a clear warning in the documentation (I am not aware of such a warning existing, but I might have missed it).

Matplotlib version

  • Operating system: Ubuntu/mybinder
  • Matplotlib version: 3.3.2, at least down to 3.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline / TkAgg
  • Python version: 3.7.7, 3.7.3, 3.7.8
  • Jupyter version (if applicable):
  • Other libraries:

#Edit: fix mybinder link.

@anntzer
Copy link
Contributor

anntzer commented Nov 2, 2020

Possibly related to #8847?

@TAdeJong
Copy link
Author

TAdeJong commented Nov 2, 2020

Yes, they do seem related! Using the code from #8847 and save as pdf and png yields the same images as shown there:
image
So, comparing, it seems the .png agrees with the software compositing, and the .pdf agrees with the agg style compositing

@TAdeJong
Copy link
Author

TAdeJong commented Nov 2, 2020

For those stumbling upon this problem: I found a workaround, as the .pdf get's the same behaviour as the .png when adding interpolation='none' to at least one of the initial imshow calls, like such:

fig,ax = plt.subplots(ncols=11, figsize=[12,3])
for i in range(11):
    ax[i].imshow(np.full((100,100), 0.1), cmap='gray', vmin=0, vmax=1, interpolation='none')
    ax[i].imshow(np.full((100,100), 0.9), cmap='inferno', vmin=0, vmax=1, alpha=0.1*i)#, interpolation='none')
    ax[i].set_title(f"$\\alpha=${0.1*i:.1f}")
plt.tight_layout()
plt.savefig('test_interp.png')
plt.savefig('test_interp.pdf')

@github-actions
Copy link

github-actions bot commented Sep 4, 2023

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Sep 4, 2023
@github-actions github-actions bot added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label Oct 4, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: color/alpha
Projects
None yet
Development

No branches or pull requests

2 participants