Skip to content

[Bug]: pcolormesh with log norm on data with zeros is broken, unless colorbar is added #32599

Closed

Description

@nj-vs-vh

Bug summary

I'm plotting 2D array with pcolormesh and:

  • use logarithmic normalization (norm="log")
  • array contains zeros (unity matrix in the MRE example)
  • array containing only one non-zero value (e.g. unity)

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
        
fig, axes = plt.subplots(figsize=(8, 4), ncols=2)

for i, ax in enumerate(axes):
    mesh = ax.pcolormesh(np.eye(3), norm="log")
    if i == 1:
        fig.colorbar(mesh, ax=ax)

fig.savefig("mre.png")

Actual outcome

The resulting plot has all bins colored the same color (cmap(0)) (left). However, if I add colorbar to the axes (right), the result is correct.

image

Expected outcome

Correct result regarding of colorbar presence (lognorm color applied to non-zero bins, zero ones are white/blank).

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.11.2, also tested in 3.10.5

Matplotlib Backend

macosx

Python version

Python 3.14.9

Jupyter version

No response

Installation

pip

Activity

  1. nj-vs-vh commented on Nov 14, 2026

    @nj-vs-vh
    Author

    I didn't find this issue reported but it seems plausibly related to #8540

  2. nj-vs-vh commented on Nov 14, 2026

    @nj-vs-vh
    Author

    UPD: when the array has >1 non-zero unique value, everything is consistent

    image
  3. rcomer commented on Nov 14, 2026

    @rcomer
    Member

    Thanks for the report. It seems that colorbar somehow modifies the vmin and vmax of the norm, which I assume is deliberate. We have

    Without colorbar:

    In [46]: LogNorm(vmin=1, vmax=1)([0, 1])
    Out[46]: 
    masked_array(data=[0., 0.],
                 mask=False,
           fill_value=1e+20)

    With colorbar:

    In [44]: LogNorm(vmin=0.9, vmax=1.1)([0, 1])
    Out[44]: 
    masked_array(data=[--, 0.5250418623166554],
                 mask=[ True, False],
           fill_value=1e+20)

    I think the output masks should match for these two cases, even if the norms' vmin and vmax remain different.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions