Note
Go to the endto download the full example code.
Scatter plot with histograms#
Add histograms to the x-axes and y-axes margins of a scatter plot.
This layout features a central scatter plot illustrating the relationshipbetween x and y, a histogram at the top displaying the distribution of x, and ahistogram on the right showing the distribution of y.
For a nice alignment of the main Axes with the marginals, two options are shownbelow:
WhileAxes.inset_axes may be a bit more complex, it allows correct handlingof main Axes with a fixed aspect ratio.
Let us first define a function that takes x and y data as input, as well asthree Axes, the main Axes for the scatter, and two marginal Axes. It will thencreate the scatter and histograms inside the provided Axes.
importmatplotlib.pyplotaspltimportnumpyasnp# Fixing random state for reproducibilitynp.random.seed(19680801)# some random datax=np.random.randn(1000)y=np.random.randn(1000)defscatter_hist(x,y,ax,ax_histx,ax_histy):# no labelsax_histx.tick_params(axis="x",labelbottom=False)ax_histy.tick_params(axis="y",labelleft=False)# the scatter plot:ax.scatter(x,y)# now determine nice limits by hand:binwidth=0.25xymax=max(np.max(np.abs(x)),np.max(np.abs(y)))lim=(int(xymax/binwidth)+1)*binwidthbins=np.arange(-lim,lim+binwidth,binwidth)ax_histx.hist(x,bins=bins)ax_histy.hist(y,bins=bins,orientation='horizontal')
Defining the Axes positions using subplot_mosaic#
We use thesubplot_mosaic function to define the positions andnames of the three axes; the empty axes is specified by'.'. We manuallyspecify the size of the figure, and can make the different axes havedifferent sizes by specifying thewidth_ratios andheight_ratiosarguments. Thelayout argument is set to'constrained' to optimize thespacing between the axes.

Defining the Axes positions using inset_axes#
inset_axes can be used to position marginalsoutside the mainAxes. The advantage of doing so is that the aspect ratio of the main Axescan be fixed, and the marginals will always be drawn relative to the positionof the Axes.
# Create a Figure, which doesn't have to be square.fig=plt.figure(layout='constrained')# Create the main Axes.ax=fig.add_subplot()# The main Axes' aspect can be fixed.ax.set_aspect('equal')# Create marginal Axes, which have 25% of the size of the main Axes. Note that# the inset Axes are positioned *outside* (on the right and the top) of the# main Axes, by specifying axes coordinates greater than 1. Axes coordinates# less than 0 would likewise specify positions on the left and the bottom of# the main Axes.ax_histx=ax.inset_axes([0,1.05,1,0.25],sharex=ax)ax_histy=ax.inset_axes([1.05,0,0.25,1],sharey=ax)# Draw the scatter plot and marginals.scatter_hist(x,y,ax,ax_histx,ax_histy)plt.show()

While we recommend using one of the two methods described above, there arenumber of other ways to achieve a similar layout:
The Axes can be positioned manually in relative coordinates using
add_axes.A gridspec can be used to create the layout(
add_gridspec) and adding only the three desiredaxes (add_subplot).Four subplots can be created using
subplots, and the unusedaxes in the upper right can be removed manually.The
axes_grid1toolkit can be used, as shown inAlign histogram to scatter plot using locatable Axes.
References
The use of the following functions, methods, classes and modules is shownin this example:
Tags:component: axesplot-type: scatterplot-type: histogramlevel: intermediate
Total running time of the script: (0 minutes 1.575 seconds)