圖表可視化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.pswp.cn/news/391573.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391573.shtml
英文地址,請注明出處:http://en.pswp.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(…