Subbu VidyaSekar
Subbu VidyaSekar

Reputation: 2615

How to plot seaborn lineplot with string variables on x-axis

I am trying to plot a time series chart using seaborn.lineplot() with string variables on x-axis. My data looks like this :

    month_year  billamount   tips
0     2018-03     200          10
1     2018-04     230          12
2     2018-05     500          10
3     2018-06     300          15
4     2018-07     200          20
5     2018-08     150          5
6     2018-09     100          5
7     2018-10     400          5
8     2018-11     500          10
9     2018-12     250          30
10    2019-01     200          20

in the above table, month_year is a object type(string) while trying to plot, it shows error message: ValueError: A wide-form input must have only numeric values.

Is there any option to plot with the string values on x-axis using seaborn lineplot.?

Upvotes: 2

Views: 13556

Answers (4)

BipulKrMahato
BipulKrMahato

Reputation: 1

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import numpy as np
import csv

f=np.genfromtxt('Data.txt',dtype=float,skip_header=1) #Data.txt is your data
month_year=f[:,0]
billamount=f[:,1]
tips=f[:2]
data=pd.DataFrame({'month_year':month_year,'billamount':bill_amount, 'tips':tips})
data.to_csv('Data.csv') # it will save the csv file
plt.figure(figsize=(8,14))
sns.lineplot(x=data['month_year'],y=data['tips'])
plt.title('seasonality of tips')
plt.xlabel('Years and Month')
plt.ylabel('Tips')
plt.show()

Upvotes: 0

bernie
bernie

Reputation: 586

It's possible, but you need to provide more guidance to seaborn:

import io
import pandas as pd
raw_data = """    month_year  billamount   tips
0     2018-03     200          10
1     2018-04     230          12
2     2018-05     500          10
3     2018-06     300          15
4     2018-07     200          20
5     2018-08     150          5
6     2018-09     100          5
7     2018-10     400          5
8     2018-11     500          10
9     2018-12     250          30
10    2019-01     200          20"""

df = pd.read_csv(io.StringIO(raw_data), sep='\s+')
sns.lineplot(x='month_year', y='billamount', data=df)

plot

Of course, if the values represented by your strings were unevenly spaced (i.e. if you skipped a month somewhere), seaborn would not detect this.

Upvotes: 2

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339570

I'm not sure if seaborn is actually supposed to work with strings in lineplots; but you can always choose to use a normal matplotlib plot.

import matplotlib.pyplot as plt
import pandas as pd

data = pd.DataFrame({"billamount" : [200, 230, 500, 300],
                     "month_year" : ["2018-03", "2018-04", "2018-05", "2018-06", ]})

plt.plot("month_year", "billamount", data=data)

plt.show()

enter image description here

Upvotes: 1

João Almeida
João Almeida

Reputation: 5077

According to the seaborn documentation lineplot doesn't support non numeric data.

It isn't totally clear what you want to achieve, however I suppose what you are looking for is the seaborn scatterplot function and you must provide the names for the x and y variables you are trying to plot.

Example:

tips = [10, 12,10,15]
billamount = [200, 230, 500, 300]
month_year= ["2018-03", "2018-04", "2018-05", "2018-06", ]
data = pd.DataFrame(np.array([tips, billamount, month_year]).T,
                    columns=["tips", "billamount", "month_year"])

ax = sns.scatterplot(x="month_year", y="billamount", data=data)

resulting plot

Upvotes: 1

Related Questions