Skip to content

[Bug]: Hexbin hbar and vbar not updating correctly on changed #28633

Open
@trygvrad

Description

@trygvrad

Bug summary

A hexbar with marginals=True has independent norms on the hexbins and the hbar and vbar unless vmin/vmax is set manually.
However, if the a changed signal is called, the behavior changes, and vmin/vmax is updated on the hbar and vbar to be equal to the vmin/vmax on main.

Aditionally, there is a bug so that the

Code for reproduction

# based on test_axes.py -- test_hexbin_linear()
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

fig, axes = plt.subplots(2, 1, figsize=(9, 9))
cs = axes[0].hexbin(x, y, gridsize=(10, 5), marginals=True,
          reduce_C_function=np.sum)

fig.colorbar(cs.hbar).set_label('hbar')
fig.colorbar(cs.vbar).set_label('vbar')
fig.colorbar(cs).set_label('main')


cs = axes[1].hexbin(x, y, gridsize=(10, 5), marginals=True,
          reduce_C_function=np.sum)
cs.set_cmap('rainbow')

fig.colorbar(cs.hbar).set_label('hbar')
fig.colorbar(cs.vbar).set_label('vbar')
fig.colorbar(cs).set_label('main')

Actual outcome

image
Only two of three colorbars change, and the limits are changed on the vbar.

Expected outcome

All three colorbars in the lower plot should change, but the limits should not update

Additional information

This bug came up as I was looking at this code in relation to the new data→color pipeline suggested in #28428.
I can find no report on this bug by actual users, so I don't think it needs a quick resolution.

  • If the new data→color pipeline discussed in VectorMappable with data type objects #28428 is approved, this bug will be resolved as part of this.
  • If the new data→color pipeline is not approved, a small bugfix for this issue could be made (good first issue?)

The relevant code is in _axes.py, in the function hexbin() which includes

        def on_changed(collection):
            collection.hbar.set_cmap(collection.get_cmap())
            collection.hbar.set_cmap(collection.get_cmap())
            collection.vbar.set_clim(collection.get_clim())
            collection.vbar.set_clim(collection.get_clim())
        collection.callbacks.connect('changed', on_changed)

where hbar and vbar is used in the wrong order.
However, only updating this will not resolve the issue where changing the colorbar changes the limits on the hbar and vbar in the case where these limits have not been set.

Operating system

No response

Matplotlib Version

3.8.2, matplotlib-dev

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

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