2

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.?

2
  • Can you provide an example code that produces this error? stackoverflow.com/help/mcve And clarify what you want to achieve? Commented Feb 21, 2019 at 9:36
  • sns.lineplot(data=data) data is the above mentioned table. Commented Feb 21, 2019 at 9:38

4 Answers 4

2

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.

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

Comments

1

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

Comments

1

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

1 Comment

I like to plot correlation between those two variables billamount,tips with lines.
0
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()

2 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
Thanks for your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.