GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors · Issue #28731 · matplotlib/matplotlib

[Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors #28731

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
[Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors#28731
Copy link
Milestone
v3.10.0

Description

@faressc
faressc
opened on Aug 15, 2024
Issue body actions

Bug summary

Plotting an equal numpy.array of dtype float64 and float32 with pyplot.imshow and a defined colors.LogNorm as norm will produce different colors in the resulting image.

Code for reproduction

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

norm = colors.LogNorm(vmin=1e-10, vmax=1)
test_numpy = np.array([[1e-4]])
test_numpy_32 = np.array([[1e-4]], dtype=np.float32)

for test in [test_numpy, test_numpy_32]:
    plt.imshow(test, norm=norm)
    plt.title(f"numpy array of dtype: {test.dtype}")
    plt.colorbar()
    plt.show()

Actual outcome

The resulting plot for the dtype float64 numpy.array looks like this:

float64

and the plot for the dtype float32 numpy.array looks like this:

float32

Expected outcome

I would expect both outputs to have the same color.

Additional information

No response

Operating system

Arch

Matplotlib Version

3.9.1.post1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.12.4

Jupyter version

No response

Installation

pip

Activity

  1. tacaswell commented on Aug 15, 2024

    @tacaswell
    tacaswell
    on Aug 15, 2024
    Member
    More actions

    I can confirm this, however it is related to having singular arrays:

    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    import numpy as np
    
    norm = colors.LogNorm(vmin=1e-10, vmax=1)
    test_numpy = np.array([[1e-4, 1e-5, 1e-1]]).T
    test_numpy_32 = np.array([[1e-4, 1e-5, 1e-1]], dtype=np.float32).T
    
    fig, axl = plt.subplots(1, 2)
    
    for ax, test in zip(axl, [test_numpy, test_numpy_32]):
        im = ax.imshow(test, norm=norm)
        ax.set_title(f"numpy array of dtype: {test.dtype}")
        fig.colorbar(im)
    
    plt.show()

    works correctly.

  2. tacaswell
    added this to the v3.9.3 milestone on Aug 15, 2024
  3. tacaswell commented on Aug 15, 2024

    @tacaswell
    tacaswell
    on Aug 15, 2024
    Member
    More actions
    ax.imshow(test, norm=norm, interpolation_stage='rgba')

    Also fixes the problem so it is definitely in our data-value resampling code.

  4. tacaswell
    modified the milestones: v3.9.3, v3.10.0 on Aug 15, 2024
  5. tacaswell commented on Aug 15, 2024

    @tacaswell
    tacaswell
    on Aug 15, 2024
    Member
    More actions

    I think this is fixed by #28122 but that seems too big of a change to backport.

  6. timhoffm commented on Aug 15, 2024

    @timhoffm
    timhoffm
    on Aug 15, 2024
    Member
    More actions

    I suspect that it may be an effect of the limited precision. float32 can only store 7-8 significant digits, but the log scale is set to 10 decades. Depending on how the exact calculation is done internally, the precision limit can translate to a macroscopic effect. A quick test would be to check if this goes away when limiting the log range to 6 or 7 decades.

  7. ksunden commented on Aug 15, 2024

    @ksunden
    ksunden
    on Aug 15, 2024
    Member
    More actions

    Indeed, changing the bounds improves it significantly (I zoomed in pretty far, didn't try too many range sizes):

    Figure_1
    Figure_2

  8. jklymak commented on Aug 15, 2024

    @jklymak
    jklymak
    on Aug 15, 2024
    Member
    More actions

    This is indeed fixed on master for both interpolation_stage='rgba' (the new default) and interpolation_stage='data' (the current default). I don't think we are going to backport either #28122 or #28061, but I also don't think we should fix this just for a point release as this would be a long-standing behaviour. Currently the workaround is interpolation_stage='rgba'. I'll close as already fixed on master

  9. jklymak
    closed this as not plannedon Aug 15, 2024
  10. faressc commented on Aug 15, 2024

    @faressc
    faressc
    on Aug 15, 2024
    Author
    More actions

    Thanks for the quick answers and happy to hear that it has been already fixed. I'll use the workaround interpolation_stage='rgba' for now :)

  11. ksunden
    changed the title [-][Bug]: Plotting numpy.arrray of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors[/-] [+][Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors[/+] on Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      • v3.10.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.