【问题标题】:How to plot seaborn lineplot with string variables on x-axis如何在 x 轴上使用字符串变量绘制 seaborn 线图
【发布时间】:2019-07-15 03:49:54
【问题描述】:

我正在尝试使用 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 轴上绘制字符串值?

【问题讨论】:

  • 您能否提供一个产生此错误的示例代码? stackoverflow.com/help/mcve 并澄清你想要达到的目标?
  • sns.lineplot(data=data)数据就是上面提到的表格。

标签: python data-visualization seaborn


【解决方案1】:

根据seaborn documentation 线图不支持非数字数据。

您想要实现的目标并不完全清楚,但我想您正在寻找的是seaborn scatterplot function,您必须提供您尝试绘制的 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)

【讨论】:

  • 我喜欢用线条绘制billamount,tips 这两个变量之间的相关性。
【解决方案2】:

我不确定 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()

【讨论】:

    【解决方案3】:

    有可能,但需要给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 不会检测到这一点。

    【讨论】:

      【解决方案4】:
      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()
      

      【讨论】:

      • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
      • 感谢您的建议。
      猜你喜欢
      • 2020-12-21
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2020-10-12
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多