Home > Back-end >  Chart visualization, one year with multiple visualization points
Chart visualization, one year with multiple visualization points

Time:11-18

I am trying to code a very simple visualization - election_year and number of votes on a chart. However, last 3 times the elections were held the same year - in April, July and November of 2021, which the charts reads as one event. How can I break the year into parts to create several chart points? Here's the code:

import matplotlib.pyplot as plt
election_year = [2009, 2013, 2014, 2017, 2021, 2021, 2021]
voters = [1678641, 1081605, 1072491, 1147283, 837671, 642165, 596456]
plt.plot(election_year, voters)
plt.show()

CodePudding user response:

You say one thing, but your code says another. You're telling that three elections were held in the same year, so if you only care about the year, you should sum the voters for 2021 and have one 2021 in election_year with the accumulated votes.

But if you want these three different elections held in 2021 to be, in fact, separated, why do you give all three the same key (2021)? You can separate them by something like this.

import matplotlib.pyplot as plt
election_year = [2009, 2013, 2014, 2017, "2021 - a", "2021 - b", "2021 - c"]
voters = [1678641, 1081605, 1072491, 1147283, 837671, 642165, 596456]
plt.plot(election_year, voters)
plt.show()
  • Related