| 1 | [Bug]: Setting norm with existing colorbar fails with 3.6.3 |
| 2 | ### Bug summary |
| 3 | |
| 4 | Setting the norm to a `LogNorm` after the colorbar has been created (e.g. in interactive code) fails with an `Invalid vmin` value in matplotlib 3.6.3. |
| 5 | |
| 6 | The same code worked in previous matplotlib versions. |
| 7 | |
| 8 | Not that vmin and vmax are explicitly set to values valid for `LogNorm` and no negative values (or values == 0) exist in the input data. |
| 9 | |
| 10 | ### Code for reproduction |
| 11 | |
| 12 | ```python |
| 13 | import matplotlib.pyplot as plt |
| 14 | from matplotlib.colors import LogNorm |
| 15 | import numpy as np |
| 16 | |
| 17 | # create some random data to fill a 2d plot |
| 18 | rng = np.random.default_rng(0) |
| 19 | img = rng.uniform(1, 5, (25, 25)) |
| 20 | |
| 21 | # plot it |
| 22 | fig, ax = plt.subplots(layout="constrained") |
| 23 | plot = ax.pcolormesh(img) |
| 24 | cbar = fig.colorbar(plot, ax=ax) |
| 25 | |
| 26 | vmin = 1 |
| 27 | vmax = 5 |
| 28 | |
| 29 | plt.ion() |
| 30 | fig.show() |
| 31 | plt.pause(0.5) |
| 32 | |
| 33 | plot.norm = LogNorm(vmin, vmax) |
| 34 | plot.autoscale() |
| 35 | plt.pause(0.5) |
| 36 | ``` |
| 37 | |
| 38 | |
| 39 | ### Actual outcome |
| 40 | |
| 41 | ``` |
| 42 | Traceback (most recent call last): |
| 43 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/backends/backend_qt.py", line 454, in _draw_idle |
| 44 | self.draw() |
| 45 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 405, in draw |
| 46 | self.figure.draw(self.renderer) |
| 47 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/artist.py", line 74, in draw_wrapper |
| 48 | result = draw(artist, renderer, *args, **kwargs) |
| 49 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper |
| 50 | return draw(artist, renderer) |
| 51 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/figure.py", line 3082, in draw |
| 52 | mimage._draw_list_compositing_images( |
| 53 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images |
| 54 | a.draw(renderer) |
| 55 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper |
| 56 | return draw(artist, renderer) |
| 57 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 3100, in draw |
| 58 | mimage._draw_list_compositing_images( |
| 59 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images |
| 60 | a.draw(renderer) |
| 61 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper |
| 62 | return draw(artist, renderer) |
| 63 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/collections.py", line 2148, in draw |
| 64 | self.update_scalarmappable() |
| 65 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/collections.py", line 891, in update_scalarmappable |
| 66 | self._mapped_colors = self.to_rgba(self._A, self._alpha) |
| 67 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/cm.py", line 511, in to_rgba |
| 68 | x = self.norm(x) |
| 69 | File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/matplotlib/colors.py", line 1694, in __call__ |
| 70 | raise ValueError("Invalid vmin or vmax") |
| 71 | ValueError: Invalid vmin or vmax |
| 72 | ``` |
| 73 | |
| 74 | ### Expected outcome |
| 75 | |
| 76 | Works, colorbar and mappable are updated with new norm. |
| 77 | |
| 78 | ### Additional information |
| 79 | |
| 80 | _No response_ |
| 81 | |
| 82 | ### Operating system |
| 83 | |
| 84 | Linux |
| 85 | |
| 86 | ### Matplotlib Version |
| 87 | |
| 88 | 3.6.3 (works with 3.6.2) |
| 89 | |
| 90 | ### Matplotlib Backend |
| 91 | |
| 92 | Multpiple backends tested, same error in all (Qt5Agg, TkAgg, agg, ...) |
| 93 | |
| 94 | ### Python version |
| 95 | |
| 96 | 3.9.15 |
| 97 | |
| 98 | ### Jupyter version |
| 99 | |
| 100 | not in jupyter |
| 101 | |
| 102 | ### Installation |
| 103 | |
| 104 | conda |