iTZMee125
iTZMee125

Reputation: 13

How to plot in matplotlib with two dimensional list

Say I have two lists

a=[[0,2,4]]
b=[[3,5,7]]

when I plot using matplotlib I know I can just get rid of the square brackets, i.e.

plt.plot([0,2,4], [3,5,7])

but what if I had a large list listed as a variable, say x, how to I plot it knowing I would I have to deal with double square brackets?

Upvotes: 0

Views: 1513

Answers (1)

Ronikos
Ronikos

Reputation: 447

From my understanding of the question, the lists are structured like [[1, 2, 3]].

This is an example of a two-dimensional list - albeit it a very useless one. If one consider another two dimensional list: [[1, 2], [3, 4]] The two lists inside the outer list can be treated as items of that list. ie:

a = [[1, 2],
     [3, 4]]

a[0] --> [1, 2]
a[1] --> [3. 4]

So in your example, the simplest way to turn the 2 dimensional list into a one dimensional list is simply take the first element of the list (because the first element is actually the useful list).

a = [[1, 2, 3]]
b = [[4, 5, 6]]

plt.plot(a[0], b[0])
plt.plot([1, 2, 3], [4, 5, 6])  # is the same as above

Upvotes: 3

Related Questions