Can't retrieve values from numpy array

Viewed 35

I have a numpy array as follows along with a reshape:

X = M.dot(X1) + B #prints [[value1 value2 ]]
X.reshape(2,1)

I am then trying to plot this point along with others as follows:

plt.plot([X[0],...],[X[1],...],'-og','LineWidth',2)

However, I can't retrieve any values and I get index 1 out of range. How can I get rid of this problem and access the two values?

1 Answers

Note: .reshape does not work in place but instead returns something. Your Original X has shape (1 ,2). In your plot statement X[1] does access the row with index 1 (i.e. the "second" row) – which does not exist.

Anyway, try

X = X.squeeze()  # X.shape now is (2,)

This converts X to an 1d array. Then X[1] accesses the second element.

Alternatively, you could change your plot statement to use X[0, 1] instead of X[1] for your original X with 1 row and two columns.

Related