遍历列表以找到 5 个最高值并创建条形图 python

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

我想创建人口最多的 5 个州的条形图。 由于三个原因,我当前的功能不起作用。 1) 人口最多的州是加利福尼亚州、德克萨斯州、佛罗里达州、纽约州和宾夕法尼亚州。 2) 图中显示的最大州,加州是图中的最后一个,而它应该是第一个。 3) y 轴值完全错误。甚至不像人口那样数以百万计。 希望问题很清楚。 非常感谢!

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)

您的代码不起作用的原因是

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

returns 数字列表:最近 5 个州的人口。 这是修改后的代码:

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()

此代码使用作为每个州人口的键(排序依据的属性)对州进行排序。

另外,当您从条形图中读取加利福尼亚州的人口为 3.9 时,这意味着 3900 万,这与您输入的数据是准确的。在“Top 5 Populated States”左侧的顶部,您可以看到“1e7”,表示 1*10^7,即 1000 万,因此您将左侧的数字乘以 1000 万。

以下是按某些属性(例如 population)对状态进行排序的方法:

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

如果您有一个非常大的列表,您可以使用 operator.attrgetter:

来加快速度
from operator import attrgetter

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

要获得前五名,只需切片:您根本不需要迭代:

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

或倒序:

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

你现在可以这样画了:

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

或者,您可以再次使用 attrgetter

plt.bar(list(map(attrgetter('name'), top_five)), list(map(attrgetter('population'), top_five)))