Python - Stacking two histograms with a scatter plot
<p>Having an example code for a scatter plot along with their histograms </p>

<pre><code>x = np.random.rand(5000,1)
y = np.random.rand(5000,1)



fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)
ax.scatter(x, y, facecolors='none')
ax.set_xlim(0,1)
ax.set_ylim(0,1)



fig1 = plt.figure(figsize=(7,7))
ax1 = fig1.add_subplot(111)
ax1.hist(x, bins=25, fill = None, facecolor='none', 
        edgecolor='black', linewidth = 1)



fig2 = plt.figure(figsize=(7,7))
ax2 = fig2.add_subplot(111)
ax2.hist(y, bins=25 , fill = None, facecolor='none', 
        edgecolor='black', linewidth = 1)
</code></pre>

<p>What I'm wanting to do is to create this graph with the histograms attached to their respected axis almost like this example</p>

<p><a href="http://i.stack.imgur.com/ZIcFZ.png" rel="nofollow"><img src="http://i.stack.imgur.com/ZIcFZ.png" alt="enter image description here"></a></p>

<p>I'm familiar with stacking and merging the x-axis</p>

<pre><code>f, (ax1, ax2, ax3) = plt.subplots(3)
ax1.scatter(x, y)
ax2.hist(x, bins=25, fill = None, facecolor='none', 
        edgecolor='black', linewidth = 1)
ax3.hist(y, bins=25 , fill = None, facecolor='none', 
        edgecolor='black', linewidth = 1)

f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
</code></pre>

<p><a href="http://i.stack.imgur.com/doW0u.png" rel="nofollow"><img src="http://i.stack.imgur.com/doW0u.png" alt="enter image description here"></a></p>

<p>But I have no idea how to attach the histograms to the y axis and x axis like in the picture I posted above, and on top of that, how to vary the size of the graphs (ie make the scatter plot larger and the histograms smaller in comparison)</p>

<p>Seaborn is the way to go for quick statistical plots. But if you want to avoid another dependency you can use <a href="http://matplotlib.org/users/gridspec.html" rel="nofollow" title="subplot2grid"><code>subplot2grid</code></a> to place the subplots and the keywords <code>sharex</code> and <code>sharey</code> to make sure the axes are synchronized.</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(100)
y = np.random.randn(100)

scatter_axes = plt.subplot2grid((3, 3), (1, 0), rowspan=2, colspan=2)
x_hist_axes = plt.subplot2grid((3, 3), (0, 0), colspan=2,
                               sharex=scatter_axes)
y_hist_axes = plt.subplot2grid((3, 3), (1, 2), rowspan=2,
                               sharey=scatter_axes)

scatter_axes.plot(x, y, '.')
x_hist_axes.hist(x)
y_hist_axes.hist(y, orientation='horizontal')
</code></pre>

<p><a href="http://i.stack.imgur.com/ZtA7q.png" rel="nofollow"><img src="http://i.stack.imgur.com/ZtA7q.png" alt="plot"></a></p>

<p>You should always look at the <a href="http://matplotlib.org/gallery.html" rel="nofollow" title="Matplotlib gallery">matplotlib gallery</a> before asking how to plot something, chances are that it will save you a few keystrokes -- I mean you won't have to ask. There are actually two plots like this in the gallery. Unfortunately the code is old and does not take advantage of <code>subplot2grid</code>, <a href="http://matplotlib.org/examples/pylab_examples/scatter_hist.html" rel="nofollow">the first one</a> uses rectangles and the <a href="http://matplotlib.org/examples/axes_grid/scatter_hist.html" rel="nofollow">second one</a> uses <code>axes_grid</code>, which is a somewhat weird beast. That's why I posted this answer.</p>

python numpy matplotlib plot