Skip to content

[Bug]: Setting alpha with an array is ignored with jupyterlab %inline backend #29595

Closed

Description

@rhiannonlynne

Bug summary

If alpha is set as an array (of equal shape as the image data), the alpha value is ignored.
This is failing for inline plots in Jupyter Lab.

Code for reproduction

import matplotlib
print(matplotlib.__version__)

import numpy as np
import matplotlib.pyplot as plt

img_data = np.random.rand(256, 200)
alpha = np.ones((256, 200))
alpha[:, 0:100] = 0.5
fig, ax = plt.subplots(1, 2, figsize=(10, 8))
ax[0].imshow(img_data, alpha=alpha)
ax[1].imshow(alpha)

Actual outcome

Image

Expected outcome

Image

Additional information

Worked with Matplotlib 3.9.4 with numpy 2.2.1
Fails in matplotlib 3.10.0 (same numpy), running in a Jupyter Lab cell.
Worked as expected when run from the command line, with backend 'macosx', or if %matplotlib osx or %matplotlib ipympl is used to decorate the cell before running the code above.

Operating system

OS/X

Matplotlib Version

3.10.0

Matplotlib Backend

inline

Python version

3.13.1

Jupyter version

4.3.4

Installation

conda

Activity

  1. ianthomas23 commented on Feb 9, 2025

    @ianthomas23
    Member

    Please can you show us the output of conda list.

  2. jklymak commented on Feb 9, 2025

    @jklymak
    Member

    I can reproduce this. Worked fine with 3.8.3, and doesn't work with the inline backend and 3.10.0

    But thankfully also reproducible by just saving as a png outside of Jupyter.

    import matplotlib
    print(matplotlib.__version__)
    import numpy as np
    import matplotlib.pyplot as plt
    
    img_data = np.random.rand(256, 200)
    alpha = np.ones((256, 200))
    alpha[:, 0:100] = 0.5
    fig, ax = plt.subplots(1, 2, figsize=(10, 8))
    ax[0].imshow(img_data, alpha=alpha)
    ax[1].imshow(alpha)
    fig.savefig('Boo.png', dpi=100)
    

    Image

    Also occurs on main...

    This bisects to 3ceba40, and is basically caused by #28382

  3. timhoffm commented on Feb 9, 2025

    @timhoffm
    Member

    @jklymak is #28437 the solution to this?

  4. jklymak commented on Feb 9, 2025

    @jklymak
    Member

    Yes I think so.

  5. timhoffm commented on Feb 10, 2025

    @timhoffm
    Member

    We need to clarify what the intended behavior for array alpha kwarg is on RGBA images. This is the blocker on #28437 (see #28437 (comment)). Unfortunately, the decision which behavior is wanted is mostly irrelevant here. As long as colormaps don't have a transparency, the produced RGBA images will have an alpha channel with constant 1, and then multiplying and replacing is the same. But we need a decision to move the PR forward.

  6. ucabova commented on Apr 3, 2025

    @ucabova

    I also encountered this issue and noticed that the issue only appears for larger resolutions. For square images, I noticed the cutoff to be at 123 pixels.

    import numpy as np
    import matplotlib.pyplot as plt
    
    array1 = np.random.random(size=(123, 123))
    alpha1 = np.ones_like(array1) * 0.2
    alpha1[:, :64] = 0.8
    
    plt.imshow(array1, alpha=alpha1)
    

    Image

    Then increasing the resolution:

    array2 = np.random.random(size=(124, 124))
    alpha2 = np.ones_like(array2) * 0.2
    alpha2[:, :64] = 0.8
    
    plt.imshow(array2, alpha=alpha2)
    

    Image

  7. timhoffm commented on Apr 3, 2025

    @timhoffm
    Member

    Closed by #28437. This should be fixed in 3.10.1.

  8. added this to the v3.10.1 milestone on Apr 4, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions