Wx_Trader
Wx_Trader

Reputation: 135

Move one subplot down

I am making a 4 panel plot and was wondering if there is any way to just move the subplot in the bottom left down a bit so it stays in line with the rest of the plots and the titles don't overlay. I believe it is because the bottom left plot doesn't have a colorbar but I'm not sure how I would fix that. I am using the add subplot function. So for the bottom left pot the axes looks like this. ax = fig.add_subplot(2,2,3,projection=proj) enter image description here

Upvotes: 0

Views: 1994

Answers (2)

Stef
Stef

Reputation: 30589

You can use inset_axes in combination with constrained layout:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, 2, layout='constrained')
for i, ax in enumerate(axs.flat):
    pcm = ax.pcolormesh(np.random.random((20, 20)) * (i + 1))
    ax.set_title("very long title\nthat strechtes over two lines")
    if i != 2:
        cax = ax.inset_axes([0, -0.35, 1, 0.1])
        fig.colorbar(pcm, ax=ax, cax=cax, orientation='horizontal')

enter image description here

See also Placing Colorbars.

Upvotes: 1

Matt Hall
Matt Hall

Reputation: 8142

I think it might work to read the current position with ax.get_position(), then move it down a bit with ax.set_position(). Something like this:

fig, axs = plt.subplots(2, 2)

pos = axs[1, 0].get_position()
new_pos = [pos.x0, pos.y0-0.1, pos.width, pos.height]
axs[1, 0].set_position(new_pos)

This results in:

The output of this code

As for 'how much?'... Not sure if there's a better way than trial and error though. Not ideal.

Upvotes: 1

Related Questions