Skip to content

[Bug]: Doesn't show 1-channel image by using matplotlib.image.AxesImage.set_data() #22808

Open
@nicolasrosa

Description

@nicolasrosa

Bug summary

I could not plot a 1-channel image while I was trying to populate a Subplots figure, although I can visualize it by using plt.imshow().

Code for reproduction

fig, axes = plt.subplots(3, 2, num=0)
# fig.canvas.mpl_connect('key_press_event', on_press)

h, w = image_size
h, w = int(h), int(w)

ax1 = axes[0][0].imshow(np.zeros((h, w, 3), dtype=np.float32))
ax2 = axes[0][1].imshow(np.zeros((h, w, 3), dtype=np.float32))
ax3 = axes[1][0].imshow(np.zeros((h, w, 3), dtype=np.float32))
ax4 = axes[1][1].imshow(np.zeros((h, w, 3), dtype=np.float32))
ax5 = axes[2][0].imshow(np.zeros((h, w), dtype=np.float32)) 
ax6 = axes[2][1].imshow(np.zeros((h, w), dtype=np.float32))

image_np = <(h,w) uint8 or float32>
image_pil = Image.fromarray(image_np).convert('RGB')

...
# ax5.set_data(image_np)  # Fails!
# ax5.set_data(image_pil)  # Works!
plt.imshow(image_np)  # Works!
plt.draw()
plt.pause(1e-9)

Actual outcome

matplotlib_bug

Expected outcome

I used the Paint to generate the following image to demonstrate what I was expecting.

matplotlib_fix_paint

Additional information

No response

Operating system

Ubuntu 20.04

Matplotlib Version

3.5.1

Matplotlib Backend

TkAgg

Python version

Python 3.8.10

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions