Closed
Description
Bug summary
I experienced some specific odd cases where I noticed that the colors of an image were not correctly set after updating the image data and setting new color limits with set_clim(). After debugging I was able to reproduce with a minimal example,..
Code for reproduction
import numpy as np
from matplotlib import pyplot as plt
# Create test data
np.random.seed(19771105)
data_0 = np.round(np.random.rand(10, 20), 1)
data_1 = data_0 + 10
# Compute data range
range_0 = data_0.min(), data_0.max()
range_1 = data_1.min(), data_1.max()
# Plot data as image; Auto adjusts colorbar scale to value range
image = plt.imshow(data_0)
cbar = plt.colorbar(image) # NOTE: Problem does not occur if colorbar not created!
print(f'expected norm = {range_0}')
print(f'actual norm = {image.norm.vmin, image.norm.vmax}')
# Change image data and update image color limits
image.set_data(data_1)
image.set_clim(range_1)
print(f'expected norm = {range_1}')
print(f'actual norm = {image.norm.vmin, image.norm.vmax}') # Incorrect!
plt.show()
Actual outcome
expected initial norm = (0.0, 1.0)
computed initial norm = (0.0, 1.0)
expected updated norm = (10.0, 11.0)
computed updated norm = (0.9, 11.0)
Expected outcome
expected initial norm = (0.0, 1.0)
computed initial norm = (0.0, 1.0)
expected updated norm = (10.0, 11.0)
computed updated norm = (10.0, 11.0)
Additional information
Only happens if color bar is present; Removing the line that creates it from code lets you see the correct image (minus color bar).
Data dependent; Only occurs if the lowest value of the new image data is higher than the highest value of the old image data (Hence +10 in the code example).
Operating system
Windows 10
Matplotlib Version
3.6.0
Matplotlib Backend
QtAgg
Python version
3.11.9
Jupyter version
Installation
pip