Skip to content

[Bug]: Matplotlib >= 3.5.0 drops scientific notation for colorbars using log ticks #24002

@jmd-dk

Description

@jmd-dk

Bug summary

When using logarithmic scaling of the data with 2D plots, the ticks for the colorbar are not written in scientific notation. Prior to Matplotlib 3.5.0 (e.g. 3.4.3), scientific notation was used, as one would expect. The problem persists in 3.6.

Code for reproduction

# Sample code for 2D plotting with log scaling,
# with the log scaling implemented
# using two different methods.

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

n = 100
np.random.seed(42)
x = np.random.random(n)
y = np.random.random(n)
z = np.logspace(np.log10(1e-7), np.log10(1e+7), n)

fig, axes = plt.subplots(2, 1)

tcs = axes[0].tricontourf(
    x, y, z,
    norm=matplotlib.colors.LogNorm(vmin=z.min(), vmax=z.max()),
)
cbar = fig.colorbar(tcs, ax=axes[0])

tcs = axes[1].tricontourf(
    x, y, z,
    locator=matplotlib.ticker.LogLocator(),
)
cbar = fig.colorbar(tcs, ax=axes[1])

plt.savefig('test.png')

Actual outcome

Here is the outcome of the above in Matplotlib 3.5.0 or 3.6.0 (the results are identical). The image properly shows logarithmic values, and the colorbar ticks are in log scale as well, but the colorbar tick labels are not written using scientific notation. One major drawback is that all tics but the highest two gets written as 0.00, while they are really 0.001, 0.0001, etc.
test_3 5 0

Expected outcome

Same as above but run with Matplotlib 3.4.3. Both methods work to produce scientific notation, as desired.
test_3 4 3

Operating system

Linux Mint 20

Matplotlib Version

As described above

Matplotlib Backend

Agg and Qt5Agg

Python version

3.8.5 and 3.9.9

Installation

from source (.tar.gz)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions