Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - matplotlib : figsize for subplots - adding space between rows

I am using the following code to make plots:

   fig, axes = plt.subplots(len(pairs), 3, figsize=(12, 324))
    for i, pair in enumerate(pairs):
        d = pd.DataFrame(columns=[pairs[0], pairs[1]])
        e = df_norm[list(pair)]
        ax0 = axes[i,0]
        ax1 = axes[i,1]
        ax2 = axes[i,2]

        d[pair[0]] = np.random.normal(e[pair[0]],0.02)
        d[pair[1]] = np.random.normal(e[pair[1]],0.02)

    d.plot.scatter(*pair, ax=ax0, c=col0, linewidths=0, s=2, alpha = 0.7)
    d.plot.scatter(*pair, ax=ax1, c=col1, linewidths=0, s=2, alpha = 0.7)
    d.plot.scatter(*pair, ax=ax2, c=col2, linewidths=0, s=2, alpha = 0.7)

fig.tight_layout()

However, my output plot all messed up like:

enter image description here

I try to change figsize=(12, 324) by making 324 bigger or smaller, but neither helps. Is there a way I can put some spaces between each row so the figures won't mess up. Thanks!

like image 332
Edamame Avatar asked Dec 07 '16 01:12

Edamame


People also ask

How do I get rid of the gap between subplots?

We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots.

How do I remove spaces between subplots in Python?

To remove the space between subplots in matplotlib, we can use GridSpec(3, 3) class and add axes as a subplot arrangement.

How do I add more space between subplots?

You can use plt. subplots_adjust to change the spacing between the subplots.

How do I fit a subplot in Matplotlib?

For subplots, this can be done manually by adjusting the subplot parameters using Figure. subplots_adjust . Figure. tight_layout does this automatically.


1 Answers

You need to ommit fig.tight_layout() and instead use subplots_adjust.

plt.subplots_adjust(top = 0.99, bottom=0.01, hspace=1.5, wspace=0.4)

with some very extreme values. hspace is the vertical gap between subplots (most probably in units of the subplot-height).

Figure has the same subplots_adjust method, so you can decide which one to chose.

like image 86
ImportanceOfBeingErnest Avatar answered Oct 10 '22 02:10

ImportanceOfBeingErnest