我正在使用seaborn 来绘制线图。
这是一个示例数据:
error_mean.head(5)
输出如下:
error_rate
10 0.829440
20 0.833747
30 0.835182
40 0.837922
50 0.835835
所以索引值确实是有序的(或者至少看起来是这样)。
这是我绘制上述数据的代码:
plt.figure(figsize=(15,5))
sns.lineplot(x=error_mean.index.values, y=error_mean['error_rate'])
如您所见,x 轴值完全乱序!我尝试用谷歌搜索这个问题,但找不到类似问题的答案。
感谢任何帮助!
最佳答案
我猜问题是 error_mean.index.values 是一个 str
类型的系列。您需要将其转换为int
。
检查之间的差异:
import pandas as pd
import seaborn as sns
import matplotlib as plt
df1 = pd.DataFrame([
["10", 0.829440],
["20", 0.833747],
["100", 0.835182],
["40" , 0.837922],
["50", 0.835835]])
sns.lineplot(x=df1[0], y=df1[1])
和
df1 = pd.DataFrame([
["10", 0.829440],
["20", 0.833747],
["100", 0.835182],
["40" , 0.837922],
["50", 0.835835]])
sns.lineplot(x=(df1[0]).astype(int), y=df1[1])
所以我会尝试:
plt.figure(figsize=(15,5))
sns.lineplot(x=error_mean.index.values.astype(int), y=error_mean['error_rate'])
关于当我绘制x轴值时,Python seaborn线图是无序的(即使它们在数据框中按顺序排列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963752/