【问题标题】:Seaborn 'Could not interpret input' errorSeaborn“无法解释输入”错误
【发布时间】:2020-10-21 12:21:05
【问题描述】:

我已经阅读了引发类似异常的帖子(即:herethere),但并不是特别有用,因为错误基本上是由于拼写错误造成的。

下面是引发我错误的非常简单的代码。阅读 seaborn 文档和示例并没有帮助。

import pandas
import seaborn
df=pandas.DataFrame([[0,25.0,100],[0,24.0,95],[0,25.6,90],[0,20,120],[0,21,130],[0,22.5,115],[1,25.0,100],[1,26.0,150],[1,24.0,120],[1,20.0,200],[1,15.0,250]], columns=["meter","T°@T","meter-reading"])
df

输出:

meter   T°@T    meter-reading
0   0   25.0    100
1   0   24.0    95
2   0   25.6    90
3   0   20.0    120
4   0   21.0    130
5   0   22.5    115
6   1   25.0    100
7   1   26.0    150
8   1   24.0    120
9   1   20.0    200
10  1   15.0    250

然后尝试绘制 FacetGrid:

g=seaborn.FacetGrid(data=df, col="meter").map(seaborn.relplot, x="T°@T",y="meter-reading")


Error: Could not interpret input 'T°@T'

非常奇怪的是,.map 遇到了问题,但其他代码工作正常:

seaborn.relplot(data=df,x="T°@T",y="meter-reading")

我做错了什么?

PS:Jupyter Notebook 和 Spyder 都会引发异常

【问题讨论】:

    标签: python seaborn facet-grid


    【解决方案1】:

    我猜这是一个错误,因为 relplot 本身可以扩展多个图表。因此,您可以通过指定sns.scatterplot 来绘制图形。我不认为 `meter' 是彩色编码的,因为它不是类别变量。

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    g = sns.FacetGrid(data=df, col="meter")
    g.map_dataframe(sns.scatterplot, x=df["T°@T"], y=df["meter-reading"], hue='meter') 
    g.set_axis_labels('T°@T','meter-reading')
    g.add_legend()
    

    【讨论】:

    • 是的,在进行实验时,我还注意到切片数据帧可以完成这项工作。我不明白为什么下面的例子有效,而不是我的。 "g.map(sns.scatterplot, "total_bill", "tip", alpha=.7)" 来自:seaborn.pydata.org/tutorial/axis_grids.html 我的意思是,毕竟我们都在使用数据框的列名。
    • 我发现 seaborn.scatterplot 和 .lineplot 在我的初始代码中没有遇到错误。因此,另一种选择是使用我的初始代码,如下所示:g=seaborn.FacetGrid(data=df, col="meter").map_dataframe(seaborn.lineplot, x="T°@T", y="meter-reading") 谢谢,您的回答让我尝试了 .relplot 以外的其他方法
    猜你喜欢
    • 2015-12-30
    • 2019-01-12
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2021-08-01
    • 2013-03-08
    相关资源
    最近更新 更多