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>I think it's hard to do this solely with <code>matplotlib</code> but you can use <code>seaborn</code> which has <code>jointplot</code> function. </p>

<pre><code>import numpy as np
import pandas as pd
import seaborn as sns
sns.set(color_codes=True)

x = np.random.rand(1000,1)
y = np.random.rand(1000,1)
data = np.column_stack((x,y))
df = pd.DataFrame(data, columns=["x", "y"])

sns.jointplot(x="x", y="y", data=df);
</code></pre>

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

python numpy matplotlib plot