16
$\begingroup$

I have two data frames (Action, Comedy). Action contains two columns (year, rating) ratings columns contains average rating with respect to year. The Comedy data frame contains the same two columns with different mean values.

I merged both data frames into a total_year data frame.

The output of total_year:

Totle year Output

Now, I would like to plot total_year on a line graph in which the X axis should contain the year column and the Y axis should contain both the action and the comedy columns.

I can plot only 1 column at a time on Y axis using following code:

total_year[-15:].plot(x='year', y='action' ,figsize=(10,5), grid=True)

How can I plot both columns on the Y axis?

I took this photo from google just to let you know guys I want to draw graph in this way:

enter image description here

$\endgroup$
1
  • $\begingroup$ i got answer of this Question here $\endgroup$
    – Bilal Butt
    Dec 12, 2017 at 14:58

2 Answers 2

18
$\begingroup$

Feeding your column names into the y values argument as a list works for me like so:

total_year[-15:].plot(x='year', y=['action', 'comedy'], figsize=(10,5), grid=True)

Using something like the answer at this link is better and gives you way more control over the labels and whatnot: adding lines with plt.plot()

$\endgroup$
1
$\begingroup$

Using Seaborn:

Need to melt down columns so that they can be treated as hue in plotting

    import seaborn as sns
    df_melted = df.melt("year",var_name="Action&Comedy",value_name="Rating")

enter image description here

    sns.relplot(data=df_melt, x="year", y="Rating", hue="Action&Comedy",kind="line", height=4, aspect=.7)
    

enter image description here

$\endgroup$
1
  • $\begingroup$ Is there a way that doesn't involve melting? In my case, I have one column for the hue (which algorithm I'm running), one column for the score (which I'd like plotted on the Y axis) and a third column for the optimal possible score (which I'd like added as another line). How could I do this? $\endgroup$ Dec 29, 2021 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.