How To Plot A Multiple Line Plot In Seaborn Using Specific Columns?
I have a bigger dataframe that resembles this format: df = pd.DataFrame([{'name': 'A', '2015': 99, '2016':98, '2017': '95', '2018':'99'}, {'name': 'B', '2015': 76, '2016':89, '2017
Solution 1:
Here I assume that the years are strings and the values are integers.
Seaborn expects data in tidy format, so first you might transform your dataframe.
import seaborn as sns
import pandas as pd
df = pd.DataFrame(
[
{"name": "A", "2015": 99, "2016": 98, "2017": 95, "2018": 99},
{"name": "B", "2015": 76, "2016": 89, "2017": 83, "2018": 85},
{"name": "C", "2015": 88, "2016": 89, "2017": 91, "2018": 91},
]
)
df.set_index("name", inplace=True)
tidy = df.stack().reset_index().rename(columns={"level_1": "year", 0: "values"})
Here is the outcome
name year values
0 A 2015991 A 2016982 A 2017953 A 2018994 B 2015765 B 2016896 B 2017837 B 2018858 C 2015889 C 20168910 C 20179111 C 201891Then, you can
- filter it for the years you want
- pass it as a
dataparameter - specify the columns at
x,y, andhue
sns.lineplot(
data=tidy[tidy["year"].isin(["2016", "2018"])], x="year", y="values", hue="name"
)
The result


Post a Comment for "How To Plot A Multiple Line Plot In Seaborn Using Specific Columns?"