0

If I create a list object such as 'a' and then I can plot out a[0] against a[1].


a = [ [1, 2, 3], [10, 20, 30], [15, 30, 45] ] 

plt.plot(a[0], a[1])

plot1



Why can I not do the below to have a second plot against a[2].

plt.plot(a[0], a[1:])

The docs say this

docs

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html

2 Answers 2

1

Let's first understand what is x values and what are y values:

In this array:

a = [ [1, 2, 3], [10, 20, 30], [15, 30, 45] ]

if you want to assign x as a[0] i.e. [1,2,3], then after passing them to a function say f, we should get the corresponding y values;

which means

y1 = f(1)
y2 = f(2)
y3 = f(3)

so for 3 x-values we would need 3 y-values

But when you pass a[1:] as y-values, you only select [[10, 20, 30], [15, 30, 45]], i.e. only 2 values, [10, 20, 30] and [15, 30, 45], there is no value available for y3.

So what you can do is either add value of y3 in your list a or transpose the y values from a shape of (2,3) to (3,2)

plt.plot(a[0], list(zip(*a[1:])))

Y - values now look like:

>>> list(zip(*a[1:]))

[(10, 15), (20, 30), (30, 45)]

y1 = (10,15)
y2 = (20,30)
y3 = (30,45)

Output:

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

Comments

0

In plot(x, y), the condition len(x) == len(y) should be met. The doc clearly mentions columns. You are trying to plot rows. Try this instead...

import numpy as np
import matplotlib.pyplot as plt

a = [ [1, 2, 3], [10, 20, 30], [15, 30, 45] ] 

plt.plot(a[0], np.transpose(a[1:]))

1 Comment

Thank you. I now get it.

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.