2 B: [5 ^9 g* v n一行Python代码有多强,可让图形秒变「手绘风」* C( e( k z, O$ b p5 G1 W% F
之前介绍过一个绘制手绘风格图形的工具cutecharts:一款蠢萌蠢萌的可视化工具7 Q, Y2 J( C `
9 [! ?( P3 A/ b; ~2 N; C5 ]
) z8 n+ Y3 C( w: t2 Y; x
但是,其功能有限,今天再介绍一个手绘工具(matplotlib.pyplot.xkcd()),一行代码可将所有Matplotlib和Seaborn绘制的图形变为手绘风格。4 L+ L8 ]! R, o" Y* z( e; T7 \
% X1 d7 {8 K4 j6 g& [2 D# y2 V9 u1 L. }6 B) s
matplotlib.pyplot.xkcd()简介 _* T! ?, x$ q q, H
这个Matplotlib子函数特别简单,只有三个参数,别看参数少,但功能可不小* t$ d% z' X3 }' b" n" b' l
# g K1 `4 C9 T9 C7 t1 i
8 Y. l* ~; e, xmatplotlib.pyplot.xkcd(scale=1, #相对于不使用xkcd的风格图,褶皱的幅度7 { J) `3 U, O1 M. j. u0 a
length=100, #褶皱长度 7 Y9 Y& Q% a" w' F randomness=2#褶皱的随机性 2 c' e4 K6 b+ {/ K7 ]* D5 \ )4 l. `' f9 B# U4 x% _+ I: o, o
matplotlib.pyplot.xkcd()使用 7 Y0 L: T. G+ I% A如下,加with行代码即可,括号中参数按个人喜好决定是否设置~ 0 x# i5 b/ Z+ _' C4 | / {# R# {) |0 p: _: g9 _* k- ^. Z2 g9 D5 U9 H/ }* D+ T* t
with plt.xkcd(scale=1, length=100, randomness=2):& n$ {! z5 m0 Z; v% e* k
#with是临时使用一下,不影响其它图使用正常样式3 b) ^5 R: B: A
绘图代码0 d0 _1 b0 ~6 E1 i& r- _
。。。。。。 ?, s4 L) s, \. M# a
plt.show()4 ^) j7 _4 |& L2 \# A! i3 q
matplotlib.pyplot.xkcd()使用实例 1 }( F0 N5 K4 k/ D @下面代码为pythonic生物人公众号之前的文章代码 5 N: X! L5 l: t) e, Y 9 P4 G8 W) ~, p : T/ n) G$ s. B c. }1 {以下参考:Python可视化25|seaborn绘制矩阵图 ! v$ [' |8 e: q! B4 U1 q! Q p3 \* Y/ F
) o7 @9 g# F1 R. W# q! E# |#支持seaborn- d, y0 F7 q) T1 V! t# N
import seaborn as sns . q# ~. ?8 {3 d# l1 Giris_sns = sns.load_dataset("iris")1 c+ H# V3 f- s+ m V/ _& W- z
with plt.xkcd(): / r- p7 U: L# P/ h) Y6 K g = sns.pairplot(9 w X& e" R4 }% U& r2 M
iris_sns, 1 A* g0 n6 z$ b hue='species', #按照三种花分类 6 Q0 H- d& C& ?9 x |' U palette=['#dc2624', '#2b4750', '#45a0a2']) ' r$ _) J0 ]; G' s+ }& m$ d9 J sns.set(style='whitegrid') ! r, e+ E$ I+ K g.fig.set_size_inches(12, 12) # G# h, @, S3 K) ?/ n sns.set(style='whitegrid', font_scale=1.5) 8 S3 G. P& o) g9 v" p- c ) s/ p! }- F; u! ?1 K
& s" @+ [1 s- |* K, p8 W8 Y
_" M7 W, v9 Y( X5 t, |6 w: t# `: c
以下参考:Python可视化29|matplotlib-饼图(pie) ' F H V+ K; _( k; g/ P. A: q) \8 C3 w
) @5 q7 f* I4 K, [; ~4 [6 D: P, limport matplotlib.pyplot as plt ' W! }, H% ]) z7 o( I! G2 bwith plt.xkcd(2 ?: x2 K, I6 t& Z
scale=4, #相对于不使用xkcd的风格图,褶皱的幅度 ) z# |# v) P3 z. a length=120, #褶皱长度 2 {9 I* E4 U2 Q$ C! K+ B8 X randomness=2): #褶皱的随机性 4 P3 w: f4 {1 D9 b( \ plt.figure(dpi=150) 4 N1 L! ^% P7 G; Q) M/ a patches, texts, autotexts = plt.pie(& ?* A' Q6 ^, U' E6 ]/ j% f; S" b
x=[1, 2, 3], #返回三个对象" Y/ U4 l! O: z
labels=['A', 'B', 'C'], 0 x; f( X, M d. ~ l$ b5 K colors=['#dc2624', '#2b4750', '#45a0a2'], 2 f+ j. i) P: {) J) G( L autopct='%.2f%%', 8 P4 ?0 }! o7 A1 @ explode=(0.1, 0, 0))2 Y, K" o. {0 I8 ?9 j
texts[1].set_size('20') #修改B的大小 W/ D" g' w7 [7 j7 j1 o 7 _, b. f+ A# u) _: C& D! q #matplotlib.patches.Wedge; ?5 U1 x3 a' l; t- Z5 O/ G# I. L% O/ t
patches[0].set_alpha(0.3) #A组分设置透明度6 A* v D$ t" y1 Q1 W
patches[2].set_hatch('|') #C组分添加网格线 % m: m# S/ W$ l' j$ u5 Z( \$ Z patches[1].set_hatch('x') 8 o; B+ O5 c! L3 f5 O - I7 F1 \( t& A plt.legend( s' z* Z' a+ E, U0 E patches, $ Q% j0 t7 D! m7 J ['A', 'B', 'C'], #添加图例7 E- s( `5 x6 _( m
title="Pie Learning", ' {2 F- U: u& a8 O \4 z loc="center left",0 o5 h. B5 u `3 y9 l
fontsize=15,* `2 U1 u } E$ _6 k M9 o9 Y/ i
bbox_to_anchor=(1, 0, 0.5, 1))4 O V5 O8 L' n( B! r+ J
8 T) ~! E! w. t3 a plt.title('Lovely pie', size=20)9 Q' m5 ?% c n- v5 U
plt.show(). i/ @9 m2 ]3 M- A- O
: s! i& t+ Y1 q0 A) f/ _ " L1 t9 R- `6 @1 D, n 0 A {; N0 V; Z7 P& P. Y) S- Y U* K4 E) q& P4 N
with plt.xkcd():" O& _( z) c# ~* k) V
from string import ascii_letters . v) e1 n% ^" L1 H0 b$ k plt.figure(dpi=150)6 @+ N: W; {9 K$ s) A$ p
patches, texts, autotexts = plt.pie(& c$ z& G) C- p/ d
x=range(1, 12), $ o t( s' B* ? labels=list(ascii_letters[26:])[0:11], - t5 P* Y3 r8 C- N0 I* M colors=[, R, b% j5 W0 L; Z/ d7 ~
'#dc2624', '#2b4750', '#45a0a2', '#e87a59', '#7dcaa9', '#649E7D'," U# C7 q- K; u) p* x* r5 z
'#dc8018', '#C89F91', '#6c6d6c', '#4f6268', '#c7cccf' ! r: k$ n1 I; v: e ],/ s' q7 a) k1 ^: q( g
autopct='%.2f%%',2 o$ d# L" p% ]) H4 f! x+ }: h
)9 v8 d- l2 k0 o! L1 F1 k
plt.legend( 9 \" t+ d# Y6 y, R; [ patches, " b( S; B6 u% Y3 }5 A list(ascii_letters[26:])[0:11], #添加图例 5 l w, H- N' q title="Pie Learning", , U' `1 ~$ W8 G9 E: V8 _ loc="center left", 7 Y: m5 \: _9 C, Y& R) E% C bbox_to_anchor=(1, 0, 0.5, 1),$ d+ V! b# T% R B8 ^" T# V8 o
ncol=2, #控制图例中按照两列显示,默认为一列显示, f7 t% `" w" | B
) " K& v4 k; ~% {! A( Y* p1 U. @2 q& V. p' O1 u) f
" b. ^" `* n/ u; _) Z. }) w4 R7 _" ]5 h& q* h, q
6 Y/ q7 B, K6 dimport matplotlib.pyplot as plt: f' e) Z, }( a% J$ f, a7 I
import numpy as np- L- V9 {2 |! K: L9 v7 l6 Q
with plt.xkcd():! Y, t% R$ R. e) s% @& m* w
plt.figure(dpi=150) , ]" Y) K5 a7 w$ o* N8 B% \5 ` labels = ['Jack', 'Rose', 'Jimmy']% J* U1 E" A" m( p s2 b& _4 z
year_2019 = np.arange(1, 4) / C8 J [( E0 e! n6 K& a0 ^! R1 r/ A8 S year_2020 = np.arange(1, 4) + 1 9 y1 k* J F& n$ Z6 T* x bar_width = 0.4 - w$ }+ ^; Z- @( t # e0 v1 G) r3 t1 M; j# p9 L# ~8 T plt.bar( 3 H/ p9 }: U( Z4 X, K5 Y np.arange(len(labels)) - bar_width / 2, #为了两个柱子一样宽 2 b. s# ?7 O8 g) I( q year_2019,5 w" ^2 O# T' C$ E/ k
color='#dc2624', % M, _! S6 i M; M- t& G9 g. f width=bar_width,0 H4 ` F* a* n4 [2 A8 l; }6 X
label='year_2019' #图例 u3 `, O& U0 p: ` ) , h' S M: @0 ~$ B! g# }/ ? plt.bar(0 D% B5 f1 c& R- }1 y" d
np.arange(len(labels)) + bar_width / 2, ( }. d/ C: h! C% j) v year_2020,, `- I# e% P( z5 p0 a' B
color='#45a0a2', ; X1 j+ D. I" Y- W% E width=bar_width, * J4 ~7 D3 }; [9 ?( ?" N label='year_2020' #图例' x; C) X9 R: K4 M3 M3 C
) " I8 @# p- W( t G6 l3 q# W plt.xticks(np.arange(0, 3, step=1), labels, rotation=45) #定义柱子名称* ?7 [4 B5 O7 v* F* ^
plt.legend(loc=2) #图例在左边) D6 h4 l" \" @
以下参考:Python可视化|matplotlib12-垂直|水平|堆积条形图详解 $ h+ h- q1 k3 x# }/ s
0 [( h, g @! I& G! s & g; s' F( w- B8 u# g 6 r- L5 v" o/ d R7 s6 _0 ~3 p- \4 k3 g* D/ b3 I
. R2 |* B3 c2 {: D " B, c* E+ T# H3 s1 s# U' t以下参考: Python可视化|matplotlib10-绘制散点图scatter7 k/ x' d" e' Z* s. }% e2 \- }
5 {: n9 x' D0 O6 Y+ Q" ` / L8 o4 f! ?& X: d o4 uimport matplotlib.pyplot as plt ' T- u3 l' O- r6 M f1 eimport numpy as np ) b4 [% {, ]& Zimport pandas as pd ! i' `' \$ }6 s% N5 E, N& @- Dfrom pandas import Series, DataFrame2 `8 E( x+ k/ P3 M, i' M# p
#数据准备 9 r9 ]1 e* y: v: o% b/ sfrom sklearn import datasets ' Q5 Y4 T' y0 a2 b 0 [* E* z) e. q& _* ]/ f& ]) diris = datasets.load_iris() 0 R/ T7 M% }* K i' V; W! k/ nx, y = iris.data, iris.target5 L8 {% L: ?; t. A. R/ W$ t
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),) L6 Y' ?. b" W1 o4 R% m& X
columns=[- C! ^. O @ g& h( Z
'sepal length(cm)', 'sepal width(cm)',7 [: D* I7 P8 }# L% C* @2 ]6 I& P
'petal length(cm)', 'petal width(cm)', 'class' 4 [- L( {2 I; O' g# r% F- }1 k ]) 9 W( u7 R0 g' p, `& H9 Xwith plt.xkcd(): ) M% K6 c: E8 m& L9 I- g) o 8 d' p9 Q, M- S( M) @# _; o
plt.figure(dpi=150) #设置图的分辨率* b/ V( e% L4 ~/ k- m2 o+ m' l# ~
#plt.style.use('Solarize_Light2') #使用Solarize_Light2风格绘图# }% l# d$ X# f# B+ V; h$ D) L
iris_type = pd_iris['class'].unique() #根据class列将点分为三类 ) h: U4 \/ X5 T9 u" e iris_name = iris.target_names #获取每一类的名称 ! q6 w0 P! F' ]6 {8 {( |. U0 t( _ colors = ['#dc2624', '#2b4750', '#45a0a2'] #三种不同颜色 E+ e8 [ v$ x; w P
markers = ['$\clubsuit$', '.', '+'] #三种不同图形 ! @* `7 r) x# I 9 F W% {* F! n. `
for i in range(len(iris_type)):5 R9 g* r& o; F; v
plt.scatter(+ i+ @6 _/ i/ Q
pd_iris.loc[pd_iris['class'] == iris_type,) u2 U* A' q3 p6 H0 i
'sepal length(cm)'], #传入数据x 6 X. T# g; f' s0 t: z pd_iris.loc[pd_iris['class'] == iris_type,/ _/ A3 ?3 N9 B4 x7 l( h
'sepal width(cm)'], #传入数据y. Z3 O& `9 P( i
s=50, #散点图形(marker)的大小 / @: a; p! U9 ]: F- f! c c=colors, #marker颜色 0 B* v* D1 }+ }$ B$ Z8 f marker=markers, #marker形状* r0 S7 p" y- n& B. C
#marker=matplotlib.markers.MarkerStyle(marker = markers,fillstyle='full'),#设置marker的填充" w; ?7 @; s2 l) W* D; P+ F
alpha=0.8, #marker透明度,范围为0-1 3 w9 I0 ] H0 _/ \ ^ facecolors='r', #marker的填充颜色,当上面c参数设置了颜色,优先c2 n# J1 c6 {, D
edgecolors='none', #marker的边缘线色 4 d6 b. L% J( @: s) U linewidths=1, #marker边缘线宽度,edgecolors不设置时,该参数不起作用 4 |0 `; C) d; z8 z3 n label=iris_name) #后面图例的名称取自label* Q+ S; E) j6 H+ ~
9 B. d' L. h7 d- l! Q
plt.legend(loc='upper right') 1 Q" E2 I1 M+ ^$ O: f) j9 |) }" Q* j
& D4 f$ N; ?7 S, F' m————————————————7 U+ @1 \/ v/ F
版权声明:本文为CSDN博主「pythonic生物人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。. F1 F# q# J# {5 [* O3 _8 ]
原文链接:https://blog.csdn.net/qq_21478261/article/details/1189637539 M! r/ w6 l5 _8 L; r W$ L
2 S8 X) P/ ?- ^2 m