0

I have followed the answer on this thread Seaborn Heatmap: Move colorbar on top of the plot regarding positioning a colorbar on a figure.

However I would like to shrink the colorbar to 50% of the default. If I read the docstring for the colorbar function it suggests I can pass the keyword argument "shrink".

This is the code I've written (where HM is a seaborn heatmap):

from mpl_toolkits.axes_grid1.colorbar import colorbar

HM_divider = make_axes_locatable(HM)
# define size and padding of axes for colorbar
cax = HM_divider.append_axes('right', size = '5%', pad = '25%')
# make colorbar for heatmap. 
# Heatmap returns an axes obj but you need to get a mappable obj (get_children)
colorbar(HM.get_children()[0], cax = cax, orientation = 'vertical', shrink=0.5)

But when I run this I get a TypeError: TypeError: __init__() got an unexpected keyword argument 'shrink'

BMichell
  • 3,581
  • 5
  • 23
  • 31
  • [this](https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/colorbar_placement.html) would be helpful. What version of `matplotlib` you're running ? – Jay Patel Feb 10 '21 at 13:48
  • Hi Jay. Version is 3.1.3 – BMichell Feb 10 '21 at 15:22
  • 2
    Don’t use make_axes_locatable or for this. Use ax.inset_axes and don’t use the shrink parameter just make the inset axes the size you want. – Jody Klymak Feb 10 '21 at 15:36
  • Thanks Jody - understand that option. But I'm still confused why it won't take the shrink parameter in this case – BMichell Feb 10 '21 at 15:43
  • 1
    If you write `colorbar()`, from which library does it come? If it comes from `matplotlib.pyplot`, please avoid confusion and write it as `plt.colorbar()`. – JohanC Feb 10 '21 at 18:01
  • apologies - import was explained in the linked question. Have edited my question now to include that. – BMichell Feb 11 '21 at 09:15

1 Answers1

1

Looking at the post linked, I am guessing this is the steps you tried:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.colorbar import colorbar

df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])

HM = sns.heatmap(df, cbar = False)
HM_divider = make_axes_locatable(HM)
cax = HM_divider.append_axes('right', size = '5%', pad = '25%')
colorbar(HM.get_children()[0], cax = cax, orientation = 'vertical', shrink=0.5)

I get the same error:

TypeError: __init__() got an unexpected keyword argument 'shrink'

Because mpl_toolkits.axes_grid1.colorbar does not have the option shrink. It is from plt.colorbar as raised in the comments. And it might be better to switch to this because the mpl_toolkits.axes_grid1.colorbar module and its colorbar implementation are deprecated in favor of matplotlib.colorbar. So something like this should go:

fig,ax = plt.subplots()
sns.heatmap(df, cbar = False,ax=ax)
fig.colorbar(ax.get_children()[0],orientation = 'vertical', shrink=0.5)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72