bharathi rajendran
bharathi rajendran

Reputation: 41

How to implement Lineplot using seaborn with x-axis as "Date"

I have tried to implement seaborn lineplot

  1. Data frame has list of Date value as index trying to make it as x-axis. Dataframe.info shows "Date" field as object
  2. I need lineplot for the 4 types of column values with date as x-axis
  3. when I tried to execute the below code it shows the error message as
ValueError: A wide-form input must have only numeric values.
<Figure size 720x360 with 0 Axes>
>>> sns.lineplot(data=file)
>>> plt.show()

Dataframe.info() message
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 59 entries, 0 to 58
Data columns (total 5 columns):
Date                                    59 non-null object
Avila Adobe                             59 non-null int64
Firehouse Museum                        59 non-null int64
Chinese American Museum                 59 non-null int64
America Tropical Interpretive Center    59 non-null int64
dtypes: int64(4), object(1)
memory usage: 2.4+ KB

Upvotes: 4

Views: 14022

Answers (1)

Hryhorii Pavlenko
Hryhorii Pavlenko

Reputation: 3910

Sorry in advance if I misunderstood the question.

The way I see it is that you need to plot integer values for given categories as y-axis, using dates as the x-axis.

I created this sample dataframe:

import pandas as pd

df = pd.DataFrame({
    'Avila Adobe': [11, 22, 33, 44, 55], 
    'Firehouse Museum': [13, 32, 23, 66, 54],
    'Chinese American Museum': [6, 15, 30, 40, 89],
    'America Tropical Interpretive Center': [40, 60, 80, 35, 17]
})

dates = pd.date_range('20190101', periods = 5)
df = df.set_index(dates)

So the table looks like this:

    Avila Adobe Firehouse Museum    Chinese American Museum America Tropical Interpretive Center
2019-01-01  11  13  6   40
2019-01-02  22  32  15  60
2019-01-03  33  23  30  80
2019-01-04  44  66  40  35
2019-01-05  55  54  89  17

The problem we have here is that the data stored in a wide format instead of a long one. So to plot the values of these 4 columns for given dates you need to transform the dataframe.

new_df = df.unstack().reset_index()
new_df.columns = ['Category', 'Date', 'Value']
new_df = new_df[['Date', 'Value', 'Category']]

Now in the long format the table looks like this:

    Date    Value   Category
0   2019-01-01  11  Avila Adobe
1   2019-01-02  22  Avila Adobe
2   2019-01-03  33  Avila Adobe
3   2019-01-04  44  Avila Adobe
4   2019-01-05  55  Avila Adobe
5   2019-01-01  13  Firehouse Museum
6   2019-01-02  32  Firehouse Museum
7   2019-01-03  23  Firehouse Museum
8   2019-01-04  66  Firehouse Museum
9   2019-01-05  54  Firehouse Museum
10  2019-01-01  6   Chinese American Museum
11  2019-01-02  15  Chinese American Museum
12  2019-01-03  30  Chinese American Museum
13  2019-01-04  40  Chinese American Museum
14  2019-01-05  89  Chinese American Museum
15  2019-01-01  40  America Tropical Interpretive Center
16  2019-01-02  60  America Tropical Interpretive Center
17  2019-01-03  80  America Tropical Interpretive Center
18  2019-01-04  35  America Tropical Interpretive Center
19  2019-01-05  17  America Tropical Interpretive Center

Now you could plot it with something like this:

import seaborn as sns
sns.lineplot(data=new_df, x='Date', y='Value', hue='Category')

Upvotes: 5

Related Questions