I have a grid of subplots created using gridspec. I know how to create subplots that span rows and columns in gridspec. Is it possible to reduce the width of a single sub-plot just by a small amount? For example, can we set the width ratio for a single subplot? The way I want it is marked in red in the image.
My code looks like this:
JavaScript
x
19
19
1
import matplotlib.gridspec as gridspec
2
import matplotlib.pyplot as plt
3
import numpy as np
4
5
plt.figure(figsize=(6, 4))
6
gs = gridspec.GridSpec(3, 5, height_ratios=[0.5,1,1])
7
8
for i in range(1, 3):
9
for j in range(5):
10
ax = plt.subplot(gs[i, j])
11
12
ax1 = plt.subplot(gs[0,1:2])
13
ax2 = plt.subplot(gs[0,2:])
14
15
for ax in [ax1, ax2]:
16
ax.tick_params(size=0)
17
ax.set_xticklabels([])
18
ax.set_yticklabels([])
19
What I tried:
I tried setting the width ratio as width_ratios = [1,1,1,1,0.5]
, but that reduces the width of the whole column (last column).
Advertisement
Answer
Thank you @JodyKlymak for mentioning about ax.set_postion
method. @mozway provided a working solution, but adding these few lines in my code gave me the desired output:
JavaScript
1
8
1
bb = ax2.get_position()
2
bb.x1 = 0.84
3
ax2.set_position(bb)
4
5
bb = ax2.get_position()
6
bb.x0 = 0.50
7
ax2.set_position(bb)
8