简体   繁体   English

python-对seaborn使用科学计数法

[英]python - using scientific notation with seaborn

I'm trying to create a kde plot using seaborn in python but when setting the colorbar values to show in scientific notation I see no difference. 我正在尝试使用python中的seaborn创建一个kde图,但是当设置颜色条值以科学计数法显示时,我发现没有区别。

See - making colorbar with scientific notation in seaborn for a heavily related topic. 请参阅- 在Seaborn中用科学记数法制作颜色条以获取与之高度相关的主题。

See - https://seaborn.pydata.org/generated/seaborn.kdeplot.html for the documentation of seaborn's kde class. 见- https://seaborn.pydata.org/generated/seaborn.kdeplot.html为seaborn的KDE类的文档。

Is there some reason why this doesn't work for the kde class or am I making a silly mistake in my formatting? 有什么原因对于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})

The result: 结果:

在此处输入图片说明

Here I would be expecting the colourbar to show an indexed notation as in the first link in the description of this problem. 在这里,我希望色标像在描述此问题时的第一个链接中那样显示索引符号。

Many thanks in advance! 提前谢谢了!

.set_scientific(True) applies to the offset label. .set_scientific(True)适用于偏移标签。 Here you don't have any offset, so it seems like it's ignored. 在这里,您没有任何偏移,因此似乎已被忽略。 There is unfortunately no canonical way to format the ticklabels themselves in scientific notation. 不幸的是,尚无规范的方式以科学计数法格式化滴答标签本身。

One method is shown in Can I show decimal places and scientific notation on the axis of a matplotlib plot using Python 2.7? 我可以使用Python 2.7在matplotlib图的轴上显示小数位和科学计数法吗?

Applying it here: 在这里应用:

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM