在seaborn箱线图中防止科学计数法

4 投票
1 回答
6497 浏览
提问于 2025-05-10 15:27

我正在使用 pandas 版本 0.17.0、matplotlib 版本 1.4.3 和 seaborn 版本 0.6.0 来创建一个箱线图。我希望 x 轴上的所有数值都以浮点数的形式显示。目前,两个最小的值(0.00001 和 0.00005)是以科学计数法显示的。

这是我用来绘制图像的代码:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv("resultsFinal2.csv")

boxplot = sns.boxplot(x="Regularisierungsparameter", y="F1", data=data.sort("Regularisierungsparameter"))

plt.show()

根据 如何防止数字在 Python matplotlib 图形中变为指数形式 的建议,我尝试了:

boxplot = sns.boxplot(x="Regularisierungsparameter", y="F1", data=data.sort("Regularisierungsparameter"))
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_scientific(False)

结果是:

    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
AttributeError: 'FixedFormatter' object has no attribute 'set_useOffset'

Seaborn 的箱线图文档提到,我可以传递一个 Axes 对象来绘制图形。所以我尝试创建一个禁用科学计数法的轴,并将其传递给 sns.boxplot:

ax1 = plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
boxplot = sns.boxplot(x="Regularisierungsparameter", y="F1", data=data.sort("Regularisierungsparameter"), ax=ax1)

但这也没有成功。有人能告诉我该怎么做吗?

相关文章:

  • 暂无相关问题
暂无标签

1 个回答

1

这可能是个不太优雅的解决办法,但它能解决问题,所以谁在乎呢?

fig, ax = plt.subplots(1, 1)
boxplot = sns.boxplot(x="Regularisierungsparameter", y="F1", data=data.sort("Regularisierungsparameter"), ax=ax)
labels = ['%.5f' % float(t.get_text()) for t in ax.get_xticklabels()]
ax.set_xticklabels(labels)

撰写回答