当我绘制x轴值时,Python seaborn线图是无序的(即使它们在数据框中按顺序排列)

标签 python plot seaborn

我正在使用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'])

我不断收到如下图: enter image description here

如您所见,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/

相关文章:

python - ~ 运算符在 Python 中有什么作用

python - 在 matplotlib 中创建发散堆积条形图

python - 为什么在 seaborn pairplot 的 kde 子图中没有显示颜色?

python - 在 pandas 的 groupby 命令后与 seaborn 一起绘制

python - Seaborn 热图 : annotate with clickable hyperlink

python - python中tkinter输入框中的条件

python - NoReverseMatch - Django 1.7 初学者教程

python - 你能像往常一样制作一个python子进程输出stdout和stderr,但也可以将输出捕获为字符串吗?

python - 从包含 matplotlib 绘图的 cmd 运行 py 文件

r - 生成堆积条形图