0

Imagine that we have a list similar to this :

[
    [1115, 1061], 
    [134], 
    [304, 357, 253, 359], 
    [512, 513, 514], 
    [543], 
    [576], 
    [533], 
    [130], 
    [513, 357, 358]
]

how can we plot a scatter plot that has the values in the list as it y value and the index value of the list as the x value.

As an example, y=1115 should be shown at x=1 and y=1061 should be shown at x=1 as well, etc.

1 Answer 1

3

You can do it with enumerate:

li = [[1115, 1061], [134], [304, 357, 253, 359], 
      [512, 513, 514], [543], [576], [533], 
      [130], [513, 357, 358]]
for i in list(enumerate(li)):
    plt.scatter([i[0]+1]*len(i[1]), i[1])
plt.show()

enter image description here

If enumerate isn't familiar, explore it at a command-line:

eli = enumerate(li)
i = eli.next()
i[0]
i[1]

list(enumerate(li))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but why for [304, 357, 253, 359], it shows 3 dots and for [512, 513, 514] it shows 1 dot, etc.
Those dots are so close together (on a 0-1200 scale!) that you can't easily see that they overlap.
I appreciate a little explanation of the code as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.