繁体   English   中英

如何在 x 轴上使用字符串变量绘制 seaborn lineplot

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

我正在尝试使用seaborn.lineplot()和 x 轴上的字符串变量绘制时间序列图表。 我的数据如下所示:

    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

在上表中, month_year是尝试绘图时的对象类型(字符串),它显示错误消息: ValueError: A wide-form input must have only numeric values.

是否有任何选项可以使用 seaborn lineplot 在 x 轴上绘制字符串值。?

是可以的,但是你需要给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)

阴谋

当然,如果您的字符串表示的值间隔不均匀(即如果您在某处跳过一个月),seaborn 将无法检测到这一点。

根据seaborn 文档lineplot 不支持非数字数据。

不完全清楚您想要实现什么,但是我想您正在寻找的是seaborn 散点图函数,并且您必须提供要绘制的 x 和 y 变量的名称。

例子:

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)

结果图

我不确定 seaborn 是否真的应该处理线图中的字符串; 但你总是可以选择使用普通的 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()

在此处输入图片说明

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM