1

I'm trying to plot 3 simple heatmaps, as shown in the code. However, 2 issues appear:

  1. Color bars does not appear in part of the heatmaps.
  2. The resulting plot is very small, and not in the usual proportion (see attached picture).

For your information: Seaborn version: 0.11.0. Matplotlib version: 3.3.4

Here is my code:

dates=pd.date_range(start='2019-01-01', periods=2, freq='D')
d0=pd.DataFrame([[np.nan, 0.1],[np.nan,0.3]], columns=dates)
d1=pd.DataFrame([[np.nan, 0],[np.nan, 0]], columns=dates)
d2=pd.DataFrame([[67,68]], columns=dates) 
DATA=[d0,d1,d2]

fig, axs = plt.subplots(ncols=2, nrows=3, sharex='col', figsize=(20, 20), 
                            gridspec_kw={'width_ratios': [1, 15]})    

for k, df in enumerate(DATA):     
    (ax0,ax1)=axs[k]
    sns.heatmap(df, annot=False, cmap=cmap, cbar_ax=ax0, ax=ax1)

And the result: enter image description here

Amit V
  • 171
  • 2
  • 10
  • I'm unsure about the exact reasons, but your problem seems to be caused by `sharex='col'` in `plt.subplots` not working for the color bars. See [here](https://stackoverflow.com/questions/42973223/how-to-share-x-axes-of-two-subplots-after-they-have-been-created) about sharing only the right side axes. – JohanC Jul 30 '21 at 08:54

1 Answers1

1

In this case, you don't need to force your color bar and heatmap to have the same x-axis, so if you remove the sharex='col' it should work out:

fig, axs = plt.subplots(ncols=2, nrows=3, figsize=(20, 20), 
                            gridspec_kw={'width_ratios': [1, 15]})    

for k, df in enumerate(DATA):     
    ax0,ax1=axs[k]
    sns.heatmap(df, annot=False, cmap=cmap, cbar_ax=ax0, ax=ax1)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Interesting! But in my case, the x axis of the right side are identical. Is there a solution which keeps the shared axis as well (at least for right side)? Maybe something like this link? https://stackoverflow.com/questions/42973223/how-to-share-x-axes-of-two-subplots-after-they-have-been-created – Amit V Jul 30 '21 at 10:37
  • you can see the answer by @TheDude in https://stackoverflow.com/questions/23528477/share-axes-in-matplotlib-for-only-part-of-the-subplots – StupidWolf Jul 30 '21 at 10:43
  • for your case with heatmaps, i think it's easier to define your plots with the same x-axis before adding it on – StupidWolf Jul 30 '21 at 10:44