Home > Mobile >  Iterating through a list to find 5 highest values and creating a bar chart python
Iterating through a list to find 5 highest values and creating a bar chart python

Time:02-03

I am wanting to create a bar chart of top 5 populated states. My current function does not work for three reasons. 1) The most populated states are California,Texas,Florida,New York, and Pennsylvania. 2) The largest state the graph shows, california is the last one on the graph when it should be the first. 3) the y axis values are completely wrong. Not even numbering in the millions like the population does. Hope the question was clear. Thanks so much!

class state:

#creating a function of state
    def __init__(self,state_name,capital,population,flower):
        self.state_name=state_name
        self.capital=capital
        self.population=population
        self.flower=flower

def five_max_population(state_list):
    states = []
    populations = []
    for state in sorted([state.population for state in state_list], reverse=True):
        for state in state_list[:5]:
            states.append(state.state_name)
            populations.append(state.population)
    plt.bar(states, populations)
    plt.title('Top 5 Populated States')
    plt.xlabel('State')
    plt.ylabel('Population')
    plt.show()

state_list = []

s=state('Alabama','Montgomery',4918689,  'Camellia')
state_list.append(s)
s=state('Alaska','Juneau',727951,  'Forget Me Not')
state_list.append(s)
s=state( 'Arizona','Phoenix',7399410,  'Saguaro Cactus Blossom')
state_list.append(s)
s=state( 'Arkansas','Little Rock',3025875,  'Apple Blossom')
state_list.append(s)
s=state( 'California','Sacramento',39562858,  'California Poppy')
state_list.append(s)
s=state( 'Colorado','Denver',5826185,  'White and Lavender Columbine')
state_list.append(s)
s=state( 'Connecticut','Hartford',3559054,  'Mountain Laurel')
state_list.append(s)
s=state( 'Delaware','Dover',982049,  'Peach Blossom')
state_list.append(s)
s=state( 'Florida','Tallahassee',21711157,  'Orange Blossom')
state_list.append(s)
s=state( 'Georgia','Atlanta',10723715,  'Cherokee Rose')
state_list.append(s)
s=state( 'Hawaii','Honolulu',1411151,  'Hibiscus')
state_list.append(s)
s=state( 'Idaho','Boise',1823594,  'Syringa')
state_list.append(s)
s=state( 'Illinois','Springfield',12620571,  'Purple Violet')
state_list.append(s)
s=state( 'Indiana','Indianapolis',6768941,  'Peony')
state_list.append(s)
s=state( 'Iowa','Des Moines',3161522,  'Wild Prairie Rose')
state_list.append(s)
s=state( 'Kansas','Topeka',2915269,  'Sunflower')
state_list.append(s)
s=state( 'Kentucky',' Frankfort',4474193,  'Goldenrod')
state_list.append(s)
s=state( 'Louisiana','Baton Rouge',4637898,  'Magnolia')
state_list.append(s)
s=state( 'Maine','Augusta',1349367,'White Pine Cone and Tassel')
state_list.append(s)
s=state( 'Maryland','Annapolis',6055558,  'Black-Eyed Susan')
state_list.append(s)
s=state( 'Massachusetts','Boston',6902371,  'Mayflower')
state_list.append(s)
s=state( 'Michigan','Lansing',9989642,  'Apple Blossom')
state_list.append(s)
s=state( 'Minnesota','Saint Paul',5673015,  'Pink and White Lady Slipper')
state_list.append(s)
s=state( 'Mississippi','Jackson',2971278,  'Magnolia')
state_list.append(s)
s=state( 'Missouri','Jefferson City',6153233,  'White Hawthorn Blossom')
state_list.append(s)
s=state( 'Montana','Helena',1076891,  'Bitterroot')
state_list.append(s)
s=state( 'Nebraska','Lincoln',1943202,  'Goldenrod')
state_list.append(s)
s=state( 'Nevada','Carson City',3132971,  'Sagebrush')
state_list.append(s)
s=state( 'New Hampshire','Concord',1365957,  'Purple Lilac')
state_list.append(s)
s=state( 'New Jersey','Trenton',8878355,  'Violet')
state_list.append(s)
s=state( 'New Mexico','Santa Fe',2100917,  'Yucca Flower')
state_list.append(s)
s=state( 'New York','Albany',19376771,  'Rose')
state_list.append(s)
s=state( 'North Carolina','Raleigh',10594553,  'Dogwood')
state_list.append(s)
s=state( 'Ohio','Columbus',  11701859,'Scarlet Carnation')
state_list.append(s)
s=state( 'Oklahoma','Oklahoma City',3973707,  'Mistletoe')
state_list.append(s)
s=state( 'Oregon','Salem',4253588,'Oregon Grape')
state_list.append(s)
s=state( 'Pennsylvania','Harrisburg',12803056,  'Mountain Laurel')
state_list.append(s)
s=state( 'Rhode Island','Providence',1060435,  'Violet')
state_list.append(s)
s=state( 'South Carolina','Columbia',5213272,  'Yellow Jessamine')
state_list.append(s)
s=state( 'North Dakota','Bismarck',766044,  'Wild Prairie Rose')
state_list.append(s)
s=state( 'South Dakota','Pierre',890620,  'Pasque Flower')
state_list.append(s)
s=state( 'Tennessee','Nashville',6886717,  'Iris')
state_list.append(s)
s=state( 'Texas','Austin',29363096,  'Bluebonnet')
state_list.append(s)
s=state( 'Utah','Salt Lake City',3258366,  'Sego Lily')
state_list.append(s)
s=state( 'Tennessee','Nashville',4918689,  'Iris')
state_list.append(s)
s=state( 'Vermont','Montpelier',623620,  'Red Clover')
state_list.append(s)
s=state( 'Virginia','Richmond',8569752,  'Dogwood')
state_list.append(s)
s=state( 'Washington','Olympia',7705917,  'Pink Rhododendron')
state_list.append(s)
s=state( 'West Virginia','Charleston',1780003,  'Rhododendron')
state_list.append(s)
s=state( 'Wisconsin','Madison',5837462,  'Wood Violet')
state_list.append(s)
s=state( 'Wyoming','Cheyenne',579917,  'Indian Paintbrush')
state_list.append(s)

This is the outcome of the function five_max_population(state_list,)

CodePudding user response:

The reason that your code doesn't work is that

sorted([state.population for state in state_list], reverse=True)

returns a list of numbers: the populations of the last 5 states. Here is the altered code:

def five_max_population(state_list):
    states = []
    populations = []
    for state in sorted(state_list, key=lambda state: state.population, reverse=True)[:5]:
        states.append(state.state_name)
        populations.append(state.population)
    plt.bar(states, populations)
    plt.title('Top 5 Populated States')
    plt.xlabel('State')
    plt.ylabel('Population')
    plt.show()

This code sorts the states with the key (attribute to sort by) being the population of each state.

Also, when you read the population of California as 3.9 from the bar chart, it means 39 million, which is accurate to the data you've input. At the top, left of the "Top 5 Populated States", you can see "1e7", meaning 1*10^7, or 10 million, so you multiply the numbers on the left by 10 million.

CodePudding user response:

Here is how you sort states by some attribute (e.g. population):

sorted(state_list, key=lambda s: s.population)

If you have a very large list, you can speed things up by using operator.attrgetter:

from operator import attrgetter

sorted(state_list, key=attrgetter('population'))

To get the top five, just slice: you don't need iteration at all:

top_five = sorted(state_list, key=attrgetter('population'))[-5:]

Or in reverse order:

top_five = sorted(state_list, key=attrgetter('population'), reverse=True)[:5]

You can plot with something like this now:

plt.bar([s.name for s in top_five], [s.population for s in top_five])

Alternatively, you can use attrgetter again:

plt.bar(list(map(attrgetter('name'), top_five)), list(map(attrgetter('population'), top_five)))
  •  Tags:  
  • Related