1

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': 83, '2018': 85}, {'name': 'C', '2015': 88, '2016':89, '2017': 91, '2018':91}])

df.set_index('name', inplace=True)

enter image description here

I would like to plot A, B, and C in a multiline plot for just the years of 2016 and 2018. After searching for a while, I can't seem to figure how to plot specific columns from a dataframe. Is the best practice creating a new one for each plot?

3
  • No, just a copy-paste error while creating an example. Thank you for pointing that out, fixed Commented May 16, 2020 at 1:49
  • You want years on the x axis? Commented May 16, 2020 at 1:52
  • Yes, x-axis would be years and y-axis would be values for each 'name' for those years Commented May 16, 2020 at 1:59

2 Answers 2

4

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   2015    99
1   A   2016    98
2   A   2017    95
3   A   2018    99
4   B   2015    76
5   B   2016    89
6   B   2017    83
7   B   2018    85
8   C   2015    88
9   C   2016    89
10  C   2017    91
11  C   2018    91

Then, you can

  1. filter it for the years you want
  2. pass it as a data parameter
  3. specify the columns at x, y, and hue
sns.lineplot(
    data=tidy[tidy["year"].isin(["2016", "2018"])], x="year", y="values", hue="name"
)

The result

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that you have numeric data on the columns and in the values, you can simply melt you dataframe and call a lineplot.

sns.lineplot(data=df.melt(id_vars=['name']), 
             x='variable', y='value', hue='name')

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.