Plotting Line graph from a Series of 3 column: Matplotlib
up vote
1
down vote
favorite
I have a df which i have manipulated as below
genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']
df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]
print(df_genre_sum)
It gives me result something like :
Now, How can I draw a graph where I can see line plot with all genres in different colors with
X axis = Years
Y axis = Revenue
I am new to matplotlib, I know to draw graph when series has index
and value
but this series has another level inside it.
Update
{'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},
'Adventure': {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},
'Comedy': {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},
'Science Fiction': {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},
'Thriller': {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
add a comment |
up vote
1
down vote
favorite
I have a df which i have manipulated as below
genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']
df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]
print(df_genre_sum)
It gives me result something like :
Now, How can I draw a graph where I can see line plot with all genres in different colors with
X axis = Years
Y axis = Revenue
I am new to matplotlib, I know to draw graph when series has index
and value
but this series has another level inside it.
Update
{'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},
'Adventure': {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},
'Comedy': {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},
'Science Fiction': {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},
'Thriller': {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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a df which i have manipulated as below
genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']
df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]
print(df_genre_sum)
It gives me result something like :
Now, How can I draw a graph where I can see line plot with all genres in different colors with
X axis = Years
Y axis = Revenue
I am new to matplotlib, I know to draw graph when series has index
and value
but this series has another level inside it.
Update
{'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},
'Adventure': {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},
'Comedy': {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},
'Science Fiction': {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},
'Thriller': {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
I have a df which i have manipulated as below
genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']
df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]
print(df_genre_sum)
It gives me result something like :
Now, How can I draw a graph where I can see line plot with all genres in different colors with
X axis = Years
Y axis = Revenue
I am new to matplotlib, I know to draw graph when series has index
and value
but this series has another level inside it.
Update
{'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},
'Adventure': {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},
'Comedy': {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},
'Science Fiction': {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},
'Thriller': {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
pandas dataframe matplotlib
edited yesterday
asked yesterday
Shashank Vivek
3,93232252
3,93232252
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I believe you need filter first by isin
with boolean indexing
, then aggregate sum
and final Series
reshape by unstack
:
df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
.groupby(['release_year', 'genre'])['revenue']
.sum()
.unstack())
df_genre_sum.plot()
The result is empty
– Shashank Vivek
yesterday
sorry, edited filtering.
– jezrael
yesterday
Can you tell me why thelegends
to represent color of each genre is missing when I do :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
– Shashank Vivek
yesterday
1
Super, you get solution. Also is possible useplt.legend(df_genre_sum.columns)
– jezrael
yesterday
1
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
|
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I believe you need filter first by isin
with boolean indexing
, then aggregate sum
and final Series
reshape by unstack
:
df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
.groupby(['release_year', 'genre'])['revenue']
.sum()
.unstack())
df_genre_sum.plot()
The result is empty
– Shashank Vivek
yesterday
sorry, edited filtering.
– jezrael
yesterday
Can you tell me why thelegends
to represent color of each genre is missing when I do :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
– Shashank Vivek
yesterday
1
Super, you get solution. Also is possible useplt.legend(df_genre_sum.columns)
– jezrael
yesterday
1
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
|
show 4 more comments
up vote
1
down vote
accepted
I believe you need filter first by isin
with boolean indexing
, then aggregate sum
and final Series
reshape by unstack
:
df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
.groupby(['release_year', 'genre'])['revenue']
.sum()
.unstack())
df_genre_sum.plot()
The result is empty
– Shashank Vivek
yesterday
sorry, edited filtering.
– jezrael
yesterday
Can you tell me why thelegends
to represent color of each genre is missing when I do :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
– Shashank Vivek
yesterday
1
Super, you get solution. Also is possible useplt.legend(df_genre_sum.columns)
– jezrael
yesterday
1
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
|
show 4 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I believe you need filter first by isin
with boolean indexing
, then aggregate sum
and final Series
reshape by unstack
:
df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
.groupby(['release_year', 'genre'])['revenue']
.sum()
.unstack())
df_genre_sum.plot()
I believe you need filter first by isin
with boolean indexing
, then aggregate sum
and final Series
reshape by unstack
:
df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
.groupby(['release_year', 'genre'])['revenue']
.sum()
.unstack())
df_genre_sum.plot()
edited yesterday
answered yesterday
jezrael
304k20237314
304k20237314
The result is empty
– Shashank Vivek
yesterday
sorry, edited filtering.
– jezrael
yesterday
Can you tell me why thelegends
to represent color of each genre is missing when I do :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
– Shashank Vivek
yesterday
1
Super, you get solution. Also is possible useplt.legend(df_genre_sum.columns)
– jezrael
yesterday
1
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
|
show 4 more comments
The result is empty
– Shashank Vivek
yesterday
sorry, edited filtering.
– jezrael
yesterday
Can you tell me why thelegends
to represent color of each genre is missing when I do :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
– Shashank Vivek
yesterday
1
Super, you get solution. Also is possible useplt.legend(df_genre_sum.columns)
– jezrael
yesterday
1
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
The result is empty
– Shashank Vivek
yesterday
The result is empty
– Shashank Vivek
yesterday
sorry, edited filtering.
– jezrael
yesterday
sorry, edited filtering.
– jezrael
yesterday
Can you tell me why the
legends
to represent color of each genre is missing when I do : 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– Shashank Vivek
yesterday
Can you tell me why the
legends
to represent color of each genre is missing when I do : 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– Shashank Vivek
yesterday
1
1
Super, you get solution. Also is possible use
plt.legend(df_genre_sum.columns)
– jezrael
yesterday
Super, you get solution. Also is possible use
plt.legend(df_genre_sum.columns)
– jezrael
yesterday
1
1
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
Cool, makes more sense to pass column names. Thanks :)
– Shashank Vivek
19 hours ago
|
show 4 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237654%2fplotting-line-graph-from-a-series-of-3-column-matplotlib%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password