【发布时间】:2021-02-23 09:39:36
【问题描述】:
我想创建一个图,其中子图在 for 循环中动态添加。应该可以以厘米为单位定义每个子图的宽度和高度,也就是说,添加的子图越多,图形就需要越大,以便为“传入”子图腾出空间。
在我的例子中,子图应该逐行添加,这样数字必须在 y 维度上变大。我遇到了这个stackoverflow post,这可能会导致正确的方向?也许gridspec module 也可以解决这个问题?
我尝试了第一篇文章中描述的代码,但这不能解决我的问题(它设置了最终的图形大小,但是添加到图形中的子图越多,每个子图变得越小,如下所示示例):
import matplotlib.pyplot as plt
# set number of plots
n_subplots = 2
def set_size(w,h,ax=None):
""" w, h: width, height in inches """
if not ax: ax=plt.gca()
l = ax.figure.subplotpars.left
r = ax.figure.subplotpars.right
t = ax.figure.subplotpars.top
b = ax.figure.subplotpars.bottom
figw = float(w)/(r-l)
figh = float(h)/(t-b)
ax.figure.set_size_inches(figw, figh)
fig = plt.figure()
for idx in range(0,n_subplots):
ax = fig.add_subplot(n_subplots,1,idx+1)
ax.plot([1,3,2])
set_size(5,5,ax=ax)
plt.show()
【问题讨论】:
标签: matplotlib plot