当前位置:网站首页>Seaborn data visualization
Seaborn data visualization
2022-07-07 17:02:00 【En^_^ Joy】
use Seaborn Data visualization
Seaborn And Matplotlib
Matplotlib drawing
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(0, 10, 500)
y = np.cumsum(np.random.randn(500, 6), 0)
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')
# display picture
plt.show()
Seaborn
There are many advanced drawing functions , And you can rewrite Matplotlib
Default parameters , So as to use simple Matplotlib
Get better results , It can be used Seaborn
Of set()
Method to set the style
Other codes are the same as above , Just add and import this module and use set()
function
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set()
plt.figure()
x = np.linspace(0, 10, 500)
y = np.cumsum(np.random.randn(500, 6), 0)
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')
# display picture
plt.show()
Seaborn Graphic introduction
For many graphics Matplotlib Can be realized , Three uses Seaborn Will be more convenient
Frequency histogram 、KDE And density diagram
Frequency histogram
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])
for col in 'xy':
plt.hist(data[col], alpha=0.5)
# display picture
plt.show()
use KDE Get the smooth evaluation of variable distribution , adopt sns.kdeplot
Realization
for col in 'xy':
sns.kdeplot(data[col], shade=True)
use distplot
We can make the frequency histogram and KDE Combine
for col in 'xy':
sns.distplot(data[col])
If you want to kdeplot The input is a two-dimensional data set , Then you can get a two-dimensional number visualization
sns.kdeplot(data['x'],data['y'])
use sns.jointplot You can see the joint distribution of two variables and the independent distribution of one variable at the same time , Use a white background here
with sns.axes_style('white'):
sns.jointplot("x", "y", data, kind='kde')
with sns.axes_style('white'):
sns.jointplot("x", "y", data, kind='hex')
Matrix diagram
The following is a matrix diagram of the relationship between the four variables
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
iris = sns.load_dataset("iris")
print(iris.head())
''' sepal_length sepal_width petal_length petal_width species 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa '''
sns.pairplot(iris, hue='species', size=2.5)
# display picture
plt.show()
Faceted frequency histogram
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
tips = sns.load_dataset("tips")
print(tips)
''' total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4 .. ... ... ... ... ... ... ... 239 29.03 5.92 Male No Sat Dinner 3 240 27.18 2.00 Female Yes Sat Dinner 2 241 22.67 2.00 Male Yes Sat Dinner 2 242 17.82 1.75 Male No Sat Dinner 2 243 18.78 3.00 Female No Thur Dinner 2 [244 rows x 7 columns] '''
tips['tip_pct'] = 100*tips['tip']/tips['total_bill']
grid = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True)
grid.map(plt.hist, "tip_pct", bins=np.linspace(1, 40, 15))
# display picture
plt.show()
Factor map
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
tips = sns.load_dataset("tips")
tips['tip_pct'] = 100*tips['tip']/tips['total_bill']
with sns.axes_style(style='ticks'):
g = sns.factorplot("day", "total_bill", "sex", data=tips, kind="box")
g.set_axis_labels("day", "Total Bill")
# display picture
plt.show()
Joint distribution
with sns.axes_style('white'):
sns.jointplot("total_bill", "tip", data=tips, kind='hex')
with sns.axes_style('white'):
sns.jointplot("total_bill", "tip", data=tips, kind='reg')
Bar chart
:sns.factorplot
Draw a bar chart
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
planets = sns.load_dataset('planets')
print(planets.head())
''' method number orbital_period mass distance year 0 Radial Velocity 1 269.300 7.10 77.40 2006 1 Radial Velocity 1 874.774 2.21 56.95 2008 2 Radial Velocity 1 763.000 2.60 19.84 2011 3 Radial Velocity 1 326.030 19.40 110.62 2007 4 Radial Velocity 1 516.220 10.50 119.47 2009 '''
with sns.axes_style('white'):
g = sns.factorplot("year", data=planets, aspect=2, kind="count", color='steelblue')
g.set_xticklabels(step=5)
# display picture
plt.show()
with sns.axes_style('white'):
g = sns.factorplot("year", data=planets, aspect=4.0, kind="count", hue='method', order=range(2001, 2015))
g.set_ylabels('Number of Planets Discovered')
边栏推荐
- 蓝桥杯 决赛 异或变换 100分
- QT picture background color pixel processing method
- LeetCode 403. 青蛙过河 每日一题
- LeetCode 300. Daily question of the longest increasing subsequence
- Lowcode: four ways to help transportation companies enhance supply chain management
- 射线与OBB相交检测
- LeetCode 1477. Find two subarrays with sum as the target value and no overlap
- ATM system
- Introduction and use of gateway
- 测试用例管理工具推荐
猜你喜欢
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
【DesignMode】外观模式 (facade patterns)
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
Cesium(3):ThirdParty/zip. js
最新高频Android面试题目分享,带你一起探究Android事件分发机制
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
运算符
二叉搜索树(基操篇)
Binary search tree (basic operation)
字节跳动Android面试,知识点总结+面试题解析
随机推荐
测试用例管理工具推荐
掌握这套精编Android高级面试题解析,oppoAndroid面试题
【医学分割】attention-unet
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
Imitate the choice of enterprise wechat conference room
LeetCode 1186. Delete once to get the sub array maximum and daily question
LeetCode 152. Product maximum subarray daily question
Build an all in one application development platform, light flow, and establish a code free industry benchmark
Pisa-Proxy SQL 解析之 Lex & Yacc
第九届 蓝桥杯 决赛 交换次数
字节跳动Android面试,知识点总结+面试题解析
Cesium(3):ThirdParty/zip. js
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
Arduino 控制的双足机器人
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
数据中台落地实施之法
LeetCode 120. Triangle minimum path and daily question
typescript ts基础知识之tsconfig.json配置选项