1

I am having trouble plotting multiple lines from a 2D list. I currently have the below dataset.

x = np.linspace(0, 4, 5)
y = [[0.32,1.25,2.36,3.36,3.52],[0.32,1.25,2.36,3.36,3.52]]

and to plot this I implemented

for i in range(len(y)):
    for x in range(len(y[i])):
        plt.plot(x[x], y[I][x])
plt.show()

How can I have it so that I have multiple lines in one graph thus plotting it from a 2d array?

1
  • Thank you, but I don't really get your comment. What is T? Commented Jul 30, 2018 at 17:43

2 Answers 2

4

To avoid a loop, you can transform y into a numpy array, transpose it with y.T so that it is aligned with the x array and then simply plot both arrays:

x = np.linspace(0, 4, 5)
y = np.asarray([[0.32,1.25,2.36,3.36,3.52],[0.2,1.5,2.6,2.3,1.5]])
print(y)

plt.plot(x, y.T)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

remove the for loop:

plt.plot(x,y[0],x,y[1])
plt.show()

this should give you the result you were looking for. However, your graphs are identical so it'll always look like one graph. If you get different data it'll work.

graph example with different y data

3 Comments

Thank you for your comment! However what happens if I have a lot of data. I don't want to do x,y[200]
then you could make a for loop that increments through the arrays in y and just say plt.plot(x,y[i])
This will work in the same way. just plt.show() at the end

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.