7 常用参数调整Adjustment of Common Parameters(代码下载)

主要讲述关于seaborn通用参数设置方法,该章节主要内容有:

  1. 主题设置 themes adjustment
  2. 颜色设置 Manage colors
  3. 轴的管理 Manage axis
  4. 边距调整 Manage margins
  5. 添加注释 Add annotations

1. 主题设置 themes adjustment

  • 灰色网格 darkgrid
  • 白色网格 whitegrid
  • 黑色 dark
  • 白色 white
  • 十字叉 ticks
# Seaborn中有五种可供选择的主题下面通过set_style设置主题
# Proposed themes: darkgrid, whitegrid, dark, white, and ticks
# Data
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
# 灰色网格 darkgrid
sns.set_style("darkgrid")
sns.boxplot(data=data)
plt.title("darkgrid")
Text(0.5, 1.0, 'darkgrid')

# 白色网格 whitegrid
sns.set_style("whitegrid")
sns.boxplot(data=data);
plt.title("whitegrid")
Text(0.5, 1.0, 'whitegrid')

# 黑色 dark
sns.set_style("dark")
sns.boxplot(data=data);
plt.title("dark")
Text(0.5, 1.0, 'dark')

# 白色 white
sns.set_style("white")
sns.boxplot(data=data);
plt.title("white")
Text(0.5, 1.0, 'white')

# 十字叉 ticks
sns.set_style("ticks")
sns.boxplot(data=data);
plt.title("ticks")
Text(0.5, 1.0, 'ticks')

2. 颜色设置 Manage colors

由于Seaborn是在matplotlib之上构建的,因此Matplotlib上的大部分定制工作也适用于seaborn。seaborn中通过palette选择颜色。可用颜色有:



seaborn有三种类型的调色板: Sequential, Diverging and Discrete:





# Sequential颜色调用
# Libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd # create data
x = np.random.rand(80) - 0.5
y = x+np.random.rand(80)
z = x+np.random.rand(80)
df = pd.DataFrame({'x':x, 'y':y, 'z':z}) # Plot with palette
sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="Blues");
# reverse palette
sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="Blues_r");

# Diverging颜色调用
# plot
sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="PuOr");
# reverse palette
sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="PuOr_r");

# Discrete颜色调用
# library & dataset
import seaborn as sns
df = sns.load_dataset('iris') # --- Use the 'palette' argument of seaborn
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, palette="Set1");
plt.legend(loc='lower right') # --- Use a handmade palette 自定义颜色板
flatui = ["#9b59b6", "#3498db", "orange"]
sns.set_palette(flatui)
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False);

3. 轴的管理 Manage axis

  • 标题 Title
  • 标尺 Ticks
  • 标签 labels
  • 坐标轴范围 limit
# 标题 Title
# Basic plot
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns # data
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars)) sns.barplot(y_pos, height, color=(0.2, 0.4, 0.6, 0.6)) # Custom Axis title 需要调用matplotlib设置轴标题
plt.xlabel('title of the xlabel', fontweight='bold', color = 'orange', fontsize='17', horizontalalignment='center');

# 标尺 Ticks
sns.barplot(y_pos, height, color=(0.2, 0.4, 0.6, 0.6))
# Custom ticks 调用matplotlib tick_params控制标尺
plt.tick_params(axis='x', colors='red', direction='out', length=15, width=5)
# You can remove them:
# plt.tick_params(bottom=False)

# 标签 labels
sns.barplot(y_pos, height, color=(0.2, 0.4, 0.6, 0.6))
# use the plt.xticks function to custom labels
plt.xticks(y_pos, bars, color='orange', rotation=45, fontweight='bold', fontsize='17', horizontalalignment='right'); # remove labels
# plt.tick_params(labelbottom='off')

# 坐标轴范围 limit
sns.barplot(y_pos, height, color=(0.2, 0.4, 0.6, 0.6))
# Set the limit
plt.xlim(0,20);
plt.ylim(0,50);

4. 边距调整 Manage margins

y_pos = np.arange(len(bars))
bars = ('A','B','C','D','E')
height = [3, 12, 5, 18, 45]
sns.barplot(y_pos, height); # If we have long labels, we cannot see it properly 当名字太长我们无法阅读
names = ("very long group name 1","very long group name 2","very long group name 3","very long group name 4","very long group name 5")
plt.xticks(y_pos, names, rotation=90);
# It's the same concept if you need more space for your titles
plt.title("This is\na very very\nloooooong\ntitle!");

# 上下边距调整
sns.barplot(y_pos, height); # If we have long labels, we cannot see it properly 当名字太长我们无法阅读
names = ("very long group name 1","very long group name 2","very long group name 3","very long group name 4","very long group name 5")
plt.xticks(y_pos, names, rotation=90);
# It's the same concept if you need more space for your titles
plt.title("This is\na very very\nloooooong\ntitle!") # Thus we have to give more margin 通过subplots_adjust调整上下区域所占范围
plt.subplots_adjust(bottom=0.4)
plt.subplots_adjust(top=0.7)

5. 添加注释 Add annotations

  • 添加文本 add text
  • 添加长方形 add rectangle
  • 添加圆 add circle
  • 添加参考线 add reference line
  • 添加公式 add formula
# 添加文本 add text
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
sns.regplot( data=df, x="x", y="y", marker='o') # Annotate with text + Arrow 添加文本和箭头
plt.annotate(
# Label and coordinate
# xy箭头尖的坐标,xytest文本起始位置
'This point is interesting!', xy=(20, 40), xytext=(0, 80),
# Custom arrow 添加箭头
arrowprops=dict(facecolor='black', shrink=0.05)
);
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

# 添加长方形 add rectangle
# libraries
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd # Data 数据
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) }) # Plot
fig1 = plt.figure()
# 添加子图
ax1 = fig1.add_subplot(111)
sns.regplot( data=df, x="x", y="y", marker='o') # Add rectangle 添加长方形
ax1.add_patch(
patches.Rectangle(
(20, 25), # (x,y) 左上角坐标
50, 50, # width and height 宽高
# You can add rotation as well with 'angle'
alpha=0.3, facecolor="red", edgecolor="black", linewidth=3, linestyle='solid')
);

# 添加圆 add circle
# Data
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) }) # Plot
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
sns.regplot( data=df, x="x", y="y", marker='o') # Annotation
ax1.add_patch(
patches.Circle(
(40, 35), # (x,y) 圆心
30, # radius 半径
alpha=0.3, facecolor="green", edgecolor="black", linewidth=1, linestyle='solid')
);

# 添加参考线 add reference line
# Plot
sns.regplot( data=df, x="x", y="y", marker='o')
# Annotation
# 添加垂直参考线
plt.axvline(40, color='r');
# 添加水平参考系
plt.axhline(50, color='green');

# 添加公式 add formula
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
sns.regplot( data=df, x="x", y="y", marker='o') # Annotation
plt.text(40, 00, r'equation: $\sum_{i=0}^\infty x_i$', fontsize=20);

[seaborn] seaborn学习笔记7-常用参数调整Adjustment of Common Parameters的更多相关文章

  1. Python学习笔记之常用函数及说明

    Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...

  2. golang学习笔记8 beego参数配置 打包linux命令

    golang学习笔记8 beego参数配置 打包linux命令 参数配置 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/docs/mvc/contro ...

  3. python3.4学习笔记(十) 常用操作符,条件分支和循环实例

    python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...

  4. python3.4学习笔记(六) 常用快捷键使用技巧,持续更新

    python3.4学习笔记(六) 常用快捷键使用技巧,持续更新 安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使 ...

  5. git学习笔记:常用命令总结

    本文根据廖雪峰的博客,记录下自己的学习笔记.主要记录常用的命令,包括仓库初始化.添加文件.提交修改.新建分支.内容暂存.分支管理.标签管理等内容. git是分布式版本控制系统. 首先是安装,从官网下载 ...

  6. JVM笔记-GC常用参数设置

    GC常用参数 -Xmn -Xms -Xmx -Xss 年轻代 最小堆 最大堆 栈空间, -Xms -Xmx 一般设置成一样大小, -XX:+UseTLAB 使用TLAB,默认打开 -XX:+Print ...

  7. java web jsp学习笔记--概述-常用语法,指令,动作元素,隐式对象,域对象

     JSP学习笔记 1.什么是jsp JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.JSP/Servlet规范.JS ...

  8. CSAPP阅读笔记-gcc常用参数初探-来自第三章3.2的笔记-P113

    gcc是一种C编译器,这次我们根据书上的代码尝试着使用它. 使用之前,先补充前置知识.编译器将源代码转换为可执行代码的流程:首先,预处理器对源代码进行处理,将#define指定的宏进行替换,将#inc ...

  9. python自动化测试学习笔记-5常用模块

    上一次学习了os模块,sys模块,json模块,random模块,string模块,time模块,hashlib模块,今天继续学习以下的常用模块: 1.datetime模块 2.pymysql模块(3 ...

随机推荐

  1. .NET平台下一个你不知道的框架,我只想说两个字:“牛逼”

    框架内容 零度框架是一套基于微服务和领域模型驱动设计的企业级快速开发框架,基于微软 .NET 6 + React 最新技术栈构建,容器化微服务最佳实践,零度框架的搭建以开发简单,多屏体验,前后端分离, ...

  2. KTV和泛型(2)

    很多使用泛型的小伙伴,都会有一个疑惑:为什么有的方法返回值前带<T>.<K, V>之类的标记,而有的方法返回值前又什么都不带呢?就像这样: // 实体基类 class Enti ...

  3. 线性表的基本操作(C语言实现)

    文章目录 这里使用的工具是DEV C++ 可以借鉴一下 实现效果 顺序存储代码实现 链式存储存储实现 这里使用的工具是DEV C++ 可以借鉴一下 一.实训名称 线性表的基本操作 二.实训目的 1.掌 ...

  4. elementUi使用dialog的进行信息的添加、删除表格数据时进行信息提示。删除或者添加成功的信息提示(SpringBoot+Vue+MybatisPlus)

    文章目录 1.添加新用户,通过dialog的弹窗形式 1.1 添加的按钮 1.2 调用方法设置窗口可见 1.3 窗口代码 1.4 提交注册信息方法 1.5 使用mybatisPlus方法进行添加信息到 ...

  5. 齐博x1商业模块仅限一个国际域名使用

    应用市场的所有商业模块 仅授权一个国际域名,大家不要试图复制到其它国际域名下使用. 仅支持一个国际域名使用,二级域名不限,但前提需要先用 www.开头的国际域名先安装,然后再到二级域名安装,并且二级域 ...

  6. Redis 01: 非关系型数据库 + 配置Redis

    数据库应用的发展历程 单机数据库时代:一个应用,一个数据库实例 缓存时代:对某些表中的数据访问频繁,则对这些数据设置缓存(此时数据库中总的数据量不是很大) 水平切分时代:将数据库中的表存放到不同数据库 ...

  7. python django超链接

    之前用django框架打了一个简易的博客网站,现在说说怎么用django做超链接. 本文基于之前讲解的博客应用,如果只想学超链接请自行删减代码或评论提问. 首先,在templates文件夹下添加det ...

  8. 部署redis-cluster

    1.环境准备 ☆ 每个Redis 节点采用相同的相同的Redis版本.相同的密码.硬件配置 ☆ 所有Redis服务器必须没有任何数据 #所有主从节点执行: [root@ubuntu2004 ~]#ba ...

  9. ML-朴素贝叶斯算法

    贝叶斯定理 w是由待测数据的所有属性组成的向量.p(c|x)表示,在数据为x时,属于c类的概率. \[p(c|w)=\frac{p(w|c)p(c)}{p(w)} \] 如果数据的目标变量最后有两个结 ...

  10. beego学习———安装bee

    Bee安装 有各种坑,一会儿GOPATH的问题,一会儿局部的问题了 唉,搞了一个小时 很重要的问题!!!!!!!!!!!! beego的bee工具只能强制新建项目在GOPATH/src目录下 虽然在别 ...