'Seaborn mixing up colorbar with axis divider?

Consider this MWE:

import seaborn
import matplotlib
import numpy as np
from matplotlib import pyplot as plt 
from mpl_toolkits.axes_grid1 import make_axes_locatable

a = np.random.randn(int(1e3))
b = np.random.rand(int(1e3))

vmin = -10
vmax = 10

seaborn.histplot(x=a, y=b, cmap='PuBu_r', cbar=True, vmin=vmin, vmax=vmax)
plt.show()
plt.close()

ax = seaborn.histplot(x=a, y=b)
# Add colorbar with help of axis divier. 
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap='PuBu_r', norm=norm)
sm.set_array([])
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
plt.colorbar(sm, cax=cax)
plt.show()
plt.close()

Naively, I'd expect both histograms to look alike, with the only difference being that the first histogram should be somewhat smaller than the second one, since the second histogram puts the colorbar with an axis divider.

However, the output is:

enter image description here enter image description here

To be honest, I cannot explain the difference in the color scheme between the two histograms to myself. Isn't seaborn (or matplotlib) mixing things up here?

EDIT: As was pointed out in the comments, putting vmin and vmax explicitly into the seaborn.histplot() calls fixed the problem. However, when comparing the output of

seaborn.histplot(x=a, y=b, cmap='PuBu_r', cbar=False, vmin=vmin, vmax=vmax)

and

ax = seaborn.histplot(x=a, y=b)
# Add colorbar with help of axis divier. 
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap='PuBu_r', norm=norm)
sm.set_array([])
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
plt.colorbar(sm, cax=cax)

I noticed that the second image (leaving out the colorbar) does not have the same size as the first image:

enter image description here

enter image description here

Is there any way to enforce this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source