8000 [Bug]: colorbar with unattached mappables can't steal space · Issue #23709 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: colorbar with unattached mappables can't steal space #23709
Closed
@QuLogic

Description

@QuLogic

Bug summary

This is something I noticed downstream in networkx: networkx/networkx#5937 (comment)

Formerly, space for a Colorbar was stolen from the current Axes; that was deprecated and now in 3.6, it is stolen from the mappable's Axes. But if the mappable is not added to an Axes, it fails with a somewhat unrelated-looking error.

Code for reproduction

import matplotlib as mpl
import matplotlib.pyplot as plt

cmap = plt.get_cmap('viridis')

pc = mpl.collections.PatchCollection([], cmap=cmap)
pc.set_array([])

plt.colorbar(pc)

Actual outcome

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/elliott/code/matplotlib/lib/matplotlib/pyplot.py", line 2053, in colorbar
    ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kwargs)
  File "/home/elliott/code/matplotlib/lib/matplotlib/figure.py", line 1260, in colorbar
    cax, kwargs = cbar.make_axes(ax, **kwargs)
  File "/home/elliott/code/matplotlib/lib/matplotlib/colorbar.py", line 1396, in make_axes
    fig = parents[0].get_figure()
AttributeError: 'NoneType' object has no attribute 'get_figure'

Expected outcome

Either we should switch to the current Axes if the mappable doesn't have one (probably not desired given the previous change), or raise a clearer error message when this happens.

Additional information

No response

Operating system

No response

Matplotlib Version

3.6.0rc1

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: color/colorbar

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0