Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a line chart from a dataframe with all unique values of a column in pandas?

I'm trying to create a line chart from the data below with the date on the x-axis, score on the y-axis and a line for each value of X

  X Date       Score
1 A 2019-06-01 80
2 A 2019-05-01 70
3 A 2019-04-01 60
4 B 2019-06-01 90
5 B 2019-05-01 50
6 B 2019-04-01 70

I have tried df.groupby('X').head().plot.line(x='Date', y='Score'). Is it necessary to pivot the table so each value of X is a new column heading?

like image 844
WillacyMe Avatar asked Dec 03 '25 10:12

WillacyMe


1 Answers

You could use Seaborn's lineplot with hue option set to your specific column X, like:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = {'X':['A', 'A', 'A', 'B', 'B', 'B'],
        'Date' :['2019-06-01', '2019-05-01', '2019-04-01', '2019-06-01', '2019-05-01', '2019-04-01'],
        'Score': [80, 70, 60, 80, 50, 70]}

df = pd.DataFrame.from_dict(data)


ax = sns.lineplot(x="Date", y="Score", hue="X", data=df)
plt.show()

enter image description here

like image 168
cincin21 Avatar answered Dec 06 '25 06:12

cincin21