在Python中调整matplotlib中各个子图的高度 [英] adjusting heights of individual subplots in matplotlib in Python
问题描述
如果我有一系列具有一列多行的子图,即:
if I have a series of subplots with one column and many rows, i.e.:
plt.subplot(4, 1, 1) # first subplot
plt.subplot(4, 1, 2) # second subplot
# ...
如何调整前N个子图的高度?例如,如果我有4个子图,每个子图在其自己的行上,则我希望它们全部具有相同的宽度,但前3个子图更短,即它们的y轴更小并且占用的绘图空间少于该行中最后一个绘图的y轴.我该怎么办?
how can I adjust the height of the first N subplots? For example, if I have 4 subplots, each on its own row, I want all of them to have the same width but the first 3 subplots to be shorter, i.e. have their y-axes be smaller and take up less of the plot than the y-axis of the last plot in the row. How can I do this?
谢谢.
推荐答案
有多种方法可以做到这一点.最基本(最不灵活)的方法是只调用以下内容:
There are multiple ways to do this. The most basic (and least flexible) way is to just call something like:
import matplotlib.pyplot as plt
plt.subplot(6,1,1)
plt.subplot(6,1,2)
plt.subplot(6,1,3)
plt.subplot(2,1,2)
这会给你这样的东西:
Which will give you something like this:
但是,这不是很灵活.如果您使用的是matplotlib> = 1.0.0,请使用 GridSpec .这非常好,并且是布置子图的更加灵活的方式.
However, this isn't very flexible. If you're using matplotlib >= 1.0.0, look into using GridSpec. It's quite nice, and is a much more flexible way of laying out subplots.
这篇关于在Python中调整matplotlib中各个子图的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!