Python seaborn matplotlib 对数据进行图表分析
2017-07-27 20:34
1231 查看
matplotlib官网
基于matplotlib的seaborn官网
图中可以看出特征11和特征14几乎将分类目标class的分布线分了。而特质12和特征19负相关,特征14与特征19强相关,注意对有些模型(朴素贝叶斯分类器假设特征独立)会有影响。
对角线为特征,左下角为相关性热点图,有上角为相关系数。
为了看到分类目标,使用:
基于matplotlib的seaborn官网
import seaborn as sns import matplotlib.pyplot as plt
import seaborn as sns sns.set_style("whitegrid") # 设置主题,可选项有darkgrid , whitegrid , dark , white ,和 ticks sns.set(style="dark", palette="muted", color_codes=True) # 通过参数设置主题
一、plt.hist
g = sns.FacetGrid(train_data, col='Survived') # col,row,hue参数用于定义绘制数据的子集。由于survived有两个参数,会有两个图 g.map(plt.hist, 'Age', bins=20) # hist直方图, ‘Age’为横坐标, bins绘制的直方数量 plt.show() #左图为在Survived=0的子集内,Age和数量的分布图。
grid = sns.FacetGrid(train_data, hue='Survived') # hue参数的把两个直方图画在同一张图上,其中有size参数决定高度,aspect长宽比,width=size*aspect grid.map(plt.hist, 'Age', alpha=0.5, bins=20) grid.add_legend() # 将hue标签显示在图上 plt.show()
二、pointplot
显示数据的均值和偏差grid = sns.FacetGrid(train_data, row='Embarked', size=2.2, aspect=1.6) grid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep') # map函数将grid的子集应用于pointplot函数,其后面参数是pointplot的参数,分别为横坐标,纵坐标,hue。palette是调色板折线的颜色。 grid.add_legend() plt.show()
sns.pointplot(x="Pclass", y="Survived", hue="Sex", data=data_train,palette={"male": "blue", "female": "pink"},markers=["*", "o"], linestyles=["-", "--"]);
三、barplot
显示数据的均值和偏差grid = sns.FacetGrid(train_data, col='Embarked', hue='Survived', palette={0: 'k', 1: 'w'}) grid.map(sns.barplot, 'Sex', 'Fare', alpha=.5) #ci=sd表示用竖线显示标准偏差,=None不显示 grid.add_legend() plt.show()
四、pairplot:
查看数据相关性。_ = sns.pairplot(df[:50], vars=[8, 11, 12, 14, 19], hue="class", size=1.5) # vars需绘制的列名,hue使用的是二分类的标记 # diag_kind="kde"能将对角线图换成kde
图中可以看出特征11和特征14几乎将分类目标class的分布线分了。而特质12和特征19负相关,特征14与特征19强相关,注意对有些模型(朴素贝叶斯分类器假设特征独立)会有影响。
五、corrplot:
主要用于分析特征的两两相关。sns.pairplot(iris)
对角线为特征,左下角为相关性热点图,有上角为相关系数。
六、heatmap
主要用于分析特征的两两相关。colormap = plt.cm.viridis plt.figure(figsize=(12,12)) plt.title('Pearson Correlation of Features', y=1.05, size=15) sns.heatmap(train.astype(float).corr(),linewidths=0.1,vmax=1.0, square=True, cmap=colormap, linecolor='white', annot=True) # linewidths,linecolor划分线的宽度和颜色,annot是否在方格里注释数据,vmin, vmax相关度最大值和最小值,此处限制1.0最大,可见图中图谱最大为1.0
七、jointplot,scatter
查看数据分布和离群点sns.jointplot(x="SepalLengthCm", y="SepalWidthCm", data=iris, size=5)
为了看到分类目标,使用:
sns.FacetGrid(iris, hue="Species", size=5) \ .map(plt.scatter, "SepalLengthCm", "SepalWidthCm") \ .add_legend()
八、boxplot
分析单一特征。sns.boxplot(x="Species", y="PetalLengthCm", data=iris)
ax = sns.boxplot(x="Species", y="PetalLengthCm", data=iris) ax = sns.stripplot(x="Species", y="PetalLengthCm", data=iris, jitter=True, edgecolor="gray") # jitter不让点分布在一条垂直线上
九、violinplot
类似于boxplot,分析单一变量。sns.violinplot(x="Species", y="PetalLengthCm", data=iris, size=6)
十、kdeplot
分析单一变量。sns.FacetGrid(iris, hue="Species", size=6) \ .map(sns.kdeplot, "PetalLengthCm") \ .add_legend()
十一、pandas画图
# Andrews Curves involve using attributes of samples as coefficients for Fourier series # and then plotting these from pandas.tools.plotting import andrews_curves andrews_curves(iris.drop("Id", axis=1), "Species")
# 将特征作为横坐标,绘制每个样例取值 from pandas.tools.plotting import parallel_coordinates parallel_coordinates(iris.drop("Id", axis=1), "Species")
将特征作为一个点绘制,通过相对权值将样例绘制成点。 from pandas.tools.plotting import radviz radviz(iris.drop("Id", axis=1), "Species")
十二、WordCloud云图
用于分析文本中word出现的频率,越高,显示越大。clean_mask=np.array(Image.open("../input/imagesforkernal/safe-zone.png")) clean_mask=clean_mask[:,:,1] #wordcloud for clean comments subset=train[train.clean==True] text=subset.comment_text.values wc= WordCloud(background_color="black",max_words=2000,mask=clean_mask,stopwords=stopword) wc.generate(" ".join(text)) plt.figure(figsize=(20,10)) plt.axis("off") plt.title("Words frequented in Clean Comments", fontsize=20) plt.imshow(wc.recolor(colormap= 'viridis' , random_state=17), alpha=0.98) plt.show()
相关文章推荐
- 使用python中的matplotlib进行绘图分析数据
- python pandas做数据分析视图分析matplotlib,seaborn模块使用
- Python进阶(三十九)-数据可视化の使用matplotlib进行绘图分析数据
- 【利用python进行数据分析-学习记录】python-matplotlib中Basemap插件的安装
- 使用python中的matplotlib进行绘图分析数据
- matplotlib -- 使用python中的matplotlib进行绘图分析数据
- 基于Python的数据可视化 matplotlib seaborn pandas
- Python点滴(三)—pandas数据分析与matplotlib画图
- Python 数据分析之matplotlib
- 获取博客积分排名,存入数据库,读取数据进行绘图(python,selenium,matplotlib)
- python数据分析——matplotlib生成折线图,散点图和直方图
- python数据分析之数据可视化matplotlib
- Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
- Python数据可视化matplotlib(一)—— 图表的基本元素
- python数据挖掘课程 十一.Pandas、Matplotlib结合SQL语句可视化分析
- python数据分析复盘——数据分析相关库之Matplotlib
- Python进阶(四十)-数据可视化の使用matplotlib进行绘图
- [python之数据分析] 基础篇1- Numpy,Scipy,Matplotlib 快速入门攻略
- python数据分析学习笔记-Numpy-Matplotlib-Pandas
- python数据挖掘课程 十二.Pandas、Matplotlib结合SQL语句对比图分析