david_doji david_doji - 3 months ago 7
Python Question

plot arrays by row with matplotlib

I have two numpy arrays (a and b) with shape (16, 850) each. I'm displaying them row by row, e.g.

plt.figure()
plt.plot(a[0], b[0])
plt.plot(a[1], b[1])
plt.plot(a[2], b[2])
...
plt.show()


Should I have to use a for loop to do it in a more pythonic way?

Answer

You can pass a multi-dimensional array to plot and each column will be created as a separate plot object. We transpose both inputs so that it will plot each row separately.

a = np.random.rand(16, 850)
b = np.random.rand(16, 850)

plt.plot(a.T, b.T)
plt.show()
Comments