I want to plot an image and colorbar with its associated histogram below. The two axes of the image and the histogram must have the same width. Furthermore, the colorbar should be the same height as the image. The part that is (and should not) be complicated is to superpose a plot of a cumulative histogram with the percentage of each bin in respect to the size of the data.
For the moment, I obtained something like this:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = np.random.normal(0,2,size=(100,100))
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
im = ax.imshow(data,cmap="bone")
divider = make_axes_locatable(ax)
ax1 = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im,cax=ax1)
ax2 = divider.append_axes("bottom",size="100%",pad = 0.3)
n,bins,patches = ax2.hist(data.flatten(),bins=20)
ax3 = ax2.twinx()
ax3.plot(bins[:-1],np.cumsum(n*100/np.size(data)),lw=2)
plt.show()
Everything is going smoothly until I try to use twinx on ax2 (in order to plot my cumulative distribution on ax3 with a different y-scale). The resulting axis, instead of being with ax2, is wrapping all the axes of the figure.
I don't understand what is wrong and how I can fix this.
