How to draw Matrix Diagram by seaborn in Python
This article mainly introduces the Python seaborn how to draw a matrix diagram, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.
A quick overview of the content of this article
1. Preparation of drawing data
Or use the Iris iris dataset
# Import the library to be used in this post The statement is as follows: import matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom pandas import Series,DataFramefrom sklearn import datasetsimport seaborn as sns # Import Iris iris dataset (method 1) # this method is more helpful to understand the dataset iris=datasets.load_iris () x, y = iris.data,iris.targety_1 = np.array (['setosa' if iota 0 else' versicolor' if iota 1 else 'virginica' for i in y]) pd_iris = pd.DataFrame ((x, y_1.reshape (150 Magne1) Columns= ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)' 'class']) # astype modifies the data type object in pd_iris to be float64pd_iris [' sepal length (cm)'] = pd_iris ['sepal length (cm)'] .astype ('float64') pd_iris [' sepal width (cm)'] = pd_iris ['sepal width (cm)'] .astype ('float64') pd_iris [' petal length (cm)'] = pd_iris ['petal length (cm)'] .astype ('float64') pd_iris [' petal width' (cm)'] = pd_iris ['petal width (cm)'] .astype ('float64') # Import Iris iris dataset (method 2) # import seaborn as sns#iris_sns = sns.load_dataset ("iris")
Simple statistics of data set
2 、 seaborn.pairplot
Syntax: seaborn.pairplot (data, hue=None, hue_order=None, palette=None, vars=None, x_vars=None, y_vars=None, kind='scatter', diag_kind='auto', markers=None, height=2.5, aspect=1, corner=False, dropna=True, plot_kws=None, diag_kws=None, grid_kws=None, size=None)
G = sns.pairplot (pd_iris) g.fig.set_size_inches (12j 12) # figure size sns.set (style='whitegrid',font_scale=1.5) # text size
The four diagonal diagrams are the distribution histograms of the variables themselves.
The 12 sheets of non-diagonal lines are the relationship between one variable and another.
Plus the classification variable g = sns.pairplot (pd_iris, hue='class'# classified according to three kinds of flowers) sns.set (style='whitegrid') g.fig.set_size_inches (12pm 12) sns.set (style='whitegrid',font_scale=1.5)
Modify the color palette
You can use Matplotlib, seaborn, color number list and other color plates.
Please refer to the seaborn palette of Python visual learning
Import palettable g = sns.pairplot (pd_iris, hue='class', palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors,#palettable color plate) sns.set (style='whitegrid') g.fig.set_size_inches (12 Magi 12) sns.set (style='whitegrid',font_scale=1.5)
G = sns.pairplot (pd_iris, hue='class', palette='Set1',#Matplotlib color) sns.set (style='whitegrid') g.fig.set_size_inches (122.12) sns.set (style='whitegrid',font_scale=1.5)
G = sns.pairplot (pd_iris, hue='class', palette= ['# dc2624','# 2b4750colors,'# 45a0a2'], # use incoming color list) sns.set (style='whitegrid') g.fig.set_size_inches (12) sns.set (style='whitegrid',font_scale=1.5)
The same subset import palettableg = sns.pairplot (pd_iris, hue='class', palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors, vars= ['sepal length (cm)', 'sepal width (cm)'], # x Select the same subset in the y-axis direction) sns.set (style='whitegrid') g.fig.set_size_inches (12pm 6) sns.set (style='whitegrid',font_scale=1.5)
Select different subsets import palettableg = sns.pairplot (pd_iris, hue='class', palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors, xray vars = ['sepal length (cm)', 'sepal width (cm)'] in the y-axis direction, and select different subsets in the y-axis direction of # x-cm = ['petal length (cm)', 'petal width (cm)'] ) sns.set (style='whitegrid') g.fig.set_size_inches (12pm 6) sns.set (style='whitegrid',font_scale=1.5)
Non-diagonal scatter plot plus trend line import palettableg = sns.pairplot (pd_iris, hue='class', palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors, kind='reg',# defaults to scatter Reg plus trend line) sns.set (style='whitegrid') g.fig.set_size_inches (12) sns.set (style='whitegrid',font_scale=1.5)
The way of drawing four diagonals
Optional parameters are 'auto',' hist' (default), 'kde', None.
Import palettableg = sns.pairplot (pd_iris, hue='class', palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors, diag_kind='hist',#hist histogram) sns.set (style='whitegrid') g.fig.set_size_inches (12) sns.set (style='whitegrid',font_scale=1.5)
Display only the triangular figure under the grid import palettableg = sns.pairplot (pd_iris, hue='class', palette='Set1', corner=True# graphic display lower left corner) g.fig.set_size_inches (122.12) sns.set (font_scale=1.5)
Graphic appearance setting import palettableg = sns.pairplot (pd_iris, hue='class', palette='Set1', markers= ['$\ clubsuit$','.','+'], # marker plot_kws=dict of scatter chart (slots 50, edgecolor= "r", linewidth=1) # non-diagonal figure marker size, outer frame, outer frame linewidth diag_kws=dict (shade=True) # whether the diagonal nuclear density map is filled) g.fig.set_size_inches (12) sns.set (font_scale=1.5)
3. Seaborn.PairGrid (more flexible matrix drawing)
Seaborn.PairGrid (data, hue=None, hue_order=None, palette=None, hue_kws=None, vars=None, x_vars=None, y_vars=None, corner=False, diag_sharey=True, height=2.5, aspect=1, layout_pad=0, despine=True, dropna=True, size=None)
Each subgraph draws the same type of graph g = sns.PairGrid (pd_iris, hue='class', palette='husl',) g = g.map (plt.scatter) # map each subgraph draws the same type of graph g = g.add_legend () g.fig.set_size_inches (12) sns.set (style='whitegrid',font_scale=1.5)
Draw different types of diagonals and non-diagonals g = sns.PairGrid (pd_iris, hue='class', palette='Set1',) g = g.map_diag (plt.hist) # diagonal histogram g = g.map_offdiag (plt.scatter) # non-diagonal scatter g = g.add_legend () g.fig.set_size_inches (12) sns.set (style='whitegrid') Font_scale=1.5)
Draw different types of diagonals above diagonals, diagonals and below diagonals g = sns.PairGrid (pd_iris, hue='class',) g = g.map_upper (sns.scatterplot) g = g.map_lower (sns.kdeplot, colors= "C0") g = g.map_diag (sns.kdeplot, lw=2) 3 draw nuclear density maps g = g.add_legend () # add legend sns.set (style='whitegrid',font_scale=1.5)
Other parameters modify g = sns.PairGrid (pd_iris, hue='class', palette='Set1', hue_kws= {"marker": ["^", "s", "D"]}, # set marker diag_sharey=False,) g = g.map_upper (sns.scatterplot,edgecolor= "w", slots 40) # set point size Frame color g = g.map_lower (sns.kdeplot, colors= "# 01a2d9") # set lower triangle color g = g.map_diag (sns.kdeplot, lw=3) # diagonal color g = g.add_legend () # add legend g.fig.set_size_inches (12) sns.set (style='whitegrid',font_scale=1.5)
Thank you for reading this article carefully. I hope the article "how to draw the Matrix Diagram of seaborn in Python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!