[英]Matplotlib. Align subplot with Bbox
I have a grid of 6 subplots.我有一个由 6 个子图组成的网格。 The last subplot is square.
最后一个子图是方形的。 I need to move it to the left so that its left y-axis is perfectly aligned with the y-axis of the plot above it.
我需要将其向左移动,以便其左侧 y 轴与其上方绘图的 y 轴完美对齐。 I know I can get and set the Bbox parameters, but I can't move it in the right direction.
我知道我可以获取和设置 Bbox 参数,但我无法将其朝正确的方向移动。 Whether I increase or decrease x1 and x2, it always seems to move to the right.
无论我增加还是减少 x1 和 x2,它似乎总是向右移动。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 3)
fig.tight_layout(pad=3.0)
fig.set_figwidth(30)
fig.set_figheight(15)
axs[1, 2].set_aspect('equal')
print(axs[1, 2].get_position())
The print command returns:打印命令返回:
Bbox(x0=0.7620938740079364, y0=0.16765873015873023, x1=0.8925502232142856, y1=0.4285714285714286)
The .get_position()
method indeed returns the bounding box of given axes. .get_position()
方法确实返回给定轴的边界框。 The idea is to use the Bbox
of the axes right above the square axes in order to align them with it.这个想法是使用
Bbox
轴正上方的轴的Bbox
以便将它们与其对齐。 This can be done with:这可以通过以下方式完成:
import matplotlib.pyplot as plt
# the figure size can be set directly from here
# I used smaller figure size than in your example for visibility
fig, axs = plt.subplots(2, 3, figsize=(15, 7))
fig.tight_layout(pad=3.0)
axs[1, 2].set_aspect('equal')
# extract positions of square and above axes
p02 = axs[0, 2].get_position()
p12 = axs[1, 2].get_position()
# make square axes with left position taken from above axes, and set position
p12 = [p02.x0, p12.y0, p12.width, p12.height]
axs[1, 2].set_position(p12)
plt.show()
Which results in:结果是:
Have you tried.你有没有尝试过。 You can play around the position numbers based on your output.
您可以根据您的输出播放位置编号。
ax[6].set_position([0.1,0.2,0.1,0.2])
You can use this URL for reference to deep dive into the subject.您可以使用此 URL 作为参考以深入了解该主题。
Customizing Figure Layouts Using GridSpec and Other Functions 使用 GridSpec 和其他函数自定义图形布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.