【问题标题】:Plotting Line graph from a Series of 3 column: Matplotlib从一系列 3 列中绘制折线图:Matplotlib
【发布时间】:2019-04-13 17:27:23
【问题描述】:

我有一个 df,我按如下方式操作

genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']

df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]

print(df_genre_sum)

它给了我类似的结果:

现在,我如何绘制一个图表,我可以用

看到所有类型的不同颜色的线图

X 轴 = 年

Y 轴 = 收入

我是 matplotlib 新手,我知道当系列有 index 和 value 时要绘制图形,但这个系列里面有另一个级别。

更新

{'动作': {1984: 735328023.0, 1985: 152427960.0, 1987: 191185897.0, 1989: 962338821.0, 1990: 381293845.0, 1992: 14358033.0, 1993: 212954613.0, 1995: 418194034.0, 1996: 959696788.0, 1997: 967263794.0}, “冒险”:{1984:361744356.0, 1985: 533537722.0, 1987: 191185897.0, 1989: 1002823860.0, 1990: 244527783.0, 1992: 504050219.0, 1993: 1133054613.0, 1995: 418194034.0, 1996: 678894988.0, 1997: 922401607.0}, “喜剧”:{1984:295212467.0, 1985: 381109762.0, 1987: 38119483.0, 1989: 372485039.0, 1990: 721212258.0, 1992: 518408252.0, 1993: 212954613.0, 1995: 390174654.0, 1996: 353602675.0, 1997: 680494326.0}, “科幻小说”:{1984:402328023.0, 1985: 381109762.0, 1987: 38119483.0, 1989: 372485039.0, 1990: 244527583.0, 1992: 14358033.0, 1993: 1079155768.0, 1995: 73959291.0, 1996: 293648382.0, 1997: 760510868.0}, “惊悚片”:{1984:78371200.0, 1985: 152427960.0, 1987: 191185897.0, 1989: 156167015.0, 1990: 136766262.0, 1992: 658716349.0, 1993: 15668826.0, 1995: 710806752.0, 1996: 978717877.0, 1997: 2178045256.0}}

【问题讨论】:

    标签: pandas dataframe matplotlib


    【解决方案1】:

    我相信您需要先通过isin 和boolean indexing 进行过滤,然后聚合sum,最后通过Series 重塑unstack:

    df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
                     .groupby(['release_year', 'genre'])['revenue']
                     .sum()
                     .unstack())
    
    
    df_genre_sum.plot()
    

    【讨论】:

    • 结果为空
    • 你能告诉我为什么legends 代表每种类型的颜色在我这样做时会丢失吗:plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions')。 imgur.com/a/VfUFYKf
    • 看看图片imgur.com/a/VfUFYKf。这个小方框告诉我们哪种类型在图表中具有哪种颜色。使用上述 cmets 中提到的 plt 代码时,此框不会显示
    • 超级,你得到了解决方案。也可以使用plt.legend(df_genre_sum.columns)
    • 酷,传递列名更有意义。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多