0
votes

Can someone share an example to create 4 scatter hist plots as a subplot? To clarify. I am planning to create a pdf of plots. Each page will have 4 subplots. Each subplot being the scatter histogram.

The example of creating scatter histogram seems to be this

Would there be any alternate functions to do this in fewer lines than using this scatter plot example and sub-plotting each of them ?

1
What exact problem do you face? Are you able to create 4 subplots? (fig, axs = plt.subplots(2,2)) Then use the example for each of the four axes in axs? - ImportanceOfBeingErnest
Apologies for lack of clarity. i was wondering if there would be a shorter way than wrapping up the example into function and creating subplots from them. If there was already a subplot_hist function in some library or in matplotlib as one exists in matlab. - whisperer
Seaborn has a jointplot function, but that explicitely does not allow to be used in subplots. But I don't see the problem of placing a def function in front of the code from the example. - ImportanceOfBeingErnest
You can also look at these alternatives coming from a not yet published documentation update of mine. They simplify creating a single such scatter histogram; but are not well suited to create subplots.That is to say, you already found the best option in the docs using the axes_grid1 approach. - ImportanceOfBeingErnest

1 Answers

2
votes

Using the linked example, all you need to do is increase the number of subplots.

Then for each subplot, you go through the example code to make each one a scatter histogram.

I've pasted a toy example below:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, axes = plt.subplots(figsize=(10,10),nrows=2, ncols=2)
print(axes)
colors = ['r','b','g','m']

for row in axes:
    for axScatter in row:
        print()
        x = np.random.randn(1000)
        y = np.random.randn(1000)
        # the scatter plot:
        # gets color from the end ('m' will be first)
        color = colors.pop()
        axScatter.scatter(x, y,color = color)
        axScatter.set_aspect(1.)

        # create new axes on the right and on the top of the current axes
        # The first argument of the new_vertical(new_horizontal) method is
        # the height (width) of the axes to be created in inches.
        divider = make_axes_locatable(axScatter)
        axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
        axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)

        # make some labels invisible
        axHistx.xaxis.set_tick_params(labelbottom=False)
        axHisty.yaxis.set_tick_params(labelleft=False)

        # now determine nice limits by hand:
        binwidth = 0.25
        xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
        lim = (int(xymax/binwidth) + 1)*binwidth

        bins = np.arange(-lim, lim + binwidth, binwidth)
        axHistx.hist(x, bins=bins,color=color)
        axHisty.hist(y, bins=bins, orientation='horizontal',color=color)

        # the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
        # thus there is no need to manually adjust the xlim and ylim of these
        # axis.

        axHistx.set_yticks([0, 50, 100])

        axHisty.set_xticks([0, 50, 100])

plt.show()