Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Stacking two histograms with a scatter plot

Having an example code for a scatter plot along with their histograms

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)

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

enter image description here

I'm familiar with stacking and merging the x-axis

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)

enter image description here

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)

like image 657
iron2man Avatar asked Sep 27 '16 16:09

iron2man


People also ask

How do you overlay two histograms in Python?

To make multiple overlapping histograms, we need to use Matplotlib pyplot's hist function multiple times. For example, to make a plot with two histograms, we need to use pyplot's hist() function two times. Here we adjust the transparency with alpha parameter and specify a label for each variable.

How do you show multiple histograms on the same axes?

In order to create two histograms on the same plot, similar to the PLOTYY function, you need to create overlapping axes and then plot each histogram on one axis. 1.In order to create overlapping axes, simply create one axes and then create another axes with the same position as the first.


1 Answers

Seaborn is the way to go for quick statistical plots. But if you want to avoid another dependency you can use subplot2grid to place the subplots and the keywords sharex and sharey to make sure the axes are synchronized.

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')

plot

You should always look at the matplotlib gallery 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 subplot2grid, the first one uses rectangles and the second one uses axes_grid, which is a somewhat weird beast. That's why I posted this answer.

like image 159
Stop harming Monica Avatar answered Nov 13 '22 14:11

Stop harming Monica