Skip to content

[Bug]: setting norm for imshow and not using a colorbar produces error #28555

Open
@rcomer

Description

@rcomer

Bug summary

I tried removing the colorbar call from @timhoffm's new example at #28546, and I got an error

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

from matplotlib import colors

np.random.seed(19680801)

data = np.random.rand(10, 20)

fig, ax = plt.subplots()

norm = colors.Normalize(vmin=np.max(data), vmax=np.min(data))
im = ax.imshow(data, norm=norm)

plt.show()

Actual outcome

With QtAgg:

Traceback (most recent call last):
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/backends/backend_qt.py", line 498, in _draw_idle
    self.draw()
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/backends/backend_agg.py", line 387, in draw
    self.figure.draw(self.renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/figure.py", line 3224, in draw
    mimage._draw_list_compositing_images(
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/axes/_base.py", line 3152, in draw
    mimage._draw_list_compositing_images(
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 651, in draw
    im, l, b, trans = self.make_image(
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 950, in make_image
    return self._make_image(self._A, bbox, transformed_bbox, clip,
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 551, in _make_image
    output = self.norm(resampled_masked)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/colors.py", line 1395, in __call__
    raise ValueError("minvalue must be less than or equal to maxvalue")
ValueError: minvalue must be less than or equal to maxvalue

With TkAgg:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/home/ruth/miniforge3/envs/mpl-dev/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/home/ruth/miniforge3/envs/mpl-dev/lib/python3.9/tkinter/__init__.py", line 814, in callit
    func(*args)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/backends/_backend_tk.py", line 271, in idle_draw
    self.draw()
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/backends/backend_tkagg.py", line 10, in draw
    super().draw()
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/backends/backend_agg.py", line 387, in draw
    self.figure.draw(self.renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/figure.py", line 3224, in draw
    mimage._draw_list_compositing_images(
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/axes/_base.py", line 3152, in draw
    mimage._draw_list_compositing_images(
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 651, in draw
    im, l, b, trans = self.make_image(
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 950, in make_image
    return self._make_image(self._A, bbox, transformed_bbox, clip,
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/image.py", line 551, in _make_image
    output = self.norm(resampled_masked)
  File "/home/ruth/git_stuff/matplotlib/lib/matplotlib/colors.py", line 1395, in __call__
    raise ValueError("minvalue must be less than or equal to maxvalue")
ValueError: minvalue must be less than or equal to maxvalue

Expected outcome

No error

Additional information

Everything is fine if I add a colorbar.

Operating system

Ubuntu

Matplotlib Version

main, 3.8.4, 3.6.3

Matplotlib Backend

QtAgg and TkAgg

Python version

3.9.19, 3.12.3, 3.11.6

Jupyter version

N/A

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions