Code, notes, and data set files are placed on gitee.
Click here to enter gitee
https://gitee.com/jiangxun07/python-data-analysis.git
Article directory
0 Introduction to Seaborn
Seaborn is a statistical chart production library based on matplotlib and whose data structure is unified with pandas .
Here is a list of some seaborn features:
- Dataset-oriented interface for computing relationships among multiple variables
- Visualizing Observations and Statistics of Category Variables
- Visualize univariate or multivariate distributions and compare with their subdatasets
- Control different dependent variables of linear regression and perform parameter estimation and plotting
- Easily visualize the overall structure of complex data
- Highly abstract the production of multi-table statistical charts and simplify the visualization process
- Provides multiple built-in themes to render matplotlib image styles
- Provides palette tools to bring your data to life
The Seaborn framework is designed to mine and understand data centered on data visualization. The data set-oriented charting functions it provides mainly operate on row and column indexes and arrays, including internal semantic mapping and statistical integration of the entire data set to generate information-rich charts.
Install in terminal:pip install seaborn
Official documents: http://seaborn.pydata.org/index.html
seaborn official documents
Chinese documents: https://seaborn.apachecn.org/#/README
seaborn official Chinese documents (needs to be accelerated)
bloggers can send
official documents privately for free Dataset: https://github.com/mwaskom/seaborn-data
1 Season Getting Started
# 先看一下matplotlab的绘图
import matplotlib.pyplot as plt
import numpy as np
# 创建一些数据
# np.random.RandomState(0) 是一个随机数种子
# 通过该随机数种子生成的随机序列<正态分布>, 可以保证数据相同
rng = np.random.RandomState(0)
x = np.linspace(0, 10, num=500)
y = np.cumsum(rng.randn(500, 6), 0) # 500行数据, 6列, 按照行计算
# 用Matplotlib默认样式画图
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')
import seaborn as sns # 导入
import matplotlib.pyplot as plt
import numpy as np
sns.set() # 使用seaborn的默认配置, 可以直接对接matplotlib绘图
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')
2 seaborn drawing
2.1 Frequency histogram
sns.set() can set the following Seaborn parameters:
- context: Set the drawing context, which can be "notebook" (default value) or "paper".
- style: Set the drawing style, which can be "white" (default value), "darkgrid", "whitegrid", "dark", "white", etc.
- palette: Set the palette, which can be "default" (default value), "cubehelix", "Set1", etc.
- font: Set the font, which can be "serif" (default value), "sans-serif", or "monospace".
- axlinestyle: Set the axis line style, which can be "–", "steps-mid", etc.
- savefig.dpi: The resolution of the saved image.
- figure.figsize: Set the image size.
- print_grid: Set whether to print the grid.
- verbose: Set whether to display information prompts.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
# 取的是正态分布的值
data=np.random.multivariate_normal([0,0],[[5,2],[2,2]],size=2000)
data=pd.DataFrame(data,columns=['x','y'])
sns.set()
# 绘制平滑估计图<直方图的拟合线>
sns.kdeplot(data['x'])
sns.kdeplot(data['y'])
sns.jointplot(data=data,x='x',y='y',kind='kde')
#data 是你的数据集。
#x 和 y 是在数据集中你想要在联合图上表示的列的名字。
#kind 参数指定你想创建的联合图的类型。在这种情况下,你选择了 'kde',也就是核密度估计图。
# kind='hex' 六边形核密度图样式 kind='kde' 线条样式
# kind='hex' 六边形核密度图样式
sns.jointplot(data=data,x='x',y='y',kind='hex')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 取的是正太分布的值
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])
plt.hist(data['x'], density=True, alpha=0.5)
plt.hist(data['y'], density=True, alpha=0.1)
plt.legend('XY', ncol=2, loc='upper left')
plt.show()
2.2 Matrix diagram
sns.set()
iris=pd.read_csv('./data/iris.csv')
#sepal_length萼片长度
#sepal_width萼片宽度
#petal_length花瓣长度
#petal_width花瓣宽度
#species 种类
iris
# pairplot pair 成双成对, 描述数据两联高枝剪的关系, 线性, 非线性关系, 相关关系等等
# hue='species' 指定分类的字段, 将每个数据划分不同颜色
sns.pairplot(iris, hue='species')
2.3 Facet frequency chart
tips=pd.read_csv('./data/tips.csv')
tips
# 计算消费占总餐费的比重
tips['tip_proportion']=tips['tip']/tips['total_bill']*100
# margin_titles 显示字段标题
grid=sns.FacetGrid(data=tips,row='sex',col='time',margin_titles=True)
grid.map(plt.hist,'tip_proportion',bins=np.linspace(0,40,15))
Why do men tip so much more at night than at noon?
2.4 Box plot
tips
with sns.axes_style(style='ticks'):
sns.catplot(data=tips,x='day',y='total_bill',hue='sex',kind='box')
#kind: 绘图类型:violin 小提琴图 swarm 散点图 box 箱线图
2.5 Classification diagram
with sns.axes_style(style='ticks'):
sns.catplot(data=tips,x='day',y='total_bill',hue='sex',kind='violin')
with sns.axes_style(style='ticks'):
sns.catplot(data=tips,x='day',y='total_bill',hue='sex',kind='swarm')
sns.catplot(data=tips, x='day', y='total_bill', hue='smoker', kind='swarm')
sns.catplot(data=tips, x='day', y='total_bill', hue='smoker', kind='violin')
2.6 Joint distribution diagram
Still using tips data
sns.set()
sns.jointplot(data=tips,x='total_bill',y='tip',kind='hex')
sns.jointplot(data=tips, x='total_bill', y='tip', kind='reg')
# reg= 构建线性回归的拟合线条
- pyecharts focuses on displaying data and has dynamic data interaction effects
- matplotlib draws floor plan
- seaborn drawing research data
2.7 Palette
sns.color_palette() # 默认的调色板
sns.palplot(sns.hls_palette(8, l=0.5, s=0.8)) # l 亮度 s 饱和度