Skip to content

[Bug]: Colorbar with log scales wrong format #23389

Closed

Description

@RemDelaporteMathurin

Bug summary

The colour bar ticks are weirdly formatted when using a logarithmic scale when using version 3.5.0 or later.

Code for reproduction

Taken from https://matplotlib.org/stable/gallery/images_contours_and_fields/contourf_log.html


import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
from matplotlib import ticker, cm

N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)

X, Y = np.meshgrid(x, y)

# A low hump with a spike coming out.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
z = Z1 + 50 * Z2

# Put in some negative values (lower left corner) to cause trouble with logs:
z[:5, :5] = -1

# The following is not strictly essential, but it will eliminate
# a warning.  Comment it out to see the warning.
z = ma.masked_where(z <= 0, z)


# Automatic selection of levels works; setting the
# log locator tells contourf to use a log scale:
fig, ax = plt.subplots()
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)

cbar = fig.colorbar(cs)

plt.show()

Actual outcome

image

Expected outcome

Running the exact same code on 3.4.2 produces the expected behaviour
image

Additional information

This worked perfectly on version 3.4.2

Operating system

Windows

Matplotlib Version

3.5.0

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

pip

Activity

  1. jklymak commented on Jul 5, 2022

    @jklymak
    Member

    Agree this is broken inadvertently. We refactored some things in colorbar, but the infinite API complexity that crept in over the years is causing problems here again. I'll try to fix, but we almost need a separate colorbar for contour, rather than tests all over the place.

  2. jklymak commented on Jul 5, 2022

    @jklymak
    Member

    BTW, a workaround here is to simply call

    cbar.ax.set_yscale('log')

    at the end of the script.

  3. RemDelaporteMathurin commented on Jul 5, 2022

    @RemDelaporteMathurin
    Author

    Interesting, I shall try it out, thanks!

  4. added this to the v3.5.3 milestone on Jul 26, 2022
  5. modified the milestones: v3.5.3, v3.5.4 on Aug 3, 2022
  6. modified the milestones: v3.5.4, v3.6.1 on Sep 25, 2022
  7. modified the milestones: v3.6.1, v3.6.2 on Oct 6, 2022
  8. modified the milestones: v3.6.2, v3.6.3 on Oct 27, 2022
  9. adrianterronesaragon commented on Aug 28, 2024

    @adrianterronesaragon

    Bug summary

    The colour bar ticks are weirdly formatted when using a logarithmic scale when using version 3.5.0 or later.

    Code for reproduction

    Taken from https://matplotlib.org/stable/gallery/images_contours_and_fields/contourf_log.html
    
    
    import matplotlib.pyplot as plt
    import numpy as np
    from numpy import ma
    from matplotlib import ticker, cm
    
    N = 100
    x = np.linspace(-3.0, 3.0, N)
    y = np.linspace(-2.0, 2.0, N)
    
    X, Y = np.meshgrid(x, y)
    
    # A low hump with a spike coming out.
    # Needs to have z/colour axis on a log scale so we see both hump and spike.
    # linear scale only shows the spike.
    Z1 = np.exp(-X**2 - Y**2)
    Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
    z = Z1 + 50 * Z2
    
    # Put in some negative values (lower left corner) to cause trouble with logs:
    z[:5, :5] = -1
    
    # The following is not strictly essential, but it will eliminate
    # a warning.  Comment it out to see the warning.
    z = ma.masked_where(z <= 0, z)
    
    
    # Automatic selection of levels works; setting the
    # log locator tells contourf to use a log scale:
    fig, ax = plt.subplots()
    cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
    
    cbar = fig.colorbar(cs)
    
    plt.show()

    Actual outcome

    image

    Expected outcome

    Running the exact same code on 3.4.2 produces the expected behaviour image

    Additional information

    This worked perfectly on version 3.4.2

    Operating system

    Windows

    Matplotlib Version

    3.5.0

    Matplotlib Backend

    No response

    Python version

    No response

    Jupyter version

    No response

    Installation

    pip

    This issue is still happening in version 3.9.12

  10. tacaswell commented on Aug 28, 2024

    @tacaswell
    Member

    We don't have a 3.9.12, did you mean 3.9.1 or 3.9.2?

    I can not reproduce this with 3.9.2, 3.8.4, or main

    so

  11. adrianterronesaragon commented on Aug 29, 2024

    @adrianterronesaragon

    Sorry, that is my Python version, my bad. I'm using Matplotlib version 3.5.1.

  12. tacaswell commented on Aug 29, 2024

    @tacaswell
    Member

    This bug looks like it was fixed in mpl 3.6.3 so you will need to upgrade atleast that far to get the fix.

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