1

I want to set a colorbar at the same height with multiple heatmaps with matplotlib.

I found a solution when using a single heatmap (link), but it doesn't work with multiple heatmaps.

Here is my code.

import matplotlib; matplotlib.use('agg')
import matplotlib.pyplot as plt
import sys
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

x = np.random.rand(240,240)
y = np.random.rand(240,240)
t = np.random.rand(240,240)
fig, ax = plt.subplots(ncols=3)
axs = [ax[0], ax[1], ax[2]]
ds = [x,y,t]
titles = ["x", "y", "t"]
for i in range(3):
    axs[i].axis('off')
    im = axs[i].imshow(ds[i])
    axs[i].set_title(titles[i])
fig.colorbar(im,ax=axs)
plt.savefig("aaa.png")
plt.clf()

Here is "aaa.png".

enter image description here

sai
  • 55
  • 5
  • 1
    The most obvious things are sometimes overly complicated in matplotlib. Take a look at this [demo](https://matplotlib.org/gallery/axes_grid1/demo_edge_colorbar.html#sphx-glr-gallery-axes-grid1-demo-edge-colorbar-py). – MaxPowers Feb 03 '19 at 08:25
  • @TomdeGeus Not duplicate. In my case, three heatmaps share one colorbar, but in the case of the link two heatmaps don't share colorbar. – sai Feb 03 '19 at 16:28

0 Answers0