Skip to content

colorbar(formatter="log") doesn't work #506

@munechika-koyo

Description

@munechika-koyo

Hello,

When I tried to create a 2-D plot with a colorbar and a logarithmic tick formatter like:

import numpy as np
import ultraplot as uplt

# Sample data
N = 20
state = np.random.RandomState(51423)
data = 11 ** (0.25 * np.cumsum(state.rand(N, N), axis=0))

# Create figure
fig, ax = uplt.subplots()
m = ax.pcolormesh(data, cmap="magma", norm="log")
ax.colorbar(m, formatter="log")

then, I ran into the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 12
     10 fig, ax = uplt.subplots()
     11 m = ax.pcolormesh(data, cmap="magma", norm="log")
---> 12 ax.colorbar(m, formatter="log")

File .../lib/python3.12/site-packages/ultraplot/axes/base.py:3602, in Axes.colorbar(self, mappable, values, loc, location, **kwargs)
   3600     self._register_guide("colorbar", (mappable, values), (loc, align), **kwargs)
   3601 else:
-> 3602     return self._add_colorbar(mappable, values, loc=loc, align=align, **kwargs)

File .../lib/python3.12/site-packages/ultraplot/internals/warnings.py:118, in _rename_kwargs.<locals>._decorator.<locals>._deprecate_kwargs_wrapper(*args, **kwargs)
    113         key_new = key_new.format(value)
    114     _warn_ultraplot(
    115         f"Keyword {key_old!r} was deprecated in version {version} and may "
    116         f"be removed in {next_release()}. Please use {key_new!r} instead."
    117     )
--> 118 return func_orig(*args, **kwargs)

File .../lib/python3.12/site-packages/ultraplot/axes/base.py:1275, in Axes._add_colorbar(self, mappable, values, loc, align, space, pad, width, length, span, row, col, rows, cols, shrink, label, title, reverse, rotation, grid, edges, drawedges, extend, extendsize, extendfrac, ticks, locator, locator_kw, format, formatter, ticklabels, formatter_kw, minorticks, minorlocator, minorlocator_kw, tickminor, ticklen, ticklenratio, tickdir, tickdirection, tickwidth, tickwidthratio, ticklabelsize, ticklabelweight, ticklabelcolor, labelloc, labellocation, labelsize, labelweight, labelcolor, c, color, lw, linewidth, edgefix, rasterized, outline, labelrotation, center_levels, **kwargs)
   1273 formatter = _not_none(formatter, getattr(norm, "_labels", None), "auto")
   1274 formatter_kw.setdefault("tickrange", (norm.vmin, norm.vmax))
-> 1275 formatter = constructor.Formatter(formatter, **formatter_kw)
   1276 categorical = isinstance(formatter, mticker.FixedFormatter)
   1277 if locator is not None:

File .../lib/python3.12/site-packages/ultraplot/constructor.py:1274, in Formatter(formatter, date, index, *args, **kwargs)
   1272     formatter = cls(formatter, *args, **kwargs)
   1273 elif formatter in FORMATTERS:
-> 1274     formatter = FORMATTERS[formatter](*args, **kwargs)
   1275 else:
   1276     raise ValueError(
   1277         f"Unknown formatter {formatter!r}. Options are: "
   1278         + ", ".join(map(repr, FORMATTERS))
   1279         + "."
   1280     )

TypeError: LogFormatter.__init__() got an unexpected keyword argument 'tickrange'

When I used the instance of matplotlib.ticker.LogFormatterSciNotation instead of "log", i.e.,:

ax.colorbar(m, formatter=LogFormatterSciNotation())

then, the code ran successfully, resulting in the following figure:

Image

Version

python: 3.12
ultraplot: 1.70.0
matplotlib: 3.10.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions