从3列系列绘制线形图:Matplotlib

时间:2018-11-10 09:34:10

标签: pandas dataframe matplotlib

我有一个如下操作的df

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

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

print(df_genre_sum)

它给我的结果是:

img

现在,我该如何绘制图形,通过

可以看到所有流派具有不同颜色的线图

X轴=年

Y轴=收入

我是matplotlib的新手,我知道要在系列包含indexvalue的情况下绘制图形,但是该系列包含另一个层次。

更新

  

{'Action':{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:680049326.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:136676262.0,     1992:658716349.0,     1993:15668826.0,     1995:710806752.0,     1996:978717877.0,     1997:2178045256.0}}

1 个答案:

答案 0 :(得分:1)

我认为您首先需要通过isinboolean indexing进行过滤,然后聚合sum,最后通过unstack进行Series重塑:

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


df_genre_sum.plot()