【问题标题】:Prevent scientific notation in seaborn boxplot在seaborn boxplot中防止科学记数法
【发布时间】:2016-02-21 15:22:44
【问题描述】:

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

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

【问题讨论】:

  • 您的链接已损坏。另外,请指定您使用的 matplotlib 版本(我的错误信息与您的不同)

标签: python pandas matplotlib seaborn


【解决方案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)

【讨论】:

    猜你喜欢
    • 2022-10-10
    • 2020-03-06
    • 1970-01-01
    • 2021-09-13
    • 2019-10-24
    • 2023-03-07
    • 2022-01-19
    相关资源
    最近更新 更多