0

Update: Removing the screenshot, Below is the code from the screenshot:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(5)
y = np.array([[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5],[1,2,5,7,9]])

plt.plot(x,y) #gives ValueError: setting an array element with a sequence.

#A relaistic example
age = [20,30,40,50,60]
salary = np.array([[200,350,414],[300,500,612,700],[500,819],[900,1012],[812,712]])

plt.plot(age,salary) #gives ValueError: setting an array element with a sequence.

I am having two arrays each of size 5, elements of y are arrays, and I want them to be plotted against each x, for example at x = 0, I want to plot all the points from y[0], is there a way?

Update: Added another example above to show a realistic case , where I need to plot different salaries of different age people, each age people can have more than one salary.

r-beginners
  • 31,170
  • 3
  • 14
  • 32

2 Answers2

1

List comprehension to the rescue!

import numpy as np
import matplotlib.pyplot as plt

age = [20,30,40,50,60]
salary = np.array([[200,350,414],[300,500,612,700],[500,819],[900,1012],[812,712]])

#creating x-y tuples
xy = [(k, j) for i, k in enumerate(age) for j in salary[i]]

#unpacking the tuples with zip
plt.scatter(*zip(*xy))

plt.show()

Sample output: ![enter image description here

However, irregular numpy arrays should not be created, and this example works perfectly well with a normal list. Just saying.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • I populated age and salary with random data, and your code performed more than 5 times better than mine where I was calling plot() method as many times as many points were there. age = np.arange(200) salary = [] for a in age: sz = np.random.randint(20) arr = [] for s0 in np.arange(sz): data = np.random.randint(20000) arr.append(data) salary.append(arr) – Devashish Priyadarshi Dec 01 '20 at 12:16
  • You can look at list comprehensions as an optimized version of a for-loop. I linked to some explanation on list comprehension; you should familiarize yourself with this great Python tool. – Mr. T Dec 01 '20 at 12:28
0

As of now I am using the following workaround, but looking for a simpler solution:

indx = -1
for a in age:
    indx+=1
    for s in salary[indx]:
        plt.plot(a,s,'o')
plt.show()
  • What's wrong with this approach? I mean, you should specify the color but apart from that? – Mr. T Dec 01 '20 at 03:32
  • In this approach , I am calling the plot method as many times as many points are there to be plotted , hence taking more time , I tested with a dataset of 200 , in x , axis, and mine was taking 5 sec + while yours took few milliseconds, Thanks – Devashish Priyadarshi Dec 01 '20 at 12:13