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
Incorrect & inconsistent coloring in .imshow() with LogNorm #19748
.imshow() with LogNorm produces incorrect and inconsistent coloring.
Code for reproduction
In this example the same array was plotted twice. The first .imshow() call plots the whole array. The following two plot two halves of the same array separately.
The mpl_toolkits is used only to fix axes and colorbar positioning; problems with coloring remain even without it.
As we can see, left parts of each patch produced by .imshow() is completely black. It seems like rounding occurred somewhere. It also can be seen, that different colors are used to represent the same numbers in different arrays.
Expected outcome
It was expected to see smooth transition from black to white from left to right. It was also expected not to see any difference in colors representing same numbers.
Matplotlib version
Operating system: Linux
Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.3.3
When I filled this bug I thought that it is may be possible to implement a kind of numerical trick to fix this. Something like doing resampling on data previously transformed with logarithm. If I understood it right, the approach of interpolating the image in RGBA space in #18782 is a sort of universal fix, which have its own drawbacks. Maybe, it is possible to interpolate the data after applying norm, but before colormapping. This way we can avoid producing image pixels that are not in the colormap.
Just provide enough pixels that you don't subsample and #18782 won't provide any out-of-colormap pixels. #18782 fixes your issue because it bypasses all the norm-making logic in imshow that does some useful things, but also has bad side effects like this one.
I think this is the third option we were struggling to find on the call
(resample) -> (norm) -> (colormap)
(norm) -> (resample) -> (colormap)
(norm) -> (colormap) -> (resample)
All make sense and have sensible uses.
The actually core of the problem here is that the resampling code we use is from Agg and clips the data at [0, 1] and is not inherent to resampling first.
I don't believe that this issue was actually addressed by #18782. Running the example code from the original bug report still shows the same behavior. Someone also noted this problem on Stack Overflow about 5 years ago, and a coworker hit this yesterday trying to use imshow to visualize a matrix.
I did my testing with Windows, CPython 3.11, matplotlib 3.8.0. Here's is basically the original bug report's demo code, but showing how pcolormesh plots the data correctly, and with some annotations to make it easier to understand what the plot is visualizing.
frommatplotlib.colorsimportLogNormfrommatplotlib.patchesimportRectangleimportmatplotlib.pyplotaspltfrommpl_toolkits.axes_grid1importmake_axes_locatableimportnumpyasnpa=np.atleast_2d(10.0**np.arange(100))
n=LogNorm(a.min(), a.max())
fig, ax=plt.subplots(1)
plt.title("a = np.atleast_2d(10. ** np.arange(100))\nn = LogNorm(a.min(), a.max())")
img1=ax.imshow(a, extent=(0, 100, 0, 10), cmap="gray", norm=n)
ax.imshow(a[:, :50], extent=(0, 50, -10, 0), cmap="gray", norm=n)
ax.imshow(a[:, 50:], extent=(50, 100, -10, 0), cmap="gray", norm=n)
# x, y so we can put the pcolormesh(a) on the same plot, but below imshow'sy, x=np.mgrid[-20:-9:10, 0:101:1]
ax.pcolormesh(x, y, a, cmap="gray", norm=n)
# Make it clear what parts of the plot are from which function callsheight=9.8ax.add_patch(Rectangle((0, 0), 100, height, ec="tab:red", fc="none", lw=2))
plt.text(1, 1, "imshow(a, norm=n)", color="tab:red")
#ax.add_patch(Rectangle((0, -10), 49.8, height, ec="tab:orange", fc="none", lw=2))
plt.text(1, -9, "imshow(a[:, :50], norm=n)", color="tab:orange")
ax.add_patch(Rectangle((50, -10), 50, height, ec="tab:purple", fc="none", lw=2))
plt.text(51, -9, "imshow(a[:, 50:], norm=n)", color="tab:purple")
#ax.add_patch(Rectangle((0, -20), 100, height, ec="tab:green", fc="none", lw=2))
plt.text(1, -19, "pcolormesh(a, norm=n)", color="tab:green")
#plt.xlim(-0.1, 100.1)
plt.ylim(-20.1, 10.1)
ax.set_yticks([])
divider=make_axes_locatable(plt.gca())
cax=divider.append_axes("bottom", size=0.2, pad=0.4)
plt.colorbar(img1, cax=cax, orientation="horizontal")
plt.tight_layout()
plt.savefig("imshow_lognorm_problems.png", bbox_inches="tight")
Oh, I forgot to note also, that if instead of using imshow with a LogNorm normalization we use imshow to plot data we already applied the exact same normalization to, the we get the plot we expect. The problem with doing this of course is that a colorbar for already nomalized data ranges from 0 to 1 rather than logscaled min(data) to max(data). Here's an updated snippet that demonstrates this.
frommatplotlib.colorsimportLogNormfrommatplotlib.patchesimportRectangleimportmatplotlib.pyplotaspltfrommpl_toolkits.axes_grid1importmake_axes_locatableimportnumpyasnpa=np.atleast_2d(10.0**np.arange(100))
n=LogNorm(a.min(), a.max())
fig, ax=plt.subplots(1)
plt.title("a = np.atleast_2d(10. ** np.arange(100))\nn = LogNorm(a.min(), a.max())")
img1=ax.imshow(a, extent=(0, 100, 0, 10), cmap="gray", norm=n)
ax.imshow(a[:, :50], extent=(0, 50, -10, 0), cmap="gray", norm=n)
ax.imshow(a[:, 50:], extent=(50, 100, -10, 0), cmap="gray", norm=n)
# x, y so we can put the pcolormesh(a) on the same plot, but below imshow'sy, x=np.mgrid[-20:-9:10, 0:101:1]
ax.pcolormesh(x, y, a, cmap="gray", norm=n)
ax.imshow(n(a), extent=(0, 100, -30, -20), cmap="gray", norm=None)
# Make it clear what parts of the plot are from which function callsheight=9.8ax.add_patch(Rectangle((0, 0), 100, height, ec="tab:red", fc="none", lw=2))
plt.text(1, 1, "imshow(a, norm=n)", color="tab:red")
#ax.add_patch(Rectangle((0, -10), 49.8, height, ec="tab:orange", fc="none", lw=2))
plt.text(1, -9, "imshow(a[:, :50], norm=n)", color="tab:orange")
ax.add_patch(Rectangle((50, -10), 50, height, ec="tab:purple", fc="none", lw=2))
plt.text(51, -9, "imshow(a[:, 50:], norm=n)", color="tab:purple")
#ax.add_patch(Rectangle((0, -20), 100, height, ec="tab:green", fc="none", lw=2))
plt.text(1, -19, "pcolormesh(a, norm=n)", color="tab:green")
#ax.add_patch(Rectangle((0, -30), 100, height, ec="tab:blue", fc="none", lw=2))
plt.text(1, -29, "imshow(n(a), norm=None) --> colorbar for this is linear [0, 1]", color="tab:blue")
#plt.xlim(-0.1, 100.1)
plt.ylim(-30.1, 10.1)
ax.set_yticks([])
divider=make_axes_locatable(plt.gca())
cax=divider.append_axes("bottom", size=0.2, pad=0.4)
plt.colorbar(img1, cax=cax, orientation="horizontal")
plt.tight_layout()
plt.savefig("imshow_lognorm_problems.png", bbox_inches="tight")
plt.show()
(Sorry, I know the some of the text on the last part of this image doesn't show up well against the image.)
@daytonb You are not using the interploation_stage kwarg which controls this behaviour. Please test that. If it doesn't work, please open a new issue with a minimal reproducible example.
That's interesting. I tried interpolation_stage='rgba' and that does indeed produce the correct plot. I'm puzzled how interpolation is relevant to this problem. Matplotlib docs do have a page about "Image antialiasing" which states
The default image interpolation in Matplotlib is 'antialiased', and it is applied to the data. This uses a hanning interpolation on the data provided by the user for reduced aliasing in most situations.
I'm curious where in the code I would find this "interpolation" implemented, because I understand the "data" vs "rgba" pros/cons on the images demonstrated in that matplotlib documentation page, but I don't understand how antialiasing is involved in a plot this this one:
x=np.linspace(0, 100, 20)
a=np.atleast_2d(10.0**x)
n=LogNorm(a.min(), a.max())
plt.title("a = np.atleast_2d(10. ** np.arange(100))\nn = LogNorm(a.min(), a.max())")
img1=ax.imshow(a, extent=(0, 100, -10, 0), interpolation_stage='data', cmap="gray", norm=n)
plt.imshow(a, extent=(0, 100, 0, 10), interpolation_stage='rgba', cmap="gray", norm=n)
# Make it clear what parts of the plot are from which function callsplt.text(1, -5, "imshow(a, norm=n, interpolation_stage='data')", color="tab:red")
plt.text(1, 5, "imshow(a, norm=n, interpolation_stage='rgba')", color="tab:orange")
par=plt.gca().twinx()
par.plot(x.flat, a.flat)
plt.show()
Your image is being up-sampled (probably). Matplotlib needs to interpolate in order to make that happen. Your data is exponential so the interpolation in data space is terrible. Your colorspace is linear because you use a log norm, so interpolation in color space is good.
Actually the above is not the real reason - sorry.
The real reason is that when we do the interpolation in data space we put a linear buffer around the data range. We rescale the data from 0.1 to 0.9 of the total range, so in your case, we put a buffer of 0.1 * 10^100 = 10*99 on either side of the data. We then linearly interpolate, and then we remove the buffer. We do this because agg hard-clips at 0 and 1, and we want to have over/under data properly flagged as such, not be indistinguishable from being exactly at the bound.
That works fine for linear data, but exponential data like this it is a terrible algorithm. Nothing survives this algorithm from the low end of the exponential distribution.
The algorithm is basically as below:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 100, 20)
a = 10.0 ** x
scaleda = 0.8 * (a - a.min())/(a.max()-a.min()) + 0.1
xx = np.linspace(0, 100, 50)
aa = np.interp(xx, x, scaleda)
fig, ax = plt.subplots()
ax.semilogy(x, a, label='orig')
ax.semilogy(x, (scaleda - 0.1) * (a.max() - a.min())/0.8 + a.min(), label='scaled')
ax.semilogy(xx, (aa - 0.1) * (a.max() - a.min())/0.8 + a.min(), label='scaled interp')
ax.legend()
plt.show()
BTW, for what it is worth, I think the default behaviour is objectively poor, and we should always use interpolation_stage='rgba', but there are others who feel just as strongly the other way that every color in an image, even a upsampled one should correspond to a color in the colormap: think red and blue colormap - should a pixel between a red/blue sharp line be white, or should it be purple?
At the moment I'm on your side of that argument 😃, though I do understand the issue of purple pixels in a colormap that doesn't actually include purple. Everything has it's pros and cons. Your description and plots help a lot in understanding what's going on. Again, can you point me at where in the matplotlib code base this linear-buffer/scaling process is coded?
Do you think it would be worth a code warning or documentation warning about this sort of consequence of interpolation_state='rgba' + norm=LogNorm()?
Actually, I guess warning on interpolation_state='rgba' + norm=LogNorm() wouldn't work because the problem doesn't have to do with using LogNorm, but rather with the exponential nature of the data itself.
I actually wonder about the algorithm and if the range could/should just be expanded one dx to the left or right, where dx is determined by the data at the low and high end of the distribution. That largely fix this problem, though it would necessitate a potentially expensive sort.
One "dx"? as in the bottom dx is the difference between the two smallest values and a top dx that is the difference between the largest two values? If so, you wouldn't need to perform a sort. You could write an O(n) algorithm that would just iterate through the data once collecting the two biggest and two smallest values, then compute these upper and lower dx values. For example:
defget_dxs(data: np.ndarray) ->tuple[float, float]:
"""Return the top dx and bottom dx of the data. The bottom dx is the distance between the smallest unique values of the data. The top dx is the distance between the biggest unique values of the data. If there are only two unique values, then both dx are 10% of the difference between those values. If there is only one unique value, then both dx are 10% of that value. """# Initialize the list of "extreme values" with the first four unique values from the data.extremes= []
fori, xinenumerate(data.flat):
iflen(extremes) <4:
ifxnotinextremes:
extremes=sorted(extremes+ [x])
continuebreakiflen(extremes) ==1:
dx_bottom=dx_top=extremes[0] /10eliflen(extremes) ==2:
dx_bottom=dx_top= (extremes[1] -extremes[0]) /10eliflen(extremes) ==3:
dx_bottom=extremes[1] -extremes[0]
dx_top=extremes[2] -extremes[1]
else:
# This case is the real meat of the algorithm.smallest, small, big, biggest=extremes# Consider each value of the data to see if it bumps out one of the top 2 or bottom 2 values.forxindata.flat[i:]:
ifx<smallest:
small, smallest=smallest, xelifsmallest<x<small:
small=xelifbig<x<biggest:
big=xelifbiggest<x:
big, biggest=biggest, xdx_bottom=small-smallestdx_top=biggest-bigreturndx_bottom, dx_top
Activity
jklymak commentedon Mar 21, 2021
Yes, this is a consequence of doing the resampling for imshow in data space. It would be closed by #18782.
mdmisch commentedon Mar 29, 2021
When I filled this bug I thought that it is may be possible to implement a kind of numerical trick to fix this. Something like doing resampling on data previously transformed with logarithm. If I understood it right, the approach of interpolating the image in RGBA space in #18782 is a sort of universal fix, which have its own drawbacks. Maybe, it is possible to interpolate the data after applying norm, but before colormapping. This way we can avoid producing image pixels that are not in the colormap.
jklymak commentedon Mar 29, 2021
Just provide enough pixels that you don't subsample and #18782 won't provide any out-of-colormap pixels. #18782 fixes your issue because it bypasses all the norm-making logic in imshow that does some useful things, but also has bad side effects like this one.
tacaswell commentedon Apr 2, 2021
I think this is the third option we were struggling to find on the call
All make sense and have sensible uses.
The actually core of the problem here is that the resampling code we use is from Agg and clips the data at [0, 1] and is not inherent to resampling first.
also xref #18487
jklymak commentedon Apr 3, 2021
Redoing the resamoling in python would be a reasonable summer of code project.
daytonb commentedon Oct 12, 2023
I don't believe that this issue was actually addressed by #18782. Running the example code from the original bug report still shows the same behavior. Someone also noted this problem on Stack Overflow about 5 years ago, and a coworker hit this yesterday trying to use
imshowto visualize a matrix.I did my testing with Windows, CPython 3.11, matplotlib 3.8.0. Here's is basically the original bug report's demo code, but showing how
pcolormeshplots the data correctly, and with some annotations to make it easier to understand what the plot is visualizing.daytonb commentedon Oct 12, 2023
Oh, I forgot to note also, that if instead of using
imshowwith aLogNormnormalization we useimshowto plot data we already applied the exact same normalization to, the we get the plot we expect. The problem with doing this of course is that a colorbar for already nomalized data ranges from 0 to 1 rather than logscaled min(data) to max(data). Here's an updated snippet that demonstrates this.(Sorry, I know the some of the text on the last part of this image doesn't show up well against the image.)
jklymak commentedon Oct 12, 2023
@daytonb You are not using the interploation_stage kwarg which controls this behaviour. Please test that. If it doesn't work, please open a new issue with a minimal reproducible example.
daytonb commentedon Oct 12, 2023
That's interesting. I tried
interpolation_stage='rgba'and that does indeed produce the correct plot. I'm puzzled how interpolation is relevant to this problem. Matplotlib docs do have a page about "Image antialiasing" which statesI'm curious where in the code I would find this "interpolation" implemented, because I understand the "data" vs "rgba" pros/cons on the images demonstrated in that matplotlib documentation page, but I don't understand how antialiasing is involved in a plot this this one:
jklymak commentedon Oct 12, 2023
Your image is being up-sampled (probably). Matplotlib needs to interpolate in order to make that happen. Your data is exponential so the interpolation in data space is terrible. Your colorspace is linear because you use a log norm, so interpolation in color space is good.
jklymak commentedon Oct 13, 2023
Actually the above is not the real reason - sorry.
The real reason is that when we do the interpolation in data space we put a linear buffer around the data range. We rescale the data from 0.1 to 0.9 of the total range, so in your case, we put a buffer of 0.1 * 10^100 = 10*99 on either side of the data. We then linearly interpolate, and then we remove the buffer. We do this because agg hard-clips at 0 and 1, and we want to have over/under data properly flagged as such, not be indistinguishable from being exactly at the bound.
That works fine for linear data, but exponential data like this it is a terrible algorithm. Nothing survives this algorithm from the low end of the exponential distribution.
The algorithm is basically as below:
jklymak commentedon Oct 13, 2023
BTW, for what it is worth, I think the default behaviour is objectively poor, and we should always use
interpolation_stage='rgba', but there are others who feel just as strongly the other way that every color in an image, even a upsampled one should correspond to a color in the colormap: think red and blue colormap - should a pixel between a red/blue sharp line be white, or should it be purple?daytonb commentedon Oct 13, 2023
At the moment I'm on your side of that argument 😃, though I do understand the issue of purple pixels in a colormap that doesn't actually include purple. Everything has it's pros and cons. Your description and plots help a lot in understanding what's going on. Again, can you point me at where in the matplotlib code base this linear-buffer/scaling process is coded?
Do you think it would be worth a code warning or documentation warning about this sort of consequence of
interpolation_state='rgba'+norm=LogNorm()?jklymak commentedon Oct 13, 2023
It starts at
matplotlib/lib/matplotlib/image.py
Line 424 in e394940
interpolation_stage='rgba', which skips all the rescaling.daytonb commentedon Oct 13, 2023
Actually, I guess warning on
interpolation_state='rgba'+norm=LogNorm()wouldn't work because the problem doesn't have to do with usingLogNorm, but rather with the exponential nature of the data itself.jklymak commentedon Oct 13, 2023
I actually wonder about the algorithm and if the range could/should just be expanded one dx to the left or right, where dx is determined by the data at the low and high end of the distribution. That largely fix this problem, though it would necessitate a potentially expensive sort.
daytonb commentedon Oct 15, 2023
One "dx"? as in the bottom dx is the difference between the two smallest values and a top dx that is the difference between the largest two values? If so, you wouldn't need to perform a sort. You could write an O(n) algorithm that would just iterate through the data once collecting the two biggest and two smallest values, then compute these upper and lower dx values. For example:
jklymak commentedon Oct 15, 2023
Yeah but now that I think about that it still probably won't help the rounding problem.