2

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

Versions:
seaborn 0.8.1
numpy 1.13.1
matplotlib 2.0.2
pandas 0.23.0

Current plot:

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

Unsuccessful attempts to reformat:

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

Current: currentplot

Goal: goalplot

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
allib
  • 23
  • 1
  • 5
  • 1
    *"I've found suggestions for other seaborn plots"* What are those suggestions? – ImportanceOfBeingErnest Jun 07 '18 at 14:50
  • Both attempts I listed were based on suggestions for other seaborn plots. https://stackoverflow.com/questions/36780948/seaborn-matplotlib-how-to-repress-scientific-notation-in-factorplot-y-axis and https://stackoverflow.com/questions/35418989/making-colorbar-with-scientific-notation-in-seaborn. Also tried to modify https://stackoverflow.com/questions/29647749/seaborn-showing-scientific-notation-in-heatmap-for-3-digit-numbers – allib Jun 07 '18 at 15:18
  • 29647749 is about heatmap annotations. 35418989 is about heatmap colorbar. 36780948 is helpful but only for a single plot. However it contains two links from which you get the general idea of setting a formatter to an axes. So the remaining problem is to get an axes `ax` from your pairplot. This is done using e.g. `g.axes[2,0]` for the lower left plot. – ImportanceOfBeingErnest Jun 07 '18 at 15:42
  • what is the problem with your second solution? It seems to be giving the correct output? – Diziet Asahi Jun 08 '18 at 08:29
  • The second solution is for a different dataset. With the larger values, the axes appear to default to scientific notation. I originally had links stating that the second image was an example of a 'correct plot' but the label seems to have been edited to show the full images – allib Jun 08 '18 at 08:33

1 Answers1

4

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')
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75