如何在Pairplot(seaborn)中使用科学记数法

时间:2018-06-07 14:40:53

标签: python matplotlib seaborn

有没有办法使用Seaborn的配对图强制科学记数法?我希望图之间有一些一致性(下面的例子)。我已经找到了其他seaborn图的建议,但是没有用Pairplot成功实现任何东西。

https://seaborn.pydata.org/generated/seaborn.pairplot.html

版本:
seaborn 0.8.1
numpy 1.13.1
matplotlib 2.0.2
大熊猫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') 

重新格式化尝试失败:

  1. plt.ticklabel_format(style = 'sci')
    
  2. import matplotlib.ticker as tkr        
    formatter = tkr.ScalarFormatter(useMathText=True)
    formatter.set_scientific(True)
    p = sns.pairplot(dfna, plot_kws = {'format': formatter})
    
  3. 电流: currentplot

    目标: goalplot

1 个答案:

答案 0 :(得分:0)

您需要将格式化程序应用于pairplot创建的每个轴。实际上,边缘上的轴就足够了,但同样可以更方便地将相同的东西应用到所有轴上。我愿意:

for ax in g.axes.flatten():
    ax.ticklabel_format(style='sci', scilimits=(0,0), axis='both')