[seaborn] seaborn学习笔记9-绘图实例(1) Drawing example(1)
文章目录
- 9 绘图实例(1) Drawing example(1)
- 1. Anscombe’s quartet(lmplot)
- 2. Color palette choices(barplot)
- 3. Different cubehelix palettes(kdeplot)
- 4. Distribution plot options(distplot)
- 5. Timeseries plot with error bands(lineplot)
- 6. FacetGrid with custom projection(FacetGrid)
- 7. FacetGrid with custom projection(FacetGrid)
- 8. Line plots on multiple facets(relplot)
- 9. Grouped barplots(catplot)
- 10. Grouped boxplots(boxplot)
9 绘图实例(1) Drawing example(1)
(代码下载)
本文主要讲述seaborn官网相关函数绘图实例。具体内容有:
- Anscombe’s quartet(lmplot)
- Color palette choices(barplot)
- Different cubehelix palettes(kdeplot)
- Distribution plot options(distplot)
- Timeseries plot with error bands(lineplot)
- FacetGrid with custom projection(FacetGrid)
- Facetting histograms by subsets of data(FacetGrid)
- Line plots on multiple facets(relplot)
- Grouped barplots(catplot)
- Grouped boxplots(boxplot)
# import packages
# from IPython.core.interactiveshell import InteractiveShell
# InteractiveShell.ast_node_interactivity = "all"
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
sns.set(style="ticks")
1. Anscombe’s quartet(lmplot)
# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")
df.head()
# Show the results of a linear regression within each dataset 显示各组的回归拟合结果
# col确认分组类别,hue设置绘图颜色类别.col_wrap设置每行多少图像
# ci置信区间大小,palette颜色设置,height设置每张图的尺寸
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
col_wrap=2, ci=None, palette="muted", height=4,
scatter_kws={"s": 50, "alpha": 1});
2. Color palette choices(barplot)
#context控制上下文本参数
sns.set(style="white", context="talk")
rs = np.random.RandomState(8)
# Set up the matplotlib figure 设置matplotlib绘图参数
f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(7, 5), sharex=True)
# Generate some sequential data 建立数据集
x = np.array(list("ABCDEFGHIJ"))
y1 = np.arange(1, 11)
# 第一个条形图
sns.barplot(x=x, y=y1, palette="rocket", ax=ax1);
# 绘制平行于x轴的水平参考线
ax1.axhline(0, color="k", clip_on=False)
# 添加标签
ax1.set_ylabel("Sequential")
# Center the data to make it diverging
y2 = y1 - 5.5
sns.barplot(x=x, y=y2, palette="vlag", ax=ax2)
ax2.axhline(0, color="k", clip_on=False)
ax2.set_ylabel("Diverging")
# Randomly reorder the data to make it qualitative 生成数据
y3 = rs.choice(y1, len(y1), replace=False)
sns.barplot(x=x, y=y3, palette="deep", ax=ax3)
ax3.axhline(0, color="k", clip_on=False)
ax3.set_ylabel("Qualitative")
# Finalize the plot 绘图最后参数调整
# 去掉了上、右和下边界。注意,这里despine()函数默认会去除上方和右侧的边界,
# 如果想要去掉左边和下方的边界,就需要额外指定left=True, bottom=True
sns.despine(bottom=True)
# 去除了所有的纵轴刻度尺
plt.setp(f.axes, yticks=[])
# ight_layout设置了每行坐标轴(子图)之间的间隔,h_pad确定上下间隔
plt.tight_layout(h_pad=2)
3. Different cubehelix palettes(kdeplot)
sns.set(style="dark")
rs = np.random.RandomState(50)
# Set up the matplotlib figure 设置图像
f, axes = plt.subplots(3, 3, figsize=(9, 9), sharex=True, sharey=True)
# Rotate the starting point around the cubehelix hue circle
for ax, s in zip(axes.flat, np.linspace(0, 3, 10)):
# Create a cubehelix colormap to use with kdeplot 颜色设置
cmap = sns.cubehelix_palette(start=s, light=1, as_cmap=True)
# Generate and plot a random bivariate dataset
x, y = rs.randn(2, 50)
# 核函数估计图制作
sns.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=ax)
ax.set(xlim=(-3, 3), ylim=(-3, 3))
f.tight_layout()
4. Distribution plot options(distplot)
sns.set(style="white", palette="muted", color_codes=True)
rs = np.random.RandomState(10)
# Set up the matplotlib figure 设置matplolitlib参数
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
# 去除边框
sns.despine(left=True)
# Generate a random univariate dataset
d = rs.normal(size=100)
# Plot a simple histogram with binsize determined automatically 直方图
sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
# Plot a kernel density estimate and rug plot 画核密度曲线
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
# Plot a filled kernel density estimate
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
# Plot a historgram and kernel density estimate
sns.distplot(d, color="m", ax=axes[1, 1])
# 隐藏y轴
plt.setp(axes, yticks=[])
plt.tight_layout()
5. Timeseries plot with error bands(lineplot)
sns.set(style="darkgrid")
# Load an example dataset with long-form data
fmri = sns.load_dataset("fmri")
# Plot the responses for different events and regions 线条图
sns.lineplot(x="timepoint", y="signal",
hue="region", style="event",
#置信区间设置
#ci=None,
data=fmri);
6. FacetGrid with custom projection(FacetGrid)
sns.set()
# Generate an example radial datast 随机生成数据
r = np.linspace(0, 10, num=100)
df = pd.DataFrame({'r': r, 'slow': r, 'medium': 2 * r, 'fast': 4 * r})
# Convert the dataframe to long-form or "tidy" format
df = pd.melt(df, id_vars=['r'], var_name='speed', value_name='theta')
# Set up a grid of axes with a polar projection 建立坐标轴
# col表示使用什么分图,这里是speed,并用hue设置不同类对应不同颜色
# subplot_kws确定坐标类型,这里是极坐标
g = sns.FacetGrid(df, col="speed", hue="speed",
subplot_kws=dict(projection='polar'), height=4.5,
sharex=False, sharey=False, despine=False)
# Draw a scatterplot onto each axes in the grid
# 在实例化后的坐标轴上画图
g.map(sns.scatterplot, "theta", "r");
7. FacetGrid with custom projection(FacetGrid)
sns.set(style="darkgrid")
tips = sns.load_dataset("tips")
# 初始化坐标轴,margin_titles行变量的标题被绘制到最后一列的右侧
g = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True)
bins = np.linspace(0, 60, 13)
g.map(plt.hist, "total_bill", color="steelblue", bins=bins);
8. Line plots on multiple facets(relplot)
sns.set(style="ticks")
dots = sns.load_dataset("dots")
# Define a palette to ensure that colors will be
# shared across the facets
# 设置颜色
palette = dict(zip(dots.coherence.unique(),
sns.color_palette("rocket_r", 6)))
# Plot the lines on two facets
sns.relplot(x="time", y="firing_rate",
hue="coherence", size="choice", col="align",
# size_order画图顺序
size_order=["T1", "T2"], palette=palette,
# aspect图的比例,facet_kws每个图参数设置
height=5, aspect=0.75, facet_kws=dict(sharex=False),
# kind画图类型,legend图例类型
kind="line", legend="brief", data=dots);
9. Grouped barplots(catplot)
sns.set(style="whitegrid")
# Load the example Titanic dataset 读取数据
titanic = sns.load_dataset("titanic")
# Draw a nested barplot to show survival for class and sex
# catplot分类数据作图
g = sns.catplot(x="class", y="survived", hue="sex", data=titanic,
height=6, kind="bar", palette="muted")
# 设置轴框
g.despine(left=True)
# 设置标签
g.set_ylabels("survival probability");
10. Grouped boxplots(boxplot)
sns.set(style="ticks", palette="pastel")
# Load the example tips dataset
tips = sns.load_dataset("tips")
# Draw a nested boxplot to show bills by day and time
# 画箱形图 hue颜色
sns.boxplot(x="day", y="total_bill",
hue="smoker", palette=["m", "g"],
data=tips)
# offset 两轴偏移绝对距离,trim只显示主要刻度
sns.despine(offset=10,trim=True)
[seaborn] seaborn学习笔记9-绘图实例(1) Drawing example(1)的更多相关文章
- [seaborn] seaborn学习笔记11-绘图实例(3) Drawing example(3)
11 绘图实例(3) Drawing example(3)(代码下载) 本文主要讲述seaborn官网相关函数绘图实例.具体内容有: Plotting a diagonal correlation m ...
- [seaborn] seaborn学习笔记12-绘图实例(4) Drawing example(4)
文章目录 12 绘图实例(4) Drawing example(4) 1. Scatterplot with varying point sizes and hues(relplot) 2. Scat ...
- [seaborn] seaborn学习笔记10-绘图实例(2) Drawing example(2)
文章目录 10 绘图实例(2) Drawing example(2) 1. Grouped violinplots with split violins(violinplot) 2. Annotate ...
- webgl学习笔记二-绘图多点
写在前面 建议先看下第一篇webgl学习笔记一-绘图单点 第一篇文章,介绍了如何用webgl绘图一个点.接下来本文介绍的是如何绘制多个点.形成一个面. webgl提供了一种很方便的机制,即缓冲区对象, ...
- Android应用开发学习笔记之绘图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.绘图常用类介绍 在Android中绘图时,常用到的几个类是Paint.Canvas.Bitmap和Bitmapt ...
- iOS学习笔记08-Quartz2D绘图
一.Quartz2D简单介绍 在iOS中常用的绘图框架就是Quartz2D,Quartz2D是Core Graphics框架的一部分,我们日常开发使用的所有UIKit组件都是由Core Graphic ...
- tensorflow学习笔记三:实例数据下载与读取
一.mnist数据 深度学习的入门实例,一般就是mnist手写数字分类识别,因此我们应该先下载这个数据集. tensorflow提供一个input_data.py文件,专门用于下载mnist数据,我们 ...
- XML学习笔记7——XSD实例
在前面的XSD笔记中,基本上是以数据类型为主线来写的,而在我的实际开发过程中,是先设计好了XML的结构(元素.属性),并写好了一份示例,然后再反过来写XSD文件(在工具生成的基础上修改),也就是说,是 ...
- python学习笔记-练手实例
1.题目:输出 9*9 乘法口诀表. 程序分析:分行与列考虑,共9行9列,i控制行,j控制列 代码: for i in range(1,10): print ('\r') for j ...
随机推荐
- FluentValidation 验证(二):WebApi 中使用 注入服务
比如你要验证用户的时候判断一下这个用户名称在数据库是否已经存在了,这时候FluentValidation 就需要注入查询数据库 只需要注入一下就可以了 public class Login3Reque ...
- 使用doctest代码测试和Sphinx自动生成文档
python代码测试并自动生成文档 Tips:两大工具:doctest--单元测试.Sphinx--自动生成文档 1.doctest doctest是python自带的一个模块.doctest有两种使 ...
- 「MySQL高级篇」MySQL索引原理,设计原则
大家好,我是melo,一名大二后台练习生,大年初三,我又来充当反内卷第一人了!!! 专栏引言 MySQL,一个熟悉又陌生的名词,早在学习Javaweb的时候,我们就用到了MySQL数据库,在那个阶段, ...
- JS中数值类型的本质
一.JS中的数值类型 众所JS爱好友周知,JS中只有一个总的数值类型--number,它包含了整型.浮点型等数值类型.其中,浮点数的实现思想有点复杂,它把一个数拆成两部分来存储.第一部分是有效位数,也 ...
- photoshop 2021 for mac安装教程,亲测可用!!!
小编分享下photoshop cc 2021 for mac 安装教程,适配M1芯片,让大家完美使用ps2021,畅享所有新功能Adobe Photoshop2021(简称PS) 新版本主要增加了Ne ...
- GlusterFS常用维护操作命令
GlusterFS常用维护操作命令 1.启动/关闭/查看glusterd服务 # /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /e ...
- Selenium4+Python3系列(五) - 多窗口处理之句柄切换
写在前面 感觉到很惭愧呀,因为居然在Selenium+Java系列中没有写过多窗口处理及句柄切换的文章,不过也无妨,不管什么语言,其思路是一样的,下面我们来演示,使用python语言来实现窗口句柄的切 ...
- Spring Cloud 整合 nacos 实现动态配置中心
上一篇文章讲解了Spring Cloud 整合 nacos 实现服务注册与发现,nacos除了有服务注册与发现的功能,还有提供动态配置服务的功能.本文主要讲解Spring Cloud 整合nacos实 ...
- 支持JDK19虚拟线程的web框架,之二:完整开发一个支持虚拟线程的quarkus应用
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<支持JDK19虚拟线程的web ...
- Oracle:ORA-39006、ORA-39213解决办法
执行Oracle数据库导入,遇到报错ORA-39006: internal error.ORA-39213: Metadata processing is not available.这还是第一次遇到 ...