目前,我正在尝试绘制57个图表。每个图都有两个数据集。我正在试图弄清楚如何绘制它们并将它们堆叠在一起,但是似乎如果我点击了3个子图,我就会得到
ValueError: num must be 1 <= num <= 2, not 3因此,似乎对允许的地块数量有某种限制?有没有其他方法可以做到这一点?
for k, v in tests.items():
print("Building graphs for " + str(k))
i = 1
while i <= v:
print("Building graph for Config " + str(i))
cur.execute('Select Iteration, Download, Upload From Ookla_Client where Config = ' + str(i) + ';')
db_return = cur.fetchall()
x = []
for test in db_return:
x.append(test[0])
y = []
for test in db_return:
y.append(test[1])
y2 = []
for test in db_return:
y2.append(test[2])
plt.subplot(2,1,i)
plt.plot(x,y,'.-')
plt.plot(x,y2,'.-')
plt.title('Config 1')
plt.xlabel('Test')
plt.ylabel('Mb/s')
plt.legend(['Download', 'Upload'], loc='upper right')
plt.grid()
i += 1发布于 2019-09-19 23:31:57
subplot函数使用3个参数:subplot(x,y,n),其中x是行数,y是列数,n是当前绘图的位置。因此,通过使用subplot(2, 1, i),您告诉Matplotlib您需要2个子图(一个在另一个上)。你想做的就是手动输入值,这样就有了网格。我当时做了一个函数来自动计算x和y的最佳值,以便有一个正方形的显示(或者说最接近正方形的是什么):
size = len(my_array_of_values)
final_x = 0
for i in range(10):
if pow(i,2) < size:
final_x += 1
final_y = ceil(size / final_x)
fig, axs = plt.subplots(final_y, final_x, sharex=False, sharey=False)然后,您可以使用以下命令访问不同的子图:current_axis = axs[floor(i/final_x)][i%final_x],i是您当前进行的迭代次数。如果我误解了你想要实现的目标,请让我知道它是否对你有效。
干杯
https://stackoverflow.com/questions/58014256
复制相似问题