I have defined a 1D array x and a matrix y. Now I want to plot all rows or a specific row of y as function(s) of x. My code below shows 3 cases where case 1 successfully executes.
Can I rectify the syntaxes of the other two cases without using Pandas DataFrame? Also, what if I want to do the same using list instead of array?
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0, 1, 2, 3, 4])
y = np.array([[0, 1, 2, 3, 4],
[0, 1, 4, 9, 16],
[0, 1, 8, 27, 64],
[0, 1, 16, 81, 256]])
print('x =',x)
print('y =',y)
''' Case 1: Want to plot all rows of y-matrix as different plots '''
plt.plot(x, y.T) # works
print('y[2,:]=',y[2,:])
y2 = y[2,:] # Cube of x
''' Case 2: Want to plot row 2 of y-matrix '''
for i in range(5): # Doesn't work
print(x[i], y2[i])
plt.plot(x[i], y2[i])
''' Case 3: Want to plot all rows of y-matrix as different plots'''
for i in range(5): # Doesn't work
for j in range(4):
plt.plot(x[i], y[i][j])
plt.show()