1

Imagine this piece of code where X is the independent variable, and Y is the dependent variable and is equal to X ** 2:

X = [1, 2, 3]
Y = [1, 4, 9]
plt.plot(X, Y)
plt.show()

What if both of my variables were in the same list:

li = [[1, 1], [2, 4], [3, 9]]

The first element of every nested list is X, and the second one is Y; how should I plot this?

I'm pretty sure someone already asked this question, but I didn't know what to search and didn't found an answer.

SMMousaviSP
  • 624
  • 7
  • 23
  • 3
    `X, Y = zip(*li)`, see [this](https://stackoverflow.com/questions/13149278/splitting-a-nested-list-into-two-lists). – BigBen Dec 12 '20 at 03:59

1 Answers1

1

You can transpose the list of lists prior to plotting it:

X, Y = list(zip(*li))
plt.plot(X, Y)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80