pyplot: Histogram on X/Y axis

Posted by Moser on 23 Mar 2018

Last week a colleague asked me for help on a figure he wanted to plot using pyplot. He had a couple of line plots and an intersecting line and wanted to show the histogram of the intersection points on both the X and Y axis.

I found a neat way of acomplishing such a plot using a GridSpec.

Here is the code, using a regular scatter plot as the central element, but as every part of the figure is filled separately you can swap any of them out. You have to make sure though that the histogram plots and the main plot have the same X and Y axes respectively.

I remove the axes from the main plot (set_axis_off()) because they looked redundant. tight_layout is used to remove the gaps between the subplots. You can tweak width_ratios and height_ratios to change how much space is allocated to the histograms and the main plot.