Update colorbar with new colorizer API by trygvrad · Pull Request #30008 · matplotlib/matplotlib · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 70 additions & 35 deletions lib/matplotlib/colorbar.py
15 changes: 11 additions & 4 deletions lib/matplotlib/colorbar.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ class _ColorbarSpine(mspines.Spines):

class Colorbar:
n_rasterize: int
mappable: cm.ScalarMappable | colorizer.ColorizingArtist
ax: Axes
alpha: float | None
cmap: colors.Colormap
norm: colors.Normalize
values: Sequence[float] | None
boundaries: Sequence[float] | None
extend: Literal["neither", "both", "min", "max"]
Expand All @@ -44,7 +41,7 @@ class Colorbar:
def __init__(
self,
ax: Axes,
mappable: cm.ScalarMappable | colorizer.ColorizingArtist | None = ...,
mappable: cm.ScalarMappable | colorizer.ColorizingArtist | colorizer.Colorizer | None = ...,
*,
cmap: str | colors.Colormap | None = ...,
norm: colors.Normalize | None = ...,
Expand All @@ -64,6 +61,16 @@ class Colorbar:
location: Literal["left", "right", "top", "bottom"] | None = ...
) -> None: ...
@property
def mappable(self) -> cm.ScalarMappable | colorizer.ColorizingArtist : ...
@property
def colorizer(self) -> colorizer.Colorizer : ...
@colorizer.setter
def colorizer(self, colorizer) -> None : ...
@property
def cmap(self) -> colors.Colormap : ...
@property
def norm(self) -> colors.Normalize : ...
@property
def long_axis(self) -> Axis: ...
@property
def locator(self) -> Locator: ...
Expand Down
18 changes: 9 additions & 9 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,17 +1200,17 @@ def colorbar(
Parameters
----------
mappable
The `matplotlib.cm.ScalarMappable` (i.e., `.AxesImage`,
`.ContourSet`, etc.) described by this colorbar. This argument is
mandatory for the `.Figure.colorbar` method but optional for the
`.pyplot.colorbar` function, which sets the default to the current
image.

Note that one can create a `.ScalarMappable` "on-the-fly" to
generate colorbars not attached to a previously drawn artist, e.g.
The `matplotlib.colorizer.ColorizingArtist` (i.e., `.AxesImage`,
`.ContourSet`, etc.) or `matplotlib.colorizer.Colorizer` described
by this colorbar. This argument is mandatory for the
`.Figure.colorbar` method but optional for the `.pyplot.colorbar`
function, which sets the default to the current image.

Note that one can create a `.Colorizer` "on-the-fly" to
generate colorbars not attached an artist, e.g.
::

fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
fig.colorbar(colorizer.Colorizer(norm=norm, cmap=cmap), ax=ax)

cax : `~matplotlib.axes.Axes`, optional
Axes into which the colorbar will be drawn. If `None`, then a new
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class FigureBase(Artist):
) -> Text: ...
def colorbar(
self,
mappable: ScalarMappable | ColorizingArtist,
mappable: ScalarMappable | ColorizingArtist | Colorizer,
cax: Axes | None = ...,
ax: Axes | Iterable[Axes] | None = ...,
use_gridspec: bool = ...,
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py