sklearn.datasets iris数据集绘图 seaborn and matplotlib

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, FancyArrowPatch, ArrowStyle
import pandas as pd
import seaborn as sns
import warnings
import numpy as np
import scipy.stats as ss
warnings.filterwarnings('ignore')

data = sns.load_dataset('iris')
df=data
df.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

sns.jointplot plt版

import seaborn as sns
sns.set(style="ticks")

iris = sns.load_dataset("iris")
g = sns.jointplot("sepal_length", "sepal_width", data=iris,
                  kind="reg", truncate=False,
                  color='m', height=6)

sns.pairplot

sns.pairplot(df, hue="species",palette='deep')
<seaborn.axisgrid.PairGrid at 0x27774c4b108>
png

sns.palette

print(sns.color_palette())
sns.palplot(sns.color_palette())
[(0.2980392156862745, 0.4470588235294118, 0.6901960784313725), (0.8666666666666667, 0.5176470588235295, 0.3215686274509804), (0.3333333333333333, 0.6588235294117647, 0.40784313725490196), (0.7686274509803922, 0.3058823529411765, 0.3215686274509804), (0.5058823529411764, 0.4470588235294118, 0.7019607843137254), (0.5764705882352941, 0.47058823529411764, 0.3764705882352941), (0.8549019607843137, 0.5450980392156862, 0.7647058823529411), (0.5490196078431373, 0.5490196078431373, 0.5490196078431373), (0.8, 0.7254901960784313, 0.4549019607843137), (0.39215686274509803, 0.7098039215686275, 0.803921568627451)]
png

matplotlib.patches : Ellipse, FancyArrowPatch, ArrowStyle

sns.set_style('ticks')
fig,ax=plt.subplots(figsize=(8,6))
ax=sns.scatterplot(data=df,x='petal_width',y='sepal_width',hue="species",palette=sns.color_palette()[:3])
means=[]
covariances=[]
labels=[(i,k) for i,k in zip(df['species'].unique(),[i for i in sns.color_palette()[:3]])]
x='petal_width'
y='sepal_width'
inputs=df.loc[:,[x,y]].values
targets=df.loc[:,'species'].values

for label in labels:
    selection = targets == label[0]
    means.append(np.mean(inputs[selection,:],axis=0))
    covariances.append(np.cov(inputs[selection,:],rowvar=0))
stdevs  = []
vectors = []
indexes = np.argsort([ np.arctan2(*mean) for mean in means ])
for mean, covariance, idx in zip(means, covariances, indexes):
    eigvals, eigvecs = np.linalg.eig(covariance)
    indexes = eigvals.argsort()[::-1]
    eigvals = eigvals[indexes]
    eigvecs = eigvecs[:, indexes]
    if all(np.sign(eigvecs[:,0]) == [-1, -1]):
        eigvecs[:,0] *= -1
    vectors.append(eigvecs[:,0])
    stdevs.append(np.sqrt(eigvals[0]))
    ax.add_patch(Ellipse(
            xy     = mean,
            width  = np.sqrt(eigvals[0]*ss.chi2.isf(0.05, 2)) * 2,
            height = np.sqrt(eigvals[1]*ss.chi2.isf(0.05, 2)) * 2,
            angle  = np.degrees(np.arctan2(*eigvecs[::-1,0])),
            fill   = False,
            edgecolor = labels[idx][1]
        ))
    for diff in (np.sqrt(eigvals) * eigvecs).transpose():
        ax.add_patch(FancyArrowPatch(
            posA = mean,
            posB = mean + diff,
            arrowstyle = ArrowStyle.Simple(
                head_width  = 8,
                tail_width  = 3,
                head_length = 8,
            ),
            facecolor = 'grey',
            edgecolor = 'white',
        ))    
        ax.plot(*mean, '*', color = 'yellow', markersize = 10, markeredgecolor = 'grey')

sns.despine()
png

Categories: Python

1 Comment

sklearn.datasets iris数据集绘图 – xinzipanghuang.home · 2020年7月24日 at 16:13

[…] 后续:利用 seaborn 库相关库进行绘制复杂图形 […]

Leave a Reply

Your email address will not be published.