图表可视化seaborn风格和调色盘

seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,包括一些高级图表可视化等工具。

使用seaborn需要先安装改模块pip3 install seaborn 。

 

一、风格style

包括set() / set_style() / axes_style() / despine() / set_context()

创建正弦函数并显示图表

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
def sinplot(flip = 1):x = np.linspace(0,14,100)for i in range(1,7):plt.plot(x,np.sin(x+i)*i)      # 6个正弦函数
sinplot()

1.set(),设置整体为默认风格

sns.set()  #默认风格为darkgrid
sinplot()

2.set_style(),自定义整体风格

参数为"white"、"dark"、 "whitegrid"、 "darkgrid"、 "ticks"或者None,默认为darkgrid

fig = plt.figure(figsize=(15,6))ax1 = fig.add_subplot(121)                
sns.set_style('whitegrid')
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)
plt.title('style - whitegrid')# 仍然可以使用matplotlib的参数

ax2 = fig.add_subplot(122)    
# sns.set_style("dark")
sinplot()

3.axes_style(),设置子图风格

可与with搭配使用,设置with代码块内的图表风格,不影响整体图表风格。

fig = plt.figure(figsize=(15,6))
with sns.axes_style("dark"): #只对with代码块内的图表风格生效,即只对第一个子图生效plt.subplot(121)       sinplot()sns.set_style("white")       #整体风格为white
plt.subplot(122)
sinplot()

 

4.despine()移除轴线

despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)

top、right、left、bottom:上、右、左、下方轴线,默认移除上方和右侧轴线

offset:xy轴和y轴的起点相对原始位置的偏移量

trim:默认坐标轴长度没有限制,会延伸到图表内容结束,True表示将坐标轴的显示的长度在最小值和最大值之间

fig = plt.figure(figsize=(20,6))
ax1 = fig.add_subplot(131)  
sinplot()
sns.despine()# 默认删除上、右坐标轴

ax2 = fig.add_subplot(132)
sns.violinplot(data=data) #小提琴图
# sns.despine(offset=1, trim=True) 

ax3 = fig.add_subplot(133)
sns.boxplot(data=data, palette="deep")
sns.despine(left=True, right = False) #最终是该despine设置生效

5.set_context()显示比例

可选参数为'paper'、 'notebook'、'talk'、'poster',默认为notebook,设置标签、线等的大小。

sns.set_context("notebook")
sinplot()

下面分别为设置为notebook、paper、talk和poster的显示结果。

 

二、 调色盘

1.color_palette()

默认取当前调色盘的颜色,返回结果是一个seaborn.palette的类,形式类似一个列表,列表中每一个元素为元组,元组用3个数值表示rgb颜色。

current_palette = sns.color_palette()  # 读取当前调色盘颜色,可添加参数n表示取几个色块
print(current_pallette,type(current_palette))
sns.palplot(current_palette)
#<class 'seaborn.palettes._ColorPalette'> [(0.9913725490196079, 0.7913725490196079, 0.7082352941176471)...]

seaborn可用调色盘有6种,deep、 muted、bright、 pastel、dark、colorblind,默认显示bright。

 

其他调色盘

sns.color_palette('Reds', 10),第一个参数表示色系,第二个参数表示取几个色块。

颜色默认是由浅到深,带r表示反转即颜色由深到浅,不是所有颜色都可以翻转哦。

#其他可用调色盘
#Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, 
#Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, 
#Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r,RdBu, RdBu_r, 
#RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, spectral
#Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r
#binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, 
#cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, 
#gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, 
#gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, 
#nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, 
#seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, 
#terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r

sns.palplot(sns.color_palette('Reds', 10))
sns.palplot(sns.color_palette('Greens_r', 7))  #

 

# 分组颜色,同一个颜色成对出现
sns.palplot(sns.color_palette('Paired',7))  #参数可以为奇数
sns.palplot(sns.color_palette('Paired', 18))

 

2.设置调色盘

set_palette(palette, n_colors=None, desat=None, color_codes=False),使用Seaborn调色盘设置Matplotlib颜色循环

palette参数可设置为seaborn color paltte | matplotlib colormap | hls | husl

sns.set_palette('Greens')
sinplot()

3.亮度和饱和度

sns.hls_palette(n_colors=6, h=.01, l=.6, s=.65)

sns.husl_palette(n_colors=6, h=.01, s=.9, l=.65),两者表示亮度和饱和度的参数位置相反。

参数n_colors表示取几个色块,h表示第一个色块的颜色,l表示亮度,s表示饱和度,h、l、s取值[0,1]。

sns.palplot(sns.hls_palette(8,0.01,0.5,0.5))
sns.palplot(sns.husl_palette(8,0.03,0.8,0.8))

 

4.按线性增长设置颜色

cubehelix_palette(n_colors=6, start=0, rot=.4, gamma=1.0, hue=0.8,light=.85, dark=.15, reverse=False, as_cmap=False)

c_colors:色块个数

start:色块的起点颜色,[0,3]之间

rot:颜色的旋转角度

gamma:颜色的伽马值,值越大颜色越深

hue:饱和度,[0,1]之间

light和dark:亮度和深度,[0,1]之间

reverse:默认为False颜色由浅到深,True表示由深到浅

as_cmp:If True, return a matplotlib colormap instead of a list of colors

sns.palplot(sns.cubehelix_palette(8, gamma=1.5)) 
sns.palplot(sns.cubehelix_palette(8, start=1, rot=-0.75)) 
sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0.5,light=0.9, reverse=True))

5.按颜色深浅设置颜色

light_palette(color, n_colors=6, reverse=False, as_cmap=False,input="rgb")和dark_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb")

color_palette()中的颜色参数为调色盘,而light_palette()和dark_palette()中的color颜色参数就是单纯的颜色,例如对于蓝色,color_palette()需设置Blues,后两者参数为blue。

sns.palplot(sns.light_palette("red")) 
sns.palplot(sns.dark_palette("red")) 
sns.palplot(sns.light_palette("blue")) 
sns.palplot(sns.dark_palette("blue", reverse=True)) 

6.设置分散颜色

diverging_palette(h_neg, h_pos, s=75, l=50, sep=10, n=6, center="light", as_cmap=False)

h_neg, h_pos:起始和终止颜色,[0,359]之间

s、l:饱和度和亮度,[0,100]之间

n:色块个数

center: 最中间颜色为浅色或者深色,{'light','dark'},默认为浅色

sns.palplot(sns.diverging_palette(0,150, s=60, l=20, n=8))
sns.palplot(sns.diverging_palette(300, 150, s=30, l=50, n=8,center='dark')) 

 

sns.set_style("white")# 设置风格
fig = plt.figure(figsize=(18,5))with sns.color_palette("Greens"): #设置局部调色盘plt.subplot(131)sinplot()sns.set_palette("husl")   #对于多系列的图表,用不同颜色区分系列
plt.subplot(132)
sinplot()x = np.arange(25).reshape(5, 5) 
cmap = sns.diverging_palette(200, 20, sep=20, as_cmap=True) 
plt.subplot(133)
sns.heatmap(x, cmap=cmap)  #显示热力图效果

 

转载于:https://www.cnblogs.com/Forever77/p/11396588.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391573.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面向Tableau开发人员的Python简要介绍(第3部分)

用PYTHON探索数据 (EXPLORING DATA WITH PYTHON) One of Tableau’s biggest advantages is how it lets you swim around in your data. You don’t always need a fine-tuned dashboard to find meaningful insights, so even someone with quite a basic understanding of T…

leetcode 191. 位1的个数(位运算)

编写一个函数&#xff0c;输入是一个无符号整数&#xff08;以二进制串的形式&#xff09;&#xff0c;返回其二进制表达式中数字位数为 ‘1’ 的个数&#xff08;也被称为汉明重量&#xff09;。 提示&#xff1a; 请注意&#xff0c;在某些语言&#xff08;如 Java&#xf…

7、芯片发展

第一台继电器式计算机由康德拉.楚泽制造&#xff08;1910-1995&#xff09;&#xff0c;这台机器使用了二进制数&#xff0c;但早期版本中使用的是机械存储器而非继电器&#xff0c;使用老式35毫米电影胶片进行穿孔编程。 同一时期&#xff0c;哈佛大学研究生霍华德.艾肯 要寻找…

seaborn分布数据可视化:直方图|密度图|散点图

系统自带的数据表格&#xff08;存放在github上https://github.com/mwaskom/seaborn-data&#xff09;&#xff0c;使用时通过sns.load_dataset(表名称)即可&#xff0c;结果为一个DataFrame。 print(sns.get_dataset_names()) #获取所有数据表名称 # [anscombe, attention, …

如何成为一个优秀的程序员_如何成为一名优秀的程序员

如何成为一个优秀的程序员by Amy M Haddad通过艾米M哈达德(Amy M Haddad) 如何成为一名优秀的程序员 (How to be a great programmer) What sets apart the really great programmers?是什么使真正出色的程序员与众不同&#xff1f; As we all know, great programmers buil…

pymc3使用_使用PyMC3了解飞机事故趋势

pymc3使用Visually exploring historic airline accidents, applying frequentist interpretations and validating changing trends with PyMC3.使用PyMC3直观地浏览历史性航空事故&#xff0c;应用常识性解释并验证变化趋势。 前言 (Preface) On the 7th of August this yea…

视频监控业务上云方案解析

摘要&#xff1a;阿里云针对安防监控服务在传统IT架构下面临的上述问题&#xff0c;基于阿里云存储服务&#xff0c;提供视频监控解决方案。从2015年推出视频监控存储与播放解决方案以来&#xff0c;帮助大量的视频监控企业解决了上云的过程中遇到的问题&#xff0c;针对不同的…

leetcode 341. 扁平化嵌套列表迭代器(dfs)

给你一个嵌套的整型列表。请你设计一个迭代器&#xff0c;使其能够遍历这个整型列表中的所有整数。 列表中的每一项或者为一个整数&#xff0c;或者是另一个列表。其中列表的元素也可能是整数或是其他列表。 示例 1: 输入: [[1,1],2,[1,1]] 输出: [1,1,2,1,1] 解释: 通过重复…

爬虫结果数据完整性校验

数据完整性分为三个方面&#xff1a; 1、域完整性&#xff08;列&#xff09; 限制输入数据的类型&#xff0c;及范围&#xff0c;或者格式&#xff0c;如性别字段必须是“男”或者“女”&#xff0c;不允许其他数据插入&#xff0c;成绩字段只能是0-100的整型数据&#xff0c;…

go map数据结构

map数据结构 key-value的数据结构&#xff0c;又叫字典或关联数组 声明&#xff1a;var map1 map[keytype]valuetype var a map[string]string var a map[string]int var a map[int]string var a map[string]map[string]string备注&#xff1a;声明是不会分配内存的&#xff0c…

吴恩达神经网络1-2-2_图神经网络进行药物发现-第2部分

吴恩达神经网络1-2-2预测毒性 (Predicting Toxicity) 相关资料 (Related Material) Jupyter Notebook for the article Jupyter Notebook的文章 Drug Discovery with Graph Neural Networks — part 1 图神经网络进行药物发现-第1部分 Introduction to Cheminformatics 化学信息…

android初学者_适用于初学者的Android广播接收器

android初学者Let’s say you have an application that depends on a steady internet connection. You want your application to get notified when the internet connection changes. How do you do that?假设您有一个依赖稳定互联网连接的应用程序。 您希望您的应用程序在…

Android热修复之 - 阿里开源的热补丁

1.1 基本介绍     我们先去github上面了解它https://github.com/alibaba/AndFix 这里就有一个概念那就AndFix.apatch补丁用来修复方法&#xff0c;接下来我们看看到底是怎么实现的。1.2 生成apatch包      假如我们收到了用户上传的崩溃信息&#xff0c;我们改完需要修复…

leetcode 456. 132 模式(单调栈)

给你一个整数数组 nums &#xff0c;数组中共有 n 个整数。132 模式的子序列 由三个整数 nums[i]、nums[j] 和 nums[k] 组成&#xff0c;并同时满足&#xff1a;i < j < k 和 nums[i] < nums[k] < nums[j] 。 如果 nums 中存在 132 模式的子序列 &#xff0c;返回…

seaborn分类数据可视:散点图|箱型图|小提琴图|lv图|柱状图|折线图

一、散点图stripplot( ) 与swarmplot&#xff08;&#xff09; 1.分类散点图stripplot( ) 用法stripplot(xNone, yNone, hueNone, dataNone, orderNone, hue_orderNone,jitterTrue, dodgeFalse, orientNone, colorNone, paletteNone,size5, edgecolor"gray", linewi…

数据图表可视化_数据可视化十大最有用的图表

数据图表可视化分析师每天使用的最佳数据可视化图表列表。 (List of best data visualization charts that Analysts use on a daily basis.) Presenting information or data in a visual format is one of the most effective ways. Researchers have proved that the human …

javascript实现自动添加文本框功能

转自&#xff1a;http://www.cnblogs.com/damonlan/archive/2011/08/03/2126046.html 昨天&#xff0c;我们公司的网络小组决定为公司做一个内部的网站&#xff0c;主要是为员工比如发布公告啊、填写相应信息、投诉、问题等等需求。我那同事给了我以下需求&#xff1a; 1.点击一…

从Mysql slave system lock延迟说开去

本文主要分析 sql thread中system lock出现的原因&#xff0c;但是笔者并明没有系统的学习过master-slave的代码&#xff0c;这也是2018年的一个目标&#xff0c;2018年我都排满了&#xff0c;悲剧。所以如果有错误请指出&#xff0c;也作为一个笔记用于后期学习。同时也给出笔…

传智播客全栈_播客:从家庭学生到自学成才的全栈开发人员

传智播客全栈In this weeks episode of the freeCodeCamp podcast, Abbey chats with Madison Kanna, a full-stack developer who works remotely for Mediavine. Madison describes how homeschooling affected her future learning style, how she tackles imposter syndrom…

leetcode 82. 删除排序链表中的重复元素 II(map)

解题思路 map记录数字出现的次数&#xff0c;出现次数大于1的数字从链表中移除 代码 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(…