Matplotlib: how to remove spacing between a group of subplots - python

I have a series of pyplot subplots that I've created using a gridspec. They all have an hspace between them, which is fine, except that I would like to keep three of them without any space. Is there a way to do this? Currently, they look like this:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure()
grid_spec = gridspec.GridSpec(nrows=10, ncols=10)
grid_spec.update(hspace=1.5)
ax1 = plt.subplot(grid_spec[0:4, :])
ax2 = plt.subplot(grid_spec[4:7, :], sharex=ax1)
# I would like to group the next 3 together
# so that they are stacked top to bottom and side by side
ax3 = plt.subplot(grid_spec[7:8, :5])
ax4 = plt.subplot(grid_spec[8:, :5], sharex=ax3)
ax5 = plt.subplot(grid_spec[8:, 5:6], sharey=ax4)
plt.show()
I would like them to be arranged like this so I can plot the following 2-D KDE diagram and have the relevant 1-D diagrams above and to the right (roughly displaying this sort of data crudely drawn in paint):
I appreciate any help with this one. Can't seem to find documentation on this sort of thing. Thanks!

You can use mpl_toolkits.axes_grid1.make_axes_locatable to subdivide the area of a subplot of a 3 x 2 grid.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig = plt.figure()
gs = fig.add_gridspec(nrows=3, ncols=2, hspace=.5,
height_ratios=[4, 3, 3], width_ratios=[7, 4])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :], sharex=ax1)
ax3 = fig.add_subplot(gs[2, 0])
div = make_axes_locatable(ax3)
ax4 = div.append_axes("top", "40%", pad=0.2, sharex=ax3)
ax5 = div.append_axes("right", "25%", pad=0.2, sharey=ax3)
ax4.tick_params(labelbottom=False)
ax5.tick_params(labelleft=False)
plt.show()
Also, you can create a subgridspec, like
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(nrows=3, ncols=2, hspace=.5,
height_ratios=[4, 3, 3], width_ratios=[7, 4])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :], sharex=ax1)
sub_gs = gridspec.GridSpecFromSubplotSpec(2,2, subplot_spec=gs[2,0], hspace=0.3, wspace=0.1,
height_ratios=[1,3], width_ratios=[3,1])
ax3 = fig.add_subplot(sub_gs[1,0])
ax4 = fig.add_subplot(sub_gs[0,0], sharex=ax3)
ax5 = fig.add_subplot(sub_gs[1,1], sharey=ax3)
ax4.tick_params(labelbottom=False)
ax5.tick_params(labelleft=False)
plt.show()
In both cases you will probably want to fine tune the parameters a bit. In general, the matplotlib gridspec tutorial gives a nice overview with many examples on this matter.

Related

Why can't I change the padding on my GridSpecFromSubplotSpec? [duplicate]

I would like to make a variable (two different) wspace using GridSpec in matplotlib.
I would like to achieve the following:
I'm using the following so far:
gs1 = gridspec.GridSpec(6, 3, width_ratios=[1.5,1,1])
gs1.update(wspace=0.4, hspace=0.3)
ax1 = fig.add_subplot(gs1[0:2,0])
ax2 = fig.add_subplot(gs1[2:4,0])
ax3 = fig.add_subplot(gs1[4:6,0])
ax4 = fig.add_subplot(gs1[0:3,1])
ax5 = fig.add_subplot(gs1[3:6,1])
ax6 = fig.add_subplot(gs1[0:3,2])
ax7 = fig.add_subplot(gs1[3:6,2])
Any idea how to obtain the two different space highlighted in green in my amazing hand drawing ?
Thanks a lot !
Sam
You may use 2 GridSpecs, one which contains one column and 3 rows and one which contains two rows and two colums. You can then let the first extend only to less than half of the figure and start the second at half the figure width. The difference between the left and right parameter will be the spacing.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure()
gs1 = GridSpec(3, 1, right=0.4)
gs2 = GridSpec(2, 2, left=0.5)
ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])
plt.show()
The same can be achieved with first defining an "outer" gridspec with two columns and placing an inner gridspec into each of them.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
fig = plt.figure()
gs = GridSpec(1, 2, width_ratios=[1.5,2], wspace=0.3)
gs1 = GridSpecFromSubplotSpec(3, 1, subplot_spec=gs[0])
gs2 = GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])
ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])
plt.show()

Variable wspace with gridspec.GridSpec in python

I would like to make a variable (two different) wspace using GridSpec in matplotlib.
I would like to achieve the following:
I'm using the following so far:
gs1 = gridspec.GridSpec(6, 3, width_ratios=[1.5,1,1])
gs1.update(wspace=0.4, hspace=0.3)
ax1 = fig.add_subplot(gs1[0:2,0])
ax2 = fig.add_subplot(gs1[2:4,0])
ax3 = fig.add_subplot(gs1[4:6,0])
ax4 = fig.add_subplot(gs1[0:3,1])
ax5 = fig.add_subplot(gs1[3:6,1])
ax6 = fig.add_subplot(gs1[0:3,2])
ax7 = fig.add_subplot(gs1[3:6,2])
Any idea how to obtain the two different space highlighted in green in my amazing hand drawing ?
Thanks a lot !
Sam
You may use 2 GridSpecs, one which contains one column and 3 rows and one which contains two rows and two colums. You can then let the first extend only to less than half of the figure and start the second at half the figure width. The difference between the left and right parameter will be the spacing.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure()
gs1 = GridSpec(3, 1, right=0.4)
gs2 = GridSpec(2, 2, left=0.5)
ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])
plt.show()
The same can be achieved with first defining an "outer" gridspec with two columns and placing an inner gridspec into each of them.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
fig = plt.figure()
gs = GridSpec(1, 2, width_ratios=[1.5,2], wspace=0.3)
gs1 = GridSpecFromSubplotSpec(3, 1, subplot_spec=gs[0])
gs2 = GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])
ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])
plt.show()

Adjusting space in-between subplots

I am plotting 4 subplots in one figure, and I want to adjust the space in-between evenly.
I tried grid.GridSpec.update.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.figure(figsize=(8,8))
gs2 = gridspec.GridSpec(2, 2)
gs2.update(wspace=0.01, hspace=0.01)
ax1 = plt.subplot(gs2[0,0],aspect='equal')
ax1.imshow(img)
ax1.axis('off')
ax2 = plt.subplot(gs2[0,1],aspect='equal')
ax2.imshow(img)
ax2.axis('off')
ax3 = plt.subplot(gs2[1,0],aspect='equal')
ax3.imshow(img)
ax3.axis('off')
ax4 = plt.subplot(gs2[1,1],aspect='equal')
ax4.imshow(img)
ax4.axis('off')
The vertical space in-between 2 plots is too big, and it does not change no matter how I adjust gs2.update(hspace= ), as shown below:
It's likely your aspect='equal' that's causing the problem.
Try this
import numpy as np
%matplotlib inline # if in a jupyter notebook like environment
img = np.ones((30, 30))
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(8,8),
gridspec_kw={'wspace': 0.01, 'hspace': 0.01})
axes = axes.ravel()
for ax in axes:
# aspect : ['auto' | 'equal' | scalar], optional, default: None
ax.imshow(img, aspect='auto')
ax.axis('off')

Python Plot two subplots

I have a set of 12 plots that I want to save as one. The first set is a 3x3 subplot of 9 segments and the 2nd part is a 2x2 subplot of 4 segments.
I've tried to add a subplot(121) between these two and I have tried to use figure(1) and figure(2), but both don't met me save the two images as one big image. Is there an easy way of doing this?
plt.subplot(331)
plt.imshow(getpoly(seg1),origin="lower")
plt.subplot(332)
plt.imshow(getpoly(seg2),origin="lower")
plt.subplot(333)
plt.imshow(getpoly(seg3),origin="lower")
plt.subplot(334)
plt.imshow(getpoly(seg4),origin="lower")
plt.subplot(335)
plt.imshow(getpoly(seg5),origin="lower")
plt.subplot(336)
plt.imshow(getpoly(seg6),origin="lower")
plt.subplot(337)
plt.imshow(getpoly(seg7),origin="lower")
plt.subplot(338)
plt.imshow(getpoly(seg8),origin="lower")
plt.subplot(339)
plt.imshow(getpoly(seg9),origin="lower")
plt.subplot(221)
plt.imshow(h1,origin="lower")
plt.colorbar()
plt.subplot(222)
plt.imshow(h2,origin="lower")
plt.colorbar()
plt.subplot(223)
plt.imshow(getpoly(h2),origin="lower")
plt.colorbar()
plt.subplot(224)
plt.imshow(h1-getpoly(h2),origin="lower")
plt.colorbar()
You would probably want to use gridspec with GridSpecFromSubplotSpec as shown here.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])
fig = plt.figure()
for i in range(9):
ax = fig.add_subplot(gs0[i//3, i%3])
ax.imshow(np.random.rand(4,4))
ax.set_xticks([]); ax.set_yticks([])
for i in range(4):
ax = fig.add_subplot(gs1[i//2, i%2])
ax.imshow(np.random.rand(4,4))
ax.set_xticks([]); ax.set_yticks([])
plt.show()

matplotlib GridSpec indexing yields unexpected results

I have a subplot arrangement that I am trying to tame with matplotlib.gridspec. Following the example HERE, I came up with the following for my plot:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
f = plt.figure()
gs1 = gridspec.GridSpec(1,1)
ax1 = plt.subplot(gs1[:,:])
gs2 = gridspec.GridSpec(1,2)
ax2 = plt.subplot(gs2[:,0])
ax3 = plt.subplot(gs2[:,1])
plt.show()
Where I am expecting to get three subplots, I get this:
How do I get the following result?:
See this example (copied almost verbatim):
fig = plt.figure()
gs1 = gridspec.GridSpec(1,1)
gs1.update(left=0.05, right=0.33, wspace=0.05)
ax1 = fig.add_subplot(gs1[:,:])
gs2 = gridspec.GridSpec(1,2)
gs2.update(left=0.38, right=0.98, wspace=0.05)
ax2 = fig.add_subplot(gs2[:,0])
ax3 = fig.add_subplot(gs2[:,1])
plt.show()

Categories

Resources