1

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.