使用 matplotlib 绘制 30x3 网格
Plot a grid 30x3 using matplotlib
我有 3 个数据集,每个数据集里面有 30 个不同的时间序列,我想并排绘制所有这些数据集,制作一个 30x3 的网格。这是我现在的代码:
for i in range(30):
plt.subplot(i+1, 1, 1) # row i, col 1 index 1
plt.plot(train[:, i].reshape(-1,), label='train')
plt.title("Sensor "+str(i))
plt.subplot(i+1, 2, 1) # row i, col 2 index 1
plt.plot(val[:, i].reshape(-1,), label='val')
plt.title("Sensor "+str(i))
plt.subplot(i+1, 3, 1) # row i, col 3 index 1
plt.plot(test[:, i].reshape(-1,), label='test')
plt.title("Sensor "+str(i))
plt.show()
我想要的结果是逐行打印,但是使用这段代码我只有一列而不是三列。具体来说,我得到了我绘制的最后一个元素 (test[:,i])。如何在循环的每次迭代中并排绘制 3 个元素?
f, ax = plt.subplots(30, 3)
for i in range(30):
ax[i, 0].plot(train[:, i].reshape(-1,), label='train')
ax[i, 0].set_title("Sensor "+str(i))
ax[i, 1].plot(val[:, i].reshape(-1,), label='val')
ax[i, 1].set_title("Sensor "+str(i))
ax[i, 2].plot(test[:, i].reshape(-1,), label='test')
ax[i, 2].set_title("Sensor "+str(i))
plt.show()
# sample data
train = np.random.randn(30,50)
test = np.random.randn(30,50)
val = np.random.randn(30,50)
data = [train, test, val]
# plotting
fig, ax = plt.subplots(nrows = 30, ncols = 3, figsize=(10,20))
for i in range(30):
for j in range(len(data)):
ax[i, j].plot(data[j][i])
输出:
我有 3 个数据集,每个数据集里面有 30 个不同的时间序列,我想并排绘制所有这些数据集,制作一个 30x3 的网格。这是我现在的代码:
for i in range(30):
plt.subplot(i+1, 1, 1) # row i, col 1 index 1
plt.plot(train[:, i].reshape(-1,), label='train')
plt.title("Sensor "+str(i))
plt.subplot(i+1, 2, 1) # row i, col 2 index 1
plt.plot(val[:, i].reshape(-1,), label='val')
plt.title("Sensor "+str(i))
plt.subplot(i+1, 3, 1) # row i, col 3 index 1
plt.plot(test[:, i].reshape(-1,), label='test')
plt.title("Sensor "+str(i))
plt.show()
我想要的结果是逐行打印,但是使用这段代码我只有一列而不是三列。具体来说,我得到了我绘制的最后一个元素 (test[:,i])。如何在循环的每次迭代中并排绘制 3 个元素?
f, ax = plt.subplots(30, 3)
for i in range(30):
ax[i, 0].plot(train[:, i].reshape(-1,), label='train')
ax[i, 0].set_title("Sensor "+str(i))
ax[i, 1].plot(val[:, i].reshape(-1,), label='val')
ax[i, 1].set_title("Sensor "+str(i))
ax[i, 2].plot(test[:, i].reshape(-1,), label='test')
ax[i, 2].set_title("Sensor "+str(i))
plt.show()
# sample data
train = np.random.randn(30,50)
test = np.random.randn(30,50)
val = np.random.randn(30,50)
data = [train, test, val]
# plotting
fig, ax = plt.subplots(nrows = 30, ncols = 3, figsize=(10,20))
for i in range(30):
for j in range(len(data)):
ax[i, j].plot(data[j][i])
输出: