You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Doesn't show 1-channel image by using matplotlib.image.AxesImage.set_data() #22808
Are you getting a traceback? If so can you share that as well?
It is also super helpful if you can give us code that we can copy-paste to run (random data is OK!) rather than trying to fill in the lines you left as pseudo code. There is a good chance we may guess wrong and debug the wrong issue.
@nicolasrosa Thank you. Do you mind defining what image_size is in the code reproduction above? More context to image_np would be helpful as well since I am trying to reproduce the actual outcome you provided.
@nicolasrosa Thank you. Do you mind defining what image_size is in the code reproduction above? More context to image_np would be helpful as well since I am trying to reproduce the actual outcome you provided.
via #15090 I would expect this to work at a technical level (and the above code runs for me without exception). I'm still not quite sure what is distinguishing working and failing.
I think the issue here is that imshow actually does two entirely different things:
given an RGB image, show it
given a 2D array, colormap it to a falsecolor image and show that
In the first case we try to infer if the user gave use [0-1] float RGB(A) or [0-255] int RGB(A), but in the second we set up a mini-processing pipeline to go from the input data (which is a 2D array of floats) to a 4-channel RGBA image (which is what we need to pass to the backends). We have to map the full gamut for floats -> a finite range of integers. To make this possible while still having any contrast the normalization functions have some max and min and any input values in that range are mapped to [0, 1] which are in turn mapped to colors (see https://matplotlib.org/stable/tutorials/colors/colormapnorms.html , https://matplotlib.org/stable/tutorials/colors/colormaps.html , and #18487 for lots of details). If the user does not specifally specify the min/max, we infer it from looking at the initial data. When you set the data later we do not automatically update the auto-inferred normalization limits. In the case of constant input data I think we go with equal vmin/vmax, but would need to check that.
I think you may want to do
im=ax.imshow(np.ones(...), vmax=0, vmax=255)
which tells Matplotlib that it should use the range [0, 255] for the false color mapping.
@mylasem The first step is to localize the bug so we can sort out what (if anything) we need to fix in the code or the documentation to prevent this problem in the future.
I thought the thing that was impossible was to infer vmin/vmax to be INTMAX/INTMIN for 1-channel false-color data, for actual RGB(A) data that we are just showing infering the range on type is justifiable.
I think question is if we get [1,0,1] is that on a 256 range or 0-1? Heuristically you could assume if all your data was between 0-1 it was on the 0-1 range, but that isn't going to be always correct.
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
tacaswell commentedon Apr 9, 2022
Are you getting a traceback? If so can you share that as well?
It is also super helpful if you can give us code that we can copy-paste to run (random data is OK!) rather than trying to fill in the lines you left as pseudo code. There is a good chance we may guess wrong and debug the wrong issue.
nicolasrosa commentedon Apr 9, 2022
mylasem commentedon Apr 9, 2022
@tacaswell @nicolasrosa This is my first time contributing and I am interested in fixing this issue. Mind if I look into this? Anything I should know?
nicolasrosa commentedon Apr 9, 2022
I forgot doing in the code, but if you try to plot the proposed images using the following lines, it will work.
nicolasrosa commentedon Apr 9, 2022
Hello @mylasem, go on. Let me know if I can help you with something.
mylasem commentedon Apr 9, 2022
@nicolasrosa Thank you. Do you mind defining what image_size is in the code reproduction above? More context to image_np would be helpful as well since I am trying to reproduce the actual outcome you provided.
nicolasrosa commentedon Apr 9, 2022
It was
image_size=(1024,2048).tacaswell commentedon Apr 9, 2022
via #15090 I would expect this to work at a technical level (and the above code runs for me without exception). I'm still not quite sure what is distinguishing working and failing.
I think the issue here is that
imshowactually does two entirely different things:In the first case we try to infer if the user gave use [0-1] float RGB(A) or [0-255] int RGB(A), but in the second we set up a mini-processing pipeline to go from the input data (which is a 2D array of floats) to a 4-channel RGBA image (which is what we need to pass to the backends). We have to map the full gamut for floats -> a finite range of integers. To make this possible while still having any contrast the normalization functions have some max and min and any input values in that range are mapped to [0, 1] which are in turn mapped to colors (see
https://matplotlib.org/stable/tutorials/colors/colormapnorms.html ,
https://matplotlib.org/stable/tutorials/colors/colormaps.html , and
#18487 for lots of details). If the user does not specifally specify the min/max, we infer it from looking at the initial data. When you set the data later we do not automatically update the auto-inferred normalization limits. In the case of constant input data I think we go with equal vmin/vmax, but would need to check that.
I think you may want to do
which tells Matplotlib that it should use the range [0, 255] for the false color mapping.
https://matplotlib.org/stable/tutorials/introductory/images.html is also a good reference.
@mylasem The first step is to localize the bug so we can sort out what (if anything) we need to fix in the code or the documentation to prevent this problem in the future.
jklymak commentedon Apr 10, 2022
... as pointed out a few times, this is theoretically impossible, so I don't think we should be doing this at all.
tacaswell commentedon Apr 10, 2022
At the c++ level I am 95% sure we dispatch on type (via the
_resamplecode path) and we have some logic to warn if the user is out of gamutmatplotlib/lib/matplotlib/image.py
Lines 713 to 731 in 2aa4b7d
I thought the thing that was impossible was to infer vmin/vmax to be INTMAX/INTMIN for 1-channel false-color data, for actual RGB(A) data that we are just showing infering the range on type is justifiable.
jklymak commentedon Apr 10, 2022
I think question is if we get [1,0,1] is that on a 256 range or 0-1? Heuristically you could assume if all your data was between 0-1 it was on the 0-1 range, but that isn't going to be always correct.
tacaswell commentedon Apr 10, 2022
If we get
[[[1, 0, 1]]](so shaped(1, 1, 3))we dispatch on type (because at ndim==3 we treat it as a direct RGB(A) image):If we get
[[1, 0, 1]](so shaped(1, 3)) we do not look at the types at all and scale from [0, 1]:github-actions commentedon Dec 10, 2025
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!