如何显示科学记数法

问题描述 投票:0回答:2

我有一个seaborn relplot。我想显示科学记数法。目前,图像在 x 和 y 刻度上占用了很大的空间。我想通过将轴转换为科学记数法来最小化它。

我的代码:

sns.relplot(x='Vmpp',y='cVmpp',data=cdf)

我的解决方案和当前输出:

#I tried a solution reported for the seaborn heatmap. It did produce a plot (I think heat plot?) but did not work. 
sns.relplot(x='Vmpp',y='cVmpp',data=cdf,fmt='.2g')

当前输出:

AttributeError: 'PathCollection' object has no property 'fmt'

python matplotlib seaborn scientific-notation relplot
2个回答
2
投票

接受的解决方案不使用 relplot。

import matplotlib.pyplot as plt
import seaborn as sns

sns.set()
titanic = sns.load_dataset('titanic')

g = sns.relplot(x='age', y='fare', data=titanic, alpha=0.5);

for axes in g.axes.flat:
    axes.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0))


1
投票

sns.relplot()
是一个图形级函数。如果您只需要简单的散点图,您可能需要使用
sns.scatterplot()
来代替。

无论如何,您都可以用通常的 matplotlib 方式微调绘图。特别是,任何刻度标签数字的科学记数法都可以通过

ax.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0))
强制使用。

我还建议设置

alpha
值,因为你有很多重叠点。这是一个完整的例子:

import matplotlib.pyplot as plt
import seaborn as sns

sns.set()
titanic = sns.load_dataset('titanic')

fig, ax = plt.subplots()
ax.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0))
sns.scatterplot(x='age', y='fare', data=titanic, alpha=0.5);

编辑: 正如 @mwaskom 指出的,您也可以使用

sns.relplot()
以这种方式更改刻度标签,在这种情况下,只需在格式化程序之前调用绘图函数即可。您不需要指定轴,因为
ticklabel_format()
也可以通过
matplotlib.pyplot
接口工作:

# [...] imports and data as above 

sns.relplot(x='age', y='fare', data=titanic, alpha=0.5)
plt.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0));
© www.soinside.com 2019 - 2024. All rights reserved.