防止 seaborn 箱线图中的科学记数法

Prevent scientific notation in seaborn boxplot

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

按照 How to prevent numbers being changed to exponential form in Python matplotlib figure 中的建议,我试过:

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 Boxplot 文档说,我可以传递一个 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)

那也没用。谁能告诉我怎么做?

这可能是一个丑陋的解决方案,但它确实有效,所以谁在乎呢

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)