[seaborn] seaborn学习笔记2-散点图Scatterplot
2 散点图Scatterplot(代码下载)
散点图能够显示2个维度上2组数据的值。每个点代表一个观察点。X(水平)和Y(垂直)轴上的位置表示变量的值。研究这两个变量之间的关系是非常有用的。在seaborn中通过regplot和lmplot制作散点图,regplot和lmplot核心功能相近,regplot相对简单点,如果要定制图像更深层次功能,需要使用lmplot。此外也用Pairplot制作多变量图。该章节主要内容有:
- 基础散点图绘制 Basic scatterplot
- 更改标记参数 Control marker features
- 自定义线性回归拟合 Custom linear regression fit
- 使用分类变量为散点图着色 Use categorical variable to color scatterplot
- 坐标轴范围设置 Control axis limits of plot
- 在散点图上添加文本注释 Add text annotation on scatterplot
- 自定义相关图 Custom correlogram
#调用seaborn
import seaborn as sns
#调用seaborn自带数据集
df = sns.load_dataset('iris')
#显示数据集
df.head()
\3cpre>\3ccode>.dataframe tbody tr th { vertical-align: top }
.dataframe thead th { text-align: right }
| 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 |
1.基础散点图绘制 Basic scatterplot
# 使用regplot()函数制作散点图。您必须提供至少2个列表:X轴和Y轴上的点的位置。
# 默认情况下绘制线性回归拟合直线,可以使用fit_reg = False将其删除
# use the function regplot to make a scatterplot 有回归曲线
# scipy<1.2会有warning
sns.regplot(x=df["sepal_length"], y=df["sepal_width"]);
C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

# Without regression fit 无回归曲线
sns.regplot(x=df["sepal_length"], y=df["sepal_width"], fit_reg=False);

2. 更改标记参数 Control marker features
# 可以散点图自定义颜色,透明度,形状和大小
# Change shape of marker控制散点的形状
sns.regplot(x=df["sepal_length"], y=df["sepal_width"], marker="+", fit_reg=False);

# List of available shapes 可用的形状查看
import matplotlib
all_shapes=matplotlib.markers.MarkerStyle.markers.keys()
all_shapes
dict_keys(['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd', '|', '_', 'P', 'X', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'None', None, ' ', ''])
# More marker customization,更具scatter_kws参数控制颜色,透明度,点的大小
sns.regplot(x=df["sepal_length"], y=df["sepal_width"], fit_reg=False, scatter_kws={"color":"darkred","alpha":0.3,"s":20});

3. 自定义线性回归拟合 Custom linear regression fit
# 您可以自定义seaborn提出的回归拟合的外观。在此示例中,颜色,透明度和宽度通过line_kws = {}选项进行控制。
sns.regplot(x=df["sepal_length"], y=df["sepal_width"], line_kws={"color":"r","alpha":0.7,"lw":5});

4. 使用分类变量为散点图着色 Use categorical variable to color scatterplot
- 每组映射一种颜色 Map a color per group
- 每组映射一个标记 Map a marker per group
- 使用其他调色板 Use another palette
- 控制每组的颜色 Control color of each group
# 每组映射一种颜色 Map a color per group
# Use the 'hue' argument to provide a factor variable hue设置species不同种类的的颜色
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False);
# Move the legend to an empty part of the plot 需要通过matplotlib更改legend的位置
import matplotlib.pyplot as plt
plt.legend(loc='best');

# 每组映射一个标记 Map a marker per group
# give a list to the marker argument 通过hue设定颜色,markes设定各点的形状
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, markers=["o", "x", "1"])
# Move the legend to an empty part of the plot
plt.legend(loc='lower right');

# 使用其他调色板 Use another palette
# Use the 'palette' argument 不同种类设定不同的颜色,颜色类型使用palette设定调色板颜色
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, palette="Set2")
# Move the legend to an empty part of the plot
plt.legend(loc='lower right');

# 控制每组的颜色 Control color of each group
# Provide a dictionary to the palette argument 调色盘使用自定义颜色
dict_color=dict(setosa="#9b59b6", virginica="#3498db", versicolor="#95a5a6")
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, palette=dict_color)
# Move the legend to an empty part of the plot
plt.legend(loc='lower right');

5. 坐标轴范围设置 Control axis limits of plot
# basic scatterplot
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False)
# control x and y limits 设置轴的范围,不过需要调用matplotlib.pyplot 模块,通常都是matplotlib和seaborn一起用
plt.ylim(0, 20)
plt.xlim(0, None)
(0, 8.122715679666298)

6. 在散点图上添加文本注释 Add text annotation on scatterplot
- 添加一个注释 Add one annotation
- 添加多个注释 Use a loop to annotate each marker
# 添加一个注释 Add one annotation
import pandas as pd
# 制作数据集
df_test = pd.DataFrame({
'x': [1, 1.5, 3, 4, 5],
'y': [5, 15, 5, 10, 2],
'group': ['A','other group','B','C','D']})
# 画散点图
p1=sns.regplot(data=df_test, x="x", y="y", fit_reg=False, marker="o", color="skyblue", scatter_kws={'s':400});
# 添加注释
p1.text(3+0.2, 4.5, "An annotation", horizontalalignment='left', size='medium', color='black', weight='semibold')
Text(3.2, 4.5, 'An annotation')

# 添加多个注释 Use a loop to annotate each marker
# basic plot
p1=sns.regplot(data=df_test, x="x", y="y", fit_reg=False, marker="o", color="skyblue", scatter_kws={'s':400})
# add annotations one by one with a loop
for line in range(0,df_test.shape[0]):
p1.text(df_test.x[line]+0.2, df_test.y[line], df_test.group[line], horizontalalignment='left', size='medium', color='black', weight='semibold')

7. 自定义相关图 Custom correlogram
- 有回归方程的散点相关图 correlogram with regression
- 无回归方程的散点相关图 correlogram without regression
- 在相关图上表示组 Represent groups on correlogram
- 相关图子图设置 Kind of plot for the diagonal subplots
- 子图参数设置 parameters adjustment of subplots
# 有回归方程的散点相关图 correlogram with regression
# library & dataset
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('iris')
# with regression 有回归方程的散点相关图
# 正对角线上的图表示数据频次的直方图,其他表示散点图
sns.pairplot(df, kind="reg");

# 无回归方程的散点相关图 correlogram without regression
sns.pairplot(df, kind="scatter");

# 在相关图上表示组 Represent groups on correlogram
# 通过hue设定种类,markers不同种类的点的表示方式
# 对角线为核密度图
sns.pairplot(df, kind="scatter", hue="species", markers=["o", "s", "D"], palette="Set2")
<seaborn.axisgrid.PairGrid at 0x21cc5179710>

# 在相关图上表示组 Represent groups on correlogram
# you can give other arguments with plot_kws plot_kws更改散点图的参数
sns.pairplot(df, kind="scatter", hue="species",plot_kws=dict(s=80, edgecolor="white", linewidth=3));

# 相关图子图设置 Kind of plot for the diagonal subplots
# diag_kind有auto,hist,kde选项,hist为直方图,kde为散点图
sns.pairplot(df, diag_kind="hist");

# 子图参数设置 parameters adjustment of subplots
# You can custom it as a density plot or histogram so see the related sections 通过diag_kws调整子图参数
sns.pairplot(df, diag_kind="kde", diag_kws=dict(shade=True, bw=.05, vertical=False));

[seaborn] seaborn学习笔记2-散点图Scatterplot的更多相关文章
- SAS学习笔记21 散点图、条形图
- [seaborn] seaborn学习笔记0-seaborn学习笔记章节
seaborn学习笔记章节 seaborn是一个基于matplotlib的Python数据可视化库.seaborn是matplotlib的高级封装,可以绘制有吸引力且信息丰富的统计图形.相对于matp ...
- python数据分析入门学习笔记
学习利用python进行数据分析的笔记&下星期二内部交流会要讲的内容,一并分享给大家.博主粗心大意,有什么不对的地方欢迎指正~还有许多尚待完善的地方,待我一边学习一边完善~ 前言:各种和数据分 ...
- python数据分析入门学习笔记儿
学习利用python进行数据分析的笔记儿&下星期二内部交流会要讲的内容,一并分享给大家.博主粗心大意,有什么不对的地方欢迎指正~还有许多尚待完善的地方,待我一边学习一边完善~ 前言:各种和数据 ...
- 学习笔记之Data Visualization
Data visualization - Wikipedia https://en.wikipedia.org/wiki/Data_visualization Data visualization o ...
- 学习笔记之Data Science
Data science - Wikipedia https://en.wikipedia.org/wiki/Data_science Data science, also known as data ...
- Matplotlib学习笔记(一)
原 matplotlib学习笔记 参考:Python数据科学入门教程 Python3.6.1 jupyter notebook .caret, .dropup > .btn > .ca ...
- 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据
机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...
- 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)
机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...
随机推荐
- Linux系统管理_磁盘管理——敬请期待!!!
df 显示磁盘空间使用情况 -a 全部文件系统列表 -h 以方便阅读的方式显示 -T 列出文件系统类型 du 查看目录和文件的磁盘空间使用情况 -a 显示目录中所有文件大小 -h 以易读方式显示文件大 ...
- 算法设计(动态规划实验报告) 基于动态规划的背包问题、Warshall算法和Floyd算法
一.名称 动态规划法应用 二.目的 1.掌握动态规划法的基本思想: 2.学会运用动态规划法解决实际设计应用中碰到的问题. 三.要求 1.基于动态规划法思想解决背包问题(递归或自底向上的实现均可): 2 ...
- XPAND模板语言语法1.0
XPAND模板语言语法1.0 Xpand模板语言一般写在以.xpt为结尾的文本文件中 ,以"« »" 作为开头和结尾 .Xpand语言主要包括以下几个标签: «IMPORT», ...
- 如何通过 C#/VB.NET 重命名 Excel 表格并设置选项卡颜色
在 Excel 文件中创建多个工作表可以使数据更加井然有序.例如,可以为不同的区域.不同的月份/年份或不同的项目等创建不同的工作表.但要区分多个工作表,则需要更改它们的名称.同时,设置不同的选项卡颜色 ...
- 题解 P4058 [Code+#1]木材
前言 这什么题啊,不就是个二分答案我从65到100都经历了一遍--(瞬间气哭) \(\sf {Solution}\) 题目理解起来不难的,大意就懒得写了. 一眼二分答案. 此题属于在形如 \(\{0, ...
- HTML躬行记(4)——Web音视频基础
公司目前的业务会接触比较多的音视频,所以有必要了解一些基本概念. 文章涉及的一些源码已上传至 Github,可随意下载. 一.基础概念 本节音视频的基础概念摘自书籍<FFmpeg入门详解 音视频 ...
- JS数据结构与算法-数组结构
数组结构 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构. 数组通常情况下用于存储一系列同一种数据类型的值. 但在JavaScript里,也可以在数组中保存不同类型的值. 但我们 ...
- Redis系列10:HyperLogLog实现海量数据基数统计
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...
- perl按照健值排序
perl中如何将按hash key值排序 BRUSH2 10752 11263 BUFFER 11264 11775 MEGASONIC 11776 12287 INPUTMODULE 12800 1 ...
- CB利用链及无依赖打Shiro
前言 前面已经学习了CC1到CC7的利用链,其中在CC2中认识了java.util.PriorityQueue ,它在Java中是一个优先队列,队列中每一个元素有自己的优先级.在反序列化这个对象时,为 ...