Skip to content

Incorrect & inconsistent coloring in .imshow() with LogNorm #19748

Closed

Description

@mdmisch

Bug report

Bug summary

.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.

import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

import numpy as np

a = 10. ** np.arange(100)

fig, ax = plt.subplots(1)
norm = mpl.colors.LogNorm(np.min(a), np.max(a))

img1 = ax.imshow(a[None,   :  ], extent = (0,  100,   0, 10),
                                 cmap = 'gray', norm = norm  )
img2 = ax.imshow(a[None,   :50], extent = (0,   50, -10,  0),
                                 cmap = 'gray', norm = norm  )
img3 = ax.imshow(a[None, 50:  ], extent = (50, 100, -10,  0),
                                 cmap = 'gray', norm = norm  )

plt.xlim(  0, 100)
plt.ylim(-10,  10)

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.show()

Actual outcome

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.

Figure_1

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
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.7.3

The matplotlib package was installed with pip.

Activity

  1. jklymak commented on Mar 21, 2021

    @jklymak
    Member

    Yes, this is a consequence of doing the resampling for imshow in data space. It would be closed by #18782.

  2. added this to the v3.5.0 milestone on Mar 21, 2021
  3. linked a pull request that will close this issueENH: allow image to interpolate post RGBA #18782on Mar 21, 2021
  4. mdmisch commented on Mar 29, 2021

    @mdmisch
    Author

    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.

  5. jklymak commented on Mar 29, 2021

    @jklymak
    Member

    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.

  6. tacaswell commented on Apr 2, 2021

    @tacaswell
    Member

    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.

    also xref #18487

  7. jklymak commented on Apr 3, 2021

    @jklymak
    Member

    Redoing the resamoling in python would be a reasonable summer of code project.

  8. daytonb commented on Oct 12, 2023

    @daytonb
    Contributor

    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.

    from matplotlib.colors import LogNorm
    from matplotlib.patches import Rectangle
    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    import numpy as np
    
    a = 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's
    y, 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 calls
    height = 9.8
    ax.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")

    imshow_lognorm_problems

  9. daytonb commented on Oct 12, 2023

    @daytonb
    Contributor

    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.

    from matplotlib.colors import LogNorm
    from matplotlib.patches import Rectangle
    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    import numpy as np
    
    a = 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's
    y, 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 calls
    height = 9.8
    ax.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()

    imshow_lognorm_problems
    (Sorry, I know the some of the text on the last part of this image doesn't show up well against the image.)

  10. jklymak commented on Oct 12, 2023

    @jklymak
    Member

    @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.

  11. daytonb commented on Oct 12, 2023

    @daytonb
    Contributor

    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 calls
    plt.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()

    demo

  12. jklymak commented on Oct 12, 2023

    @jklymak
    Member

    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.

  13. jklymak commented on Oct 13, 2023

    @jklymak
    Member

    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()
    

    imageinterp

  14. jklymak commented on Oct 13, 2023

    @jklymak
    Member

    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?

  15. daytonb commented on Oct 13, 2023

    @daytonb
    Contributor

    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()?

  16. jklymak commented on Oct 13, 2023

    @jklymak
    Member

    It starts at

    if A.ndim == 2 and self._interpolation_stage != 'rgba':
    . Note the cut-out for interpolation_stage='rgba', which skips all the rescaling.

  17. daytonb commented on Oct 13, 2023

    @daytonb
    Contributor

    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.

  18. jklymak commented on Oct 13, 2023

    @jklymak
    Member

    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.

  19. daytonb commented on Oct 15, 2023

    @daytonb
    Contributor

    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:

    def get_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 = []
        for i, x in enumerate(data.flat):
            if len(extremes) < 4:
                if x not in extremes:
                    extremes = sorted(extremes + [x])
                continue
            break
    
        if len(extremes) == 1:
            dx_bottom = dx_top = extremes[0] / 10
        elif len(extremes) == 2:
            dx_bottom = dx_top = (extremes[1] - extremes[0]) / 10
        elif len(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.
            for x in data.flat[i:]:
                if x < smallest:
                    small, smallest = smallest, x
                elif smallest < x < small:
                    small = x
                elif big < x < biggest:
                    big = x
                elif biggest < x:
                    big, biggest = biggest, x
            dx_bottom = small - smallest
            dx_top = biggest - big
        return dx_bottom, dx_top
  20. jklymak commented on Oct 15, 2023

    @jklymak
    Member

    Yeah but now that I think about that it still probably won't help the rounding problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions