生活随笔
收集整理的這篇文章主要介紹了
数据可视化组队学习:《Task05 - 样式色彩秀芳华》笔记
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄 前言 1 matplotlib全局樣式設(shè)定 1.1 matplotlib自帶的樣式 1.2 打造屬于自己的全局繪圖樣式 1.2.1 使用.mplstyle文件來設(shè)置 1.2.2 設(shè)置rcparams 1.2.3 修改matolotlibrc文件來修改樣式 2 matplotlib的色彩設(shè)置 2.1 設(shè)置顏色的方式 2.1.1 RGB/RGBA 2.1.2 HEX RGB/RGBA 2.1.3 灰度色階 2.1.4 八種基本顏色 2.2 使用colormap設(shè)置一組顏色 作業(yè)
前言
本文為《Task05 - 樣式色彩秀芳華》筆記。
1 matplotlib全局樣式設(shè)定
從頭到尾保持繪圖的樣式。
1.1 matplotlib自帶的樣式
直接調(diào)用即可。
plt
. style
. use
( 'default' )
一共有26中自帶樣式:
print ( plt
. style
. available
)
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
1.2 打造屬于自己的全局繪圖樣式
1.2.1 使用.mplstyle文件來設(shè)置
創(chuàng)建后綴名為.mplstyle的文件,添加如下列所示的內(nèi)容:
axes.titlesize : 24 axes.labelsize : 20 lines.linewidth : 3 lines.markersize : 10 xtick.labelsize : 16 ytick.labelsize : 16
之后調(diào)用如下所示的代碼(路徑自己填自己的):
plt
. style
. use
( 'file/presentation.mplstyle' )
plt
. plot
( [ 1 , 2 , 3 , 4 ] , [ 2 , 3 , 4 , 5 ] )
另外,樣式可以混合使用:
plt
. style
. use
( [ 'dark_background' , 'file/presentation.mplstyle' ] )
plt
. plot
( [ 1 , 2 , 3 , 4 ] , [ 2 , 3 , 4 , 5 ] )
1.2.2 設(shè)置rcparams
import matplotlib
as mpl
import matplotlib
. pyplot
as plt
import numpy
as npmpl
. rcParams
[ 'lines.linewidth' ] = 2
mpl
. rcParams
[ 'lines.linestyle' ] = '--'
plt
. plot
( [ 1 , 2 , 3 , 4 ] , [ 2 , 3 , 4 , 5 ] )
更便捷的方式:
mpl
. rc
( 'lines' , linewidth
= 4 , linestyle
= '-.' )
plt
. plot
( [ 1 , 2 , 3 , 4 ] , [ 2 , 3 , 4 , 5 ] )
1.2.3 修改matolotlibrc文件來修改樣式
1.2.2中的rc其實是matolotlibrc文件。
mpl
. matplotlib_fname
( )
然后根據(jù)輸出的路徑找到matolotlibrc文件直接修改。好家伙!
2 matplotlib的色彩設(shè)置
可視化編碼的角度對顏色進行分析 色相:沒有明顯的順序性、一般不用來表達數(shù)據(jù)量的高低,而是用來表達數(shù)據(jù)列的類別。 亮度和飽和度:在視覺上很容易區(qū)分出優(yōu)先級的高低、被用作表達順序或者表達數(shù)據(jù)量視覺通道。 👉圖表配色指南🔗 👉學(xué)會這6個可視化配色基本技巧,還原數(shù)據(jù)本身的意義🔗
2.1 設(shè)置顏色的方式
2.1.1 RGB/RGBA
RGB (red, green, blue) RGBA (red, green, blue, alpha)
plt
. style
. use
( 'default' )
plt
. plot
( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , color
= ( 0.1 , 0.2 , 0.5 ) )
plt
. plot
( [ 4 , 5 , 6 ] , [ 1 , 2 , 3 ] , color
= ( 0.1 , 0.2 , 0.5 , 0.5 ) )
2.1.2 HEX RGB/RGBA
plt
. plot
( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , color
= '#0f0f0f' )
plt
. plot
( [ 4 , 5 , 6 ] , [ 1 , 2 , 3 ] , color
= '#0f0f0f80' )
2.1.3 灰度色階
當(dāng)只有一個位于[0,1]的值時,表示灰度色階。
plt
. plot
( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , color
= '0.5' )
2.1.4 八種基本顏色
matplotlib有八個基本顏色,可以用單字符串來表示,分別是’:
b’‘g’‘r’‘c’‘m’‘y’‘k’‘w’ blue green red cyan magenta yellow black white
import matplotlib
. pyplot
as plt
import numpy
as npcolors
= [ 'b' , 'g' , 'r' , 'c' , 'm' , 'y' , 'k' , 'w' ] fig
, axs
= plt
. subplots
( 2 , 4 ) for ax
, c
in zip ( axs
. flat
, colors
) : ax
. plot
( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , color
= c
) fig
. tight_layout
( )
在參數(shù)color處可以使用以下顏色:
2.2 使用colormap設(shè)置一組顏色
作業(yè)
查閱matplotlib官網(wǎng),列舉出Sequential,Diverging,Cyclic,Qualitative,Miscellaneous分別有哪些內(nèi)置的colormap,并以代碼繪圖的形式展現(xiàn)出來
import numpy
as np
import matplotlib
. pyplot
as pltcmaps
= [ ( 'Perceptually Uniform Sequential' , [ 'viridis' , 'plasma' , 'inferno' , 'magma' , 'cividis' ] ) , ( 'Sequential' , [ 'Greys' , 'Purples' , 'Blues' , 'Greens' , 'Oranges' , 'Reds' , 'YlOrBr' , 'YlOrRd' , 'OrRd' , 'PuRd' , 'RdPu' , 'BuPu' , 'GnBu' , 'PuBu' , 'YlGnBu' , 'PuBuGn' , 'BuGn' , 'YlGn' ] ) , ( 'Sequential (2)' , [ 'binary' , 'gist_yarg' , 'gist_gray' , 'gray' , 'bone' , 'pink' , 'spring' , 'summer' , 'autumn' , 'winter' , 'cool' , 'Wistia' , 'hot' , 'afmhot' , 'gist_heat' , 'copper' ] ) , ( 'Diverging' , [ 'PiYG' , 'PRGn' , 'BrBG' , 'PuOr' , 'RdGy' , 'RdBu' , 'RdYlBu' , 'RdYlGn' , 'Spectral' , 'coolwarm' , 'bwr' , 'seismic' ] ) , ( 'Cyclic' , [ 'twilight' , 'twilight_shifted' , 'hsv' ] ) , ( 'Qualitative' , [ 'Pastel1' , 'Pastel2' , 'Paired' , 'Accent' , 'Dark2' , 'Set1' , 'Set2' , 'Set3' , 'tab10' , 'tab20' , 'tab20b' , 'tab20c' ] ) , ( 'Miscellaneous' , [ 'flag' , 'prism' , 'ocean' , 'gist_earth' , 'terrain' , 'gist_stern' , 'gnuplot' , 'gnuplot2' , 'CMRmap' , 'cubehelix' , 'brg' , 'gist_rainbow' , 'rainbow' , 'jet' , 'turbo' , 'nipy_spectral' , 'gist_ncar' ] ) ] gradient
= np
. linspace
( 0 , 1 , 256 )
gradient
= np
. vstack
( ( gradient
, gradient
) ) def plot_color_gradients ( cmap_category
, cmap_list
) : nrows
= len ( cmap_list
) figh
= 0.35 + 0.15 + ( nrows
+ ( nrows
- 1 ) * 0.1 ) * 0.22 fig
, axs
= plt
. subplots
( nrows
= nrows
, figsize
= ( 6.4 , figh
) ) fig
. subplots_adjust
( top
= 1 - .35 / figh
, bottom
= .15 / figh
, left
= 0.2 , right
= 0.99 ) axs
[ 0 ] . set_title
( cmap_category
+ ' colormaps' , fontsize
= 14 ) for ax
, name
in zip ( axs
, cmap_list
) : ax
. imshow
( gradient
, aspect
= 'auto' , cmap
= name
) ax
. text
( - .01 , .5 , name
, va
= 'center' , ha
= 'right' , fontsize
= 10 , transform
= ax
. transAxes
) for ax
in axs
: ax
. set_axis_off
( ) for cmap_category
, cmap_list
in cmaps
: plot_color_gradients
( cmap_category
, cmap_list
) fig
. savefig
( 'colrmap.png' ) plt
. show
( )
<img src="https://img-blog.csdnimg.cn/20201224103616817.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzY1NTI4Mg==,size_16,color_FFFFFF,t_70)
學(xué)習(xí)如何自定義colormap,并將其應(yīng)用到任意一個數(shù)據(jù)集中,繪制一幅圖像,注意colormap的類型要和數(shù)據(jù)集的特性相匹配,并做簡單解釋
import matplotlib
. pyplot
as plt
from matplotlib
. colors
import ListedColormap
def plot_examples ( colormaps
) : """Helper function to plot data with associated colormap.""" np
. random
. seed
( 19680801 ) data
= np
. random
. randn
( 30 , 30 ) n
= len ( colormaps
) fig
, axs
= plt
. subplots
( 1 , n
, figsize
= ( n
* 2 + 2 , 3 ) , constrained_layout
= True , squeeze
= False ) for ax
, cmap
in zip ( axs
. flat
, colormaps
) : """創(chuàng)建不規(guī)則的矩形網(wǎng)格圖。vmin,vmax: 顏色條的范圍rasterized: 柵格化(位圖),默認為None.cmap:注意得是ListedColormap類型的""" psm
= ax
. pcolormesh
( data
, cmap
= cmap
, rasterized
= True , vmin
= - 4 , vmax
= 4 ) fig
. colorbar
( psm
, ax
= ax
) plt
. show
( ) """
ListedColormap: matplotlib.colors.ListedColormap(colors, name='from_list', N=None).Colormap object generated from a list of colors.
"""
cmap
= ListedColormap
( [ "darkorange" , "gold" , "lawngreen" , "lightseagreen" ] )
plot_examples
( [ cmap
] )
總結(jié)
以上是生活随笔 為你收集整理的数据可视化组队学习:《Task05 - 样式色彩秀芳华》笔记 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。