Home > Net >  How to plot specific index from array?
How to plot specific index from array?

Time:07-05

I have these three lists:

amountOfYes = [38, 33, 30, 29, 29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 26]
amountOfNo = [39, 35, 33, 32, 30, 28, 24, 22, 21, 21, 21, 20, 20, 20, 19]
index = [0, 118, 393, 317, 1, 8, 9, 29, 226, 297, 331, 52, 105, 251, 316]

amountOfYes & amountOfNo are lists filled with the amount of Yes and No that the top 15 sessions with most attendance had and Index is a list filled with the correspondent position that voting.

the first voting had 38 yes and 39 no, the 118th had 33 yes and 35 no.

How do I plot the amount of yes and no with the corresponding index?

EDIT:

The idea is to create a line graph similar to this one: Graph with the first 20 votings

This chart has the first 20 votings, I need the votings specific votings of Index

I was going with

plt.plot(amountOfYes[index], label = 'Yes', color = 'red')
plt.plot( amountOfNo[index], label = 'No', color = 'black')

The X-axis should be the index and the Y the number of votes each option got

CodePudding user response:

I don't know if this is what you're going for, but it seems like the most useful way to implement what you want. The gaps in you indices make it hard to get a useful graph. I made strings out of the indices, this way they show up with tick marks of that index.

import matplotlib.pyplot as plt
import numpy as np

amountOfYes = [38, 33, 30, 29, 29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 26]
amountOfNo = [39, 35, 33, 32, 30, 28, 24, 22, 21, 21, 21, 20, 20, 20, 19]
index = [0, 118, 393, 317, 1, 8, 9, 29, 226, 297, 331, 52, 105, 251, 316]
index = [str(x) for x in index]
plt.plot(index,amountOfYes, label = 'Yes', color = 'red')
plt.plot(index,amountOfNo, label = 'No', color = 'black')
plt.legend()
plt.xlabel("index")
plt.ylabel("amount of votes")
plt.show()

This results in the following image:

enter image description here

If you do just want the plot with the gaps, I suggest sorting the two vote arrays based on the index array, and plotting index vs amountOfYes and amountOfNo:

import matplotlib.pyplot as plt
import numpy as np

amountOfYes = [38, 33, 30, 29, 29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 26]
amountOfNo = [39, 35, 33, 32, 30, 28, 24, 22, 21, 21, 21, 20, 20, 20, 19]
index = [0, 118, 393, 317, 1, 8, 9, 29, 226, 297, 331, 52, 105, 251, 316]

zipped = list(zip(amountOfYes,amountOfNo,index))
zipped.sort(key=lambda x:x[2])

amountOfYes = [x[0] for x in zipped]
amountOfNo = [x[1] for x in zipped]
index = [x[2] for x in zipped]

plt.plot(index,amountOfYes, label = 'Yes', color = 'red')
plt.plot(index,amountOfNo, label = 'No', color = 'black')
plt.legend()
plt.xlabel("index")
plt.ylabel("amount of votes")
plt.show()

which gives the following (note how long the line segments are, a line plot is not really the best way in this case I think, I'd go with a bar plot perhaps, as this suggests some kind of continuity in the gaps): enter image description here

  • Related