【问题标题】:Plot line graph Seaborn while iterating across columns [duplicate]在跨列迭代时绘制线图Seaborn [重复]
【发布时间】:2019-11-14 10:43:03
【问题描述】:

给定:

    Month = ["Jan","Feb","Mar","Apr","May","Jun"]
Apple= [500,180,1141, 1209, 600,1200]
Orange= [900,350,198,789,650,500]
Cherry = [852,415,874,404, 692,444]

list = {'Month': Month,
       'Apple': Apple,
       'Orange': Orange,
       'Cherry': Cherry}

我正在尝试绘制一个折线图,其中 x= Month 和 y= Apple:Cherry 1) 一个图连同所有 3 个变量(Apple、Orange 和 Cherry)和 2) 折线图和每个变量 (x=月,y = Apple 等)。

我尝试过如下所示的跨列迭代,但它似乎无法通过 Seaborn 工作:

for i in range (df.shape[1]-1):
    sns.lineplot(x=df[:,0], y=df[:,i+1])

【问题讨论】:

  • 首先不要用list或dict命名任何自定义对象
  • 请注意,当您的数据集具有特定组织时,seaborn 将是最强大的。如here 所述(可能在其他地方),这种格式也称为“长格式”或“整洁”数据

标签: python pandas dataframe


【解决方案1】:

IIUC,你想要hue 中的seaborn

df = pd.DataFrame(lst)
new_df = df.melt(id_vars='Month', 
                 value_name='val', 
                 var_name='type')

sns.lineplot(x='Month', y='val', hue='type', data=new_df)

输出:

【讨论】:

  • 如何创建单独的图?我尝试了 iterate 语句,因为我想为每个变量设置单独的子图。
  • 你真的需要seaborn吗:df.plot(x='Month',subplots=True)
  • 在 Seaborn 中,绘制 x= 月会使它出现故障。例如,它从四月、二月、一月开始。订单也不是一个函数。有什么建议吗?
  • 对照索引绘图并将刻度标签更改为ax.set_xticklabels()?
  • 知道了,谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 2021-04-06
  • 2017-11-24
  • 2021-08-04
  • 2020-09-01
相关资源
最近更新 更多