首页 文章

在matplotlib中定制配对图 - seaborn

提问于
浏览
0

我对配对图的定制有困难 .

1)对角线上的kde图未按类别着色

2)对角线上的图形不适合并被裁剪

3)我想控制图例 Headers 的字体大小

最后得到一条我不明白的消息:

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

我的代码使用可重现的示例如下:

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

您的建议将不胜感激 .

1 回答

  • 1

    要解决1)和2)将你的seaborn更新到0.8.1版本 . 也可能更新matplotlib .

    要解决3)将配对图分配给变量 g 并调用

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

    对于您收到的警告,这是由于"Class"列是网格的一部分 . 这无论如何都没有多大意义,所以通过将变量指定为网格来保留它,在本例中为 vars = iris.feature_names, .

    完整代码:

    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

相关问题