首页 > 解决方案 > 尝试用 Python 计算每十年的自杀人数

问题描述

我刚开始使用 Python,我正在尝试分析一些数据,目前的目标是一个图表,其中 Y 是总数,X 是年份。

我试过寻找有类似问题但没有真正成功的人。我试过阅读数组,甚至尝试过二维数组,但也没有成功。

for row in csv.reader:
        if line_count == 0:
            print(f'Comumn names are {", ".join(row)}')
            line_count += 1
        elif line_count <= 1000:
            print(f"\t{row[0]} is the country and {row[1]} the year. The 
                     sex was {row[2]}.")
            t = f"\t{row[0]} is the country and {row[1]} the year. The 
                     sex was {row[2]}."

            if " male" in t:
                males += 1
            elif "female" in t:
                females += 1
            else:
                none += 1
            line_count += 1
        else:
           break



def stat():
       if "198" in line_count(row[1]):
           total[0] += 1
       elif "199" in line_count(row[1]):
            total[1] += 1
       elif "200" in line_count(row[1]):
            total[2] += 1
       elif "201" in line_count(row[1]):
            total[3] += 1
       elif "202" in line_count(row[1]):
            total[4] += 1

问题在于我的 matplotlib 正在使用 plt.plot()

我正在尝试围绕以下方面做一些事情:

plt.plot(years, currentDecadenDeaths)



years = [1980, 1990, 2000, 2010, 2020]

currentDecadenDeaths = [[years[0],[years[1],[years[2],[years[3], 
[years[4], [total[0], total[1], total[2], total[3], total[4]]]

plt.plot(years, currentDecadenDeaths)

标签: pythonmatplotlib

解决方案


years = [1980, 1990, 2000, 2010, 2020]

currentDecadenDeaths = [total[0], total[1], total[2], total[3], total[4]]

plt.plot(years, currentDecadenDeaths)

推荐阅读