|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
& a! R" Y ~4 l% W, q$ P' l# v! w# l1 L( L* C
9 f; k `) @% _) m
第一步:使用anaconda安装Matplotlib库:
/ p2 D, _! m/ X+ z E- G& e. g5 J% w, A; O
conda install Matplotlib9 W, W7 F: a5 ]9 n {
, R6 i M* j2 b5 p
( C9 h {& x) E7 ^ S% z# U0 F2 ^6 h
6 A- r0 I* v) R$ I第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
/ }" S- h Y1 i$ j+ B
1 }) D3 I' f0 p6 N' R- ) t" w% y% Q* Z% b; ]
- " b) N- q$ M( q; J
- , i0 h6 a4 L/ r6 ?! k7 ?
) X+ w/ W0 m6 [6 P. ~9 n# @& P w
9 ]6 H. b. i( W/ }5 P) D
) x4 v/ _0 X6 T! R+ j
) N: Y. c- {: A7 J& j" n8 I; L" a
% e/ }* g/ K$ E- h
* t& u4 `3 V2 w
import matplotlib.pyplot as pltx_values = [1981, 1982, 1983, 1984, 1985]y_values = [22.8, 22.3, 22.9, 21.8, 22.2]fig, ax = plt.subplots() #绘制画布,地图ax.plot(x_values, y_values, linewidth=3)ax.set_title("1981-1985 temperature", fontsize=24) #标题ax.set_xlabel("time(year)", fontsize=14) #坐标轴标签ax.set_ylabel("temperature(℃)", fontsize=14)ax.tick_params(axis='both', labelsize=14) #刻度标记plt.show()
8 r$ ^' [, A0 I5 K 代码读取后显示:
! b% D) Z6 J( m; f& Z
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - 5 \( ~4 L# U" ?; l; Z5 L/ M
- , ~% R6 b7 ^. K- |, t, ]& \
- & P* P$ v2 K: e& M
- 9 w8 p) n# w1 p* S3 X
- N) ]* l+ A- c& m/ ^, w- ?+ m. ]
( W- b1 {* N) Z9 L' O2 c. b7 M3 a
6 B. {! m- f% h
$ L$ A: W7 a! R. p
: n9 b) W4 X1 R2 T- 8 ~+ f* }. C/ p; s7 i
% }6 u& F. w9 D! q" i7 b- 7 H2 C( d0 p7 m4 [( L' C0 \1 K
X- C- X8 q3 p4 w$ y4 |- , t* D2 v: [1 [7 V: ^: z1 A- x* e
- 2 `- D: s$ `5 i' Q! _2 o' d
- + @) H( c1 }9 o$ R- l- r( k
- 2 l" Z8 N+ G& V9 [3 H$ F
- & X' j2 `1 r) \+ R
- 6 z Z! R9 w8 ^& C" ~5 d0 B
. O L. j0 \6 U3 l3 {% t K% N1 L- 9 `7 p. b% O5 Q: s, {& g q3 @
- $ ]: F3 U" t+ Z2 H
( M K# ^ C: A& v" \- k6 k- + c. b! ]' v5 V" |" W/ N
& Q/ \4 Z* c$ D! x$ n7 _- g
import matplotlib.pyplot as pltx_values = [1981, 1982, 1983, 1984, 1985]y_values = [22.8, 22.3, 22.9, 21.8, 22.2]fig, ax = plt.subplots()ax.plot(x_values, y_values, linewidth=3)# 使用内置样式画图print(plt.style.available) #显示有哪些可用的内置样式mystyles = plt.style.availablefor mystyle in mystyles:plt.style.use(mystyle) #使用内置样式fig, ax = plt.subplots()ax.plot(x_values, y_values)ax.set_ylabel("temperature(℃)", fontsize=14)ax.set_xlabel("Value") #坐标轴标签ax.set_ylabel("Square of Value")ax.tick_params(axis='both') #刻度标记plt.show()
+ m$ O- O( K5 E% A5 V/ N6 E 所有的内置样式有(print(plt.style.available)):
( o I5 a9 w0 I( d2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
7 A; V; Q! k" s" Z2 l
' L O5 k$ ?9 M. }, i2 @' M% v4 T( V K
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()# P @& l. n' N* U3 u; ^; n0 j
. L' ?4 u( D. ?& @0 y( Y8 r) Z% O
如'bmh':
2 k8 y- U4 e& W0 [; S- $ z, e$ N" A2 }2 \. K- U7 [
# N# o7 k) T- x& }6 d
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
' E3 I8 A" I7 y: l% m/ }* `) p
其余的样式同理可得。 ; L& H$ o) s8 P
第4步:使用Matplotlib绘制简单的散点图- " S4 P5 z5 Q6 \+ t9 G
- 0 t, {9 Z& l `& y0 y* D' s
- & F; }- i, F0 B9 u
/ v0 O+ \: p6 t9 ^" J+ A( V8 H
# |& Q: d e. K- 9 }2 v' K$ N2 P$ a
- . ^! `* l! }2 B8 k
- + [! X4 C4 e2 W6 _$ w& ]
- ' ?# V; f6 T: v; s; Z* O3 o6 O/ E7 C
- ; m" Y4 t( R5 F2 B; y
; F& b E8 E! P' U5 x i
( `: c" b/ ^+ U7 s- & }8 I' e0 J5 ~; m9 c. t6 q" t0 c9 \
- V& j6 s& q q8 Y3 H; N1 ~
import matplotlib.pyplot as pltx_values = range(1, 20) #取连续的1-20的整数y = [x**2 for x in x_values] #x值的二次方为y值plt.style.use('fast') #使用内置样式fig, ax = plt.subplots()ax.scatter(x_values, y, c='red', s=50)#绘制散点图,传递x和y坐标,点的尺寸s#颜色c,可用设置为'red',(0, 0.8, 0)ax.set_title("1981-1985 temperature", fontsize=24) #标题ax.set_xlabel("Value") #坐标轴标签ax.set_ylabel("temperature(℃)", fontsize=14)ax.tick_params(axis='both') #刻度标记plt.show()! S# E+ D/ i0 a" p1 Y
注:内置样式可以更换,这里选择的是‘fast’。
- E, R) ]; \2 K5 v ~$ E' [* G |