I would like to display a 2D np.array
with imshow
and the respective colorbar which should share its axis with a histogram of the np.array
. Here is an attempt, however, without shared axes.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, ax = plt.subplots(figsize=(7,10))
data = np.random.normal(0, 0.2, size=(100,100))
cax = ax.imshow(data, interpolation='nearest', cmap=cm.jet)
divider = make_axes_locatable(plt.gca())
axBar = divider.append_axes("bottom", '5%', pad='7%')
axHist = divider.append_axes("bottom", '30%', pad='7%')
cbar = plt.colorbar(cax, cax=axBar, orientation='horizontal')
axHist.hist(np.ndarray.flatten(data), bins=50)
plt.show()
I tried to use the sharex
argument in axHist
with axHist = divider.append_axes("bottom", '30%', pad='7%', sharex=axBar)
but this somehow shifts the histogram data:
Besides the shared axis x, how could one modify the histogram to take the same colors as the colormap, similar to here?