【问题标题】:python Stacked area chartpython堆积面积图
【发布时间】:2020-03-24 22:13:31
【问题描述】:

我正在尝试创建一个堆积面积图,显示课程的演变及其数量随时间的变化。所以我的数据框是(index=Year):

                    Area  Courses
Year                             
1900         Agriculture      0.0
1900        Architecture     32.0
1900           Astronomy     10.0
1900             Biology     20.0
1900           Chemistry     25.0
1900   Civil Engineering     21.0
1900           Education     14.0
1900  Engineering Design     10.0
1900             English     30.0
1900           Geography      1.0

去年:2011 年。

我尝试了几种解决方案,例如 df.plot.area()、df.plot.area(x='Years')。 然后我认为将区域作为列会有所帮助,所以我尝试了

df.pivot_table(index = 'Year', columns = 'Area', values = 'Courses', aggfunc = 'sum')

但我没有获得每年的课程总和,而是:

Area  Aeronautical Engineering  ...  Visual Design
Year                            ...               
1900                       NaN  ...            NaN
1901                       NaN  ...            NaN

感谢您的帮助。 这是我的第一篇文章。对不起,如果我错过了什么。

更新。这是我的代码:

df = pd.read_csv(filepath, encoding= 'unicode_escape')
df = df.groupby(['Year','GenArea'])['Taught'].sum().to_frame(name = 'Courses').reset_index()
plt.stackplot(df['Year'], df['Courses'], labels = df['GenArea'])
plt.legend(loc='upper left')
plt.show()

这里是数据集的链接:https://data.world/makeovermonday/2020w12

【问题讨论】:

    标签: python stacked-area-chart


    【解决方案1】:

    使用额外的给定信息,我做了这个。希望你喜欢!

    import pandas as pd
    import matplotlib.pyplot as plt
    
    plt.close('all')
    
    df=pd.read_csv('https://query.data.world/s/djx5mi7dociacx7smdk45pfmwp3vjo',
                   encoding='unicode_escape')
    df=df.groupby(['Year','GenArea'])['Taught'].sum().to_frame(name=
                 'Courses').reset_index()
    aux1=df.duplicated(subset='GenArea', keep='first').values
    aux2=df.duplicated(subset='Year', keep='first').values
    
    n=len(aux1);year=[];courses=[]
    
    for i in range(n):
        if not aux1[i]:
            courses.append(df.iloc[i]['GenArea'])
        if not aux2[i]:
            year.append(df.iloc[i]['Year'])
        else:
            continue
    
    del aux1,aux2
    df1=pd.DataFrame(index=year)
    s=0
    
    for i in range(len(courses)):
        df1[courses[i]]=0
    for i in range(n):
        string=df.iloc[i]['GenArea']
        if any(df1.iloc[s].values==0):
            df1.at[year[s],string]=df.iloc[i]['Courses']
        else:
            s+=1
            df1.at[year[s],string]=df.iloc[i]['Courses']
    
    del year,courses,df
    df1=df1[df1.columns[::-1]]
    df1.plot.area(legend='reverse')
    

    【讨论】:

    • 我阅读了那篇文章并尝试了此代码plt.stackplot(courses_by_area['Year'], courses_by_area['Courses'], labels = courses_by_area['Area']),但仍然无法正常工作。
    • 您能添加一个代码示例吗?我不明白你想要什么或你想做什么。
    • 我已经发布了我所有的代码。我有该数据集,其中包含每年的研究领域,我想绘制每年教授的课程数量以及它们在各个领域的分布。我希望现在更清楚了。谢谢!
    • 当我要一段代码时,我要的是一段我与你分享的所有数据来绘制的代码。不客气!
    • 哇,谢谢。我期待这样的事情。我去练习,直到我完全理解为止。最好的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多