Skip to content

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

Closed

Description

@TAdeJong

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.

Activity

  1. anntzer commented on Nov 2, 2020

    @anntzer
    Contributor

    Possibly related to #8847?

  2. TAdeJong commented on Nov 2, 2020

    @TAdeJong
    Author

    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

  3. TAdeJong commented on Nov 2, 2020

    @TAdeJong
    Author

    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')
  4. github-actions commented on Sep 4, 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!

  5. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Oct 4, 2023
  6. codeceptsDE commented on Aug 17, 2026

    @codeceptsDE

    wow, thanks @TAdeJong for the workaround!
    The issue still persists, but it seems like the referenced #8847 just recently received a fix PR #32107.
    Lets see what effect this will have on this issue, and also on #25806!

  7. ayshih commented on Aug 18, 2026

    @ayshih
    Contributor

    I've confirmed that this issue has also been fixed by #32107

  8. added this to the v3.12.0 milestone on Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Actiontopic: color/alpha

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions