user8270077
user8270077

Reputation: 5071

Customizing pairplot in matplotlib - seaborn

I have a difficulty in the customization of the pairplot.

1) The kde plots in the diagonal are not colored by Class

2) The plots in the diagonal do not fit and get cropped

3) I would like to control the font size of the title of the legend

Finally a get a message I do not understand:

C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\nonparametric\kde.py:494: RuntimeWarning: invalid value encountered in true_divide
  binned = fast_linbin(X,a,b,gridsize)/(delta*nobs)
C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:34: RuntimeWarning: invalid value encountered in double_scalars
  FAC1 = 2*(np.pi*bw/RANGE)**2

My code using a reproducible example is here:

import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns
%matplotlib inline
from sklearn import datasets

iris = datasets.load_iris() 

df = np.concatenate( (iris.data,  np.matrix(iris.target).T), axis = 1)

df1 = pd.DataFrame(df, columns = iris.feature_names + ['Class'])

SMALL_SIZE = 20
MEDIUM_SIZE = 25
BIGGER_SIZE = 30

plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=MEDIUM_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

sns.pairplot(df1, hue = 'Class', diag_kind = 'kde', plot_kws = {'alpha': 0.6, 's': 80, 'edgecolor': 'k'}, size = 6);

enter image description here

Upvotes: 2

Views: 3093

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339290

To solve 1) and 2) update your seaborn to version 0.8.1. Possibly update matplotlib as well.

To solve 3) assign the pairplot to a variable g and call

g._legend.get_title().set_fontsize(20)

For the warning you get, this is due to the "Class" column being part of the grid. This does not make too much sense anyways, so leave that one out by specifying the variables to grid, in this case vars = iris.feature_names,.

Complete code:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn import datasets

iris = datasets.load_iris() 

df = np.concatenate( (iris.data,  np.matrix(iris.target).T), axis = 1)

df1 = pd.DataFrame(df, columns = iris.feature_names + ['Class'])


g = sns.pairplot(df1, vars = iris.feature_names, hue = 'Class', diag_kind = 'kde', 
             plot_kws = {'alpha': 0.6, 's': 80, 'edgecolor': 'k'}, size = 2);

g._legend.get_title().set_fontsize(20)    
plt.show()

enter image description here

Upvotes: 3

Related Questions