Skip to content

[Bug]: colors.PowerNorm results in incorrect colorbar #25239

@bartvstratum

Description

@bartvstratum

Bug summary

colors.PowerNorm ignores the (in this case) vmin values, and the resulting colorbar seems to b incorrect.

Code for reproduction

import matplotlib.pyplot as pl
import matplotlib.colors as colors
import numpy as np
    
data = np.random.random(100*100).reshape(100,100) * 600 + 398
    
pl.figure()
pl.imshow(data, cmap=pl.cm.terrain, norm=colors.PowerNorm(gamma=0.2, vmin=398, vmax=1000))
pl.colorbar()

Actual outcome

It seems that vmin is changed to some pretty number (in this case 300), but as a result, the colorbar seems to be incorrect. In the screenshot, my mouse is hoovering over the blue data point, which has a value of 398.062 (see value in top right corner of window). However, corresponding values in the colorbar are greenish, while blue colors correspond to much lower numbers.

Screenshot_20230217_102233

Expected outcome

I would expect matplotlib to respect the vmin and vmax values that I set in colors.PowerNorm.

You can best see the correct outcome if you manually set vmin at the value that matplotlib chooses:

import matplotlib.pyplot as pl
import matplotlib.colors as colors
import numpy as np

data = np.random.random(100*100).reshape(100,100) * 600 + 398

pl.figure()
pl.subplot(121)
pl.imshow(data, cmap=pl.cm.terrain, norm=colors.PowerNorm(gamma=0.2, vmin=398, vmax=1000))
cb=pl.colorbar()

pl.subplot(122)
pl.imshow(data, cmap=pl.cm.terrain, norm=colors.PowerNorm(gamma=0.2, vmin=300, vmax=1000))
cb=pl.colorbar()

Figure_4

Additional information

No response

Operating system

Manjaro Linux

Matplotlib Version

3.6.2

Matplotlib Backend

Qt5Agg

Python version

3.10.9

Jupyter version

None

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions