|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
. N+ Y4 T0 w) ]6 k
5 m8 ?. W4 x* B3 s! j. g% I) F
& E/ t# \& R# w0 I
第一步:使用anaconda安装Matplotlib库:
2 T- Z, S# [- K" g: o& z
G& s8 q5 [! T5 k
conda install Matplotlib
# D" j# M! @2 y
4 M7 `8 Y6 [7 o/ |* d1 g) E7 \, B- ?
. k& P/ {. f$ ~! m7 H0 ~
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
% j" o X* U7 o- 0 Q( }, H! o* F) _* I. x
- ! C! s5 s1 l% k: X! f3 j& J1 A1 j( C
- 3 q: O9 M# M. S0 n
I$ W+ b$ y" f5 h) m& \2 D6 |" y$ k# F
- Q) x' I: K; I. ^) E# r- P( M: {- 3 O7 ~# B3 P' w! Y3 Z4 I6 n6 P" a
5 F. b" Y+ @- D+ s- 7 M, [5 V, q) G8 M# _
- 4 M' B8 f v! Y! ^. f' V. |
8 W) `) q9 a6 g: q' a6 A( 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)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()4 `' W2 k6 }7 r3 }+ _/ n
代码读取后显示:
2 L" C- U: w" Q5 u
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - 7 S% g8 d1 [" z9 q
3 s) e3 J. ]# R1 x8 d" t
( L2 M& {( c3 u% m* D1 ]- # I+ d$ K& E- F6 l7 l" b |
- 7 d9 g2 C# c8 T' R2 P
- 5 d" C. d: D$ c( }% U
- 7 _ C' Z) @; r. w1 m. r
- . Z* X2 ?+ {. M4 I7 g$ `5 ]
; e" h8 e i- U' Y- # }2 ^) Y4 t& j; o! _" \
# w8 f0 J# g+ e1 F3 X; |5 D& R- + k2 @, C0 b% H+ b6 @( I# F' ~
9 y# b$ w9 n0 c
) m) O7 c9 D- f
0 S2 B9 y8 G: D, ]7 m, \1 b. W
+ a# r. ?! i- `; b, y9 m
+ j( J9 S j' l. B+ K% w
2 g0 E3 l! D% v' L; H) e$ y
" J. ~6 {2 U3 }3 r4 m- q
7 U: w5 U# {7 [& C: H6 d- ' `8 h) r* A R
- ! X8 C* ^( @$ d
- ) a/ j! p" c# Y( b
- * \2 |1 J# I4 I* ?. G! V
1 e& s% S+ v# p2 f, F; n8 X
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()) a7 M* }! l& x4 c) W X* b7 z
所有的内置样式有(print(plt.style.available)):
" F( C1 M% V8 e& L; q+ e
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
2 i$ [! [0 C7 v7 K" e) b& b( z- * [; P( w, J' L* w& e
: O5 E9 ?( G' b: u% X' Y+ M0 @! [
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()6 Y& D ]8 u3 S8 B. L7 |% }! S
/ ]' L" _9 [% u' D7 U! y* X& Z
如'bmh': - , R- z3 e* A; i$ d5 I: z; q. s
- $ z5 `/ i$ Q0 _/ d$ P: T
3 a# U5 U% f* C7 L7 R2 ]
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()8 V/ O5 ]; I* C/ T G
其余的样式同理可得。 " v' C( }* ~/ O# U% a* w7 `' Y+ W
第4步:使用Matplotlib绘制简单的散点图- 3 x6 x! a* q6 d" ?6 P
1 d$ O" A4 s( G% m$ [+ R1 G
% ?( g; D4 r6 L6 n5 x+ L* i; `0 E- & E' `5 f* h) k$ I3 e3 r" T
- ' g2 U1 b- w! @- U
- 4 O/ a% f% V e9 O: y
- 8 L8 D+ G% i' |7 x3 S1 p( V. c
- . b0 y2 {: q% l" j- m- f* }
3 P% B: w9 P' o8 X; b2 n
' V$ ~0 J6 g5 K! L1 Y7 Z7 K1 O; v# Z
& Y% q4 }3 w/ x: _
8 g, `3 [5 C- |6 h1 h
3 z: [9 {: E- e) I1 d# _! E1 i. e9 p, k$ H, \, C' M
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()+ b) z! R: G. O8 m* J& p
注:内置样式可以更换,这里选择的是‘fast’。
& Q0 a1 T* Z8 [3 M' p' F |