0

I have 3 datasets, each one with 30 different time series inside it, and I would like to plot all of them side by side, making a grid 30x3. This is the code I have a the moment:

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()

The results I want, is to print row by row, but with this code I only have one column instead of three. Specifically, I get the last element I plot (test[:,i]). How can I plot 3 elements side by side in each iteration of my loop?

Fabio
  • 635
  • 6
  • 17
  • [Read the documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplot.html) again. `Subplot` expects arguments `nrows`, `ncols`, `index`, where you're giving it `nrows`, `index`, `ncols` – AJ Biffl Dec 16 '21 at 21:12

2 Answers2

1
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()
Z Li
  • 4,133
  • 1
  • 4
  • 19
0
# 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])

Output:

enter image description here

mujjiga
  • 16,186
  • 2
  • 33
  • 51