微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Seaborn Relplot 科学记数法

如何解决Python Seaborn Relplot 科学记数法

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

我的代码

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

enter image description here

我的解决方案和当前输出

#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',data=cdf,fmt='.2g')

当前输出

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

enter image description here

解决方法

sns.relplot() 是一个 figure-level 函数。如果您只需要简单的散点图,您可能需要改用 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',0))
sns.scatterplot(x='age',y='fare',data=titanic,alpha=0.5);

enter image description here

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

# [...] imports and data as above 

sns.relplot(x='age',alpha=0.5)
plt.ticklabel_format(axis='both',0));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。