如何在 Pairplot (seaborn) 中使用科学记数法 [英] How to use scientific notation in Pairplot (seaborn)
问题描述
有没有办法使用 Seaborn 的 Pairplot 强制使用科学记数法?我希望图之间有一些一致性(下面的例子).我已经找到了其他 seaborn 情节的建议,但没有成功地使用 Pairplot 实现任何东西.
Is there a way to force scientific notation using Seaborn's Pairplot? I'm hoping for some consistency between plots (examples below). I've found suggestions for other seaborn plots, but have not successfully implemented anything with Pairplot.
https://seaborn.pydata.org/generated/seaborn.pairplot.html
版本:
seaborn 0.8.1
麻木 1.13.1
matplotlib 2.0.2
熊猫 0.23.0
Versions:
seaborn 0.8.1
numpy 1.13.1
matplotlib 2.0.2
pandas 0.23.0
当前情节:
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib.pyplot as plt
#remove NAs
dfna = df.dropna()
#Correlation function
mean = np.zeros(3)
cov = np.random.uniform(.2, .4, (3, 3))
cov += cov.T
cov[np.diag_indices(3)] = 1
def corrfunc(x, y, **kws):
r, _ = stats.pearsonr(x, y)
ax = plt.gca()
ax.annotate("{:.2f}".format(r),
xy=(.3, .45), xycoords=ax.transAxes, fontsize = 30)
#2D regression + distribution
p = sns.pairplot(dfna, diag_kind="kde", diag_kws=dict(shade=True, color = "Blue"), plot_kws=dict(s=5, edgecolor="Blue", color = "Blue", linewidth=1))
p.fig.text(0.5, 1.00,'BLAHH', fontsize=30)
p.map_upper(plt.scatter, color = "White", edgecolor = "White")
p.map_upper(corrfunc)
p.fig.text(0.35, -0.04, "xxx", ha ='left', fontsize = 20)
p.fig.text(-0.04, 0.68, "xxx", ha ='left', fontsize = 20, rotation = 90)
#p.savefig('pairplot.svg')
尝试重新格式化失败:
plt.ticklabel_format(style = 'sci')
import matplotlib.ticker as tkr
formatter = tkr.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
p = sns.pairplot(dfna, plot_kws = {'format': formatter})
当前:
目标:
推荐答案
您需要将格式化程序应用于由 pairplot
创建的每个轴.实际上,边缘上的轴就足够了,但更容易将相同的东西均匀地应用于所有轴.我会这样做:
You need to apply your formatter to each of the axes created by pairplot
. actually, the axes on the edge would be sufficient, but it's easier to apply the same thing to all axes equally. I would do:
for ax in g.axes.flatten():
ax.ticklabel_format(style='sci', scilimits=(0,0), axis='both')
这篇关于如何在 Pairplot (seaborn) 中使用科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!