Skip to content

Creating colorbar without artist fails with LogNorm #13967

@dstansby

Description

@dstansby

#13171 documented how to create a colobar that is not associated with an artist. This recipe fails when the colorbar norm is a LogNorm however:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as mcolor

fig, ax = plt.subplots()
sm = cm.ScalarMappable(norm=mcolor.LogNorm(), cmap='viridis')
fig.colorbar(sm)
plt.draw()

gives

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    fig.colorbar(sm)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/figure.py", line 2208, in colorbar
    cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colorbar.py", line 1641, in colorbar_factory
    cb = Colorbar(cax, mappable, **kwargs)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colorbar.py", line 1183, in __init__
    ColorbarBase.__init__(self, ax, **kw)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colorbar.py", line 460, in __init__
    self.draw_all()
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colorbar.py", line 483, in draw_all
    self._process_values()
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colorbar.py", line 932, in _process_values
    b = self.norm.inverse(self._uniform_y(self.cmap.N + 1))
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colors.py", line 1085, in inverse
    return vmin * np.ma.power((vmax / vmin), val)
ZeroDivisionError: division by zero

Changing LogNorm to Normalize makes the example work fine again, as intended.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions