【问题标题】:python - using scientific notation with seabornpython - 在 seaborn 中使用科学记数法
【发布时间】:2019-10-24 18:05:47
【问题描述】:

我正在尝试在 python 中使用 seaborn 创建一个 kde 图,但是在将颜色条值设置为以科学记数法显示时,我看不出有什么区别。

请参阅 - making colorbar with scientific notation in seaborn 了解相关主题。

有关 seaborn 的 kde 类的文档,请参阅 - https://seaborn.pydata.org/generated/seaborn.kdeplot.html

是否有某些原因导致这对 kde 类不起作用,或者我在格式化时犯了一个愚蠢的错误?

import numpy as np
import seaborn as sns
import matplotlib.ticker as tkr

a = np.random.normal(0,1,size=100)
b = np.random.normal(0,1,size=100)

fig, ax = plt.figure(), plt.subplot(111)
formatter = tkr.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
sns.kdeplot(a,b, n_levels=10, shade=True, cmap='Blues',
            cbar=True, cbar_kws={'format':formatter})

结果:

在这里,我希望颜色条显示索引符号,如该问题描述中的第一个链接中所示。

非常感谢!

【问题讨论】:

    标签: python seaborn colorbar scientific-notation


    【解决方案1】:

    .set_scientific(True) 适用于偏移标签。在这里你没有任何偏移量,所以它似乎被忽略了。不幸的是,没有规范的方法可以用科学记数法格式化刻度标签本身。

    一种方法显示在Can I show decimal places and scientific notation on the axis of a matplotlib plot using Python 2.7?

    在这里应用它:

    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.ticker as mticker
    
    a = np.random.normal(0,1,size=100)
    b = np.random.normal(0,1,size=100)
    
    fig, ax = plt.figure(), plt.subplot(111)
    
    f = mticker.ScalarFormatter(useOffset=False, useMathText=True)
    g = lambda x,pos : "${}$".format(f._formatSciNotation('%1.10e' % x))
    
    sns.kdeplot(a,b, n_levels=10, shade=True, cmap='Blues',
                cbar=True, cbar_kws={'format': mticker.FuncFormatter(g)})
    
    plt.show()
    

    【讨论】:

    • 谢谢,如果它们不能被规范地重新格式化,这看起来是最好的方法。很遗憾,它没有包含在这个类的功能中,但是你能做什么:')
    • “你能做什么?” - Matplotlib 是一个开源社区驱动的项目。每个人都可以做出贡献,无论是通过提出有关错误或缺失功能的问题,还是实施新代码。
    猜你喜欢
    • 2021-09-13
    • 2018-11-17
    • 2016-02-21
    • 2020-10-31
    • 1970-01-01
    • 2016-05-26
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多