|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
/ f1 U0 R% p9 W& u1 `! c6 x2 @8 J8 Z% F1 A( G3 A, d
; C: h! H- ~3 ]. _6 s$ w0 l第一步:使用anaconda安装Matplotlib库: - 8 G* V( H# h% W C/ W
, S. A- G& z) ~& @" o. ~
conda install Matplotlib
% u2 y; C! l6 P1 \3 q1 T+ k( c2 _5 A & V) g% i9 ?. d# t' n
' P' z; C6 [1 d) ~2 y
0 q3 E1 B% n( N) ^6 A# h第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
2 P! `$ s3 v6 e) d1 N4 h+ v3 |0 p- + c3 K; j( o+ [$ i$ E1 y
- 4 M0 ~: i2 S3 }8 i& s
- 2 y+ _/ V# q8 P4 E4 A; m
- $ ?. j6 p2 d2 Y+ F: C
- - t+ [) w* w- ]0 ^7 l, f+ n
- 2 s% F& ^* v& ?9 o
- g2 S, R* C" J A
- / y7 K- P4 i% ^) ]
5 `9 t9 q& v9 m# h1 k4 @5 R
4 Y1 P/ a( `7 G+ M6 s! X0 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()
/ I) R. X4 @8 h 代码读取后显示:
6 A9 |" }8 Z( F1 L C
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
" ]+ Y4 M5 }! d: M6 {- h+ |- & i* W) R& i: l
- : K0 P/ H/ x; r
' Y9 S0 {6 v9 ]. M- G- : t& C( w) S& V2 r: G! W, O l
- : z, j5 z( r% D* ~' o
- ( V( D! K' C$ M0 }3 j- `' @
8 _2 }# d% m2 o; `; Q) a- 8 b% J8 G2 i/ y7 c" N
" H& {) e5 l/ A& o" {* N9 U n
1 `$ j* z4 ?! e- 4 {: n1 L7 ~) ?' f; |! L! q+ j. L v
# V' ~' s. \8 j @* X9 N, N
: ^# L% q' P2 g, s, A' R
6 U! O/ j6 f+ B% m$ R$ R9 Q; H% S+ d! [$ |- / \) B8 T+ b" J* \
5 }' m+ r: L: c$ s+ |4 n
' t7 H/ A8 }0 o/ M4 N: d
$ T& ?5 ~5 Z; _3 l& e0 k- n# }- : B3 u: t4 r1 p0 b
) ~3 x8 `3 r% Y9 Y# R, `% A- " d8 ?- _" Y9 @9 R
- 0 \$ N% b9 q, ~" G3 v7 ~4 ]
' ]2 V; G2 {# I" J9 T- i! I4 P9 z. t: Y; h0 L! F
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()
2 K1 U' T$ h$ w 所有的内置样式有(print(plt.style.available)):
; L; H( c8 s. t1 q- e2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
4 Z+ e c+ I; A5 ^- . B& t4 k$ ^- L$ R0 P0 e" w& j6 H1 r, A
6 U& q- E8 B6 b# H+ }
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
2 A" w( p+ s# a7 t$ T* Q: j
- f: v9 |0 z: b7 g3 |
如'bmh':
; o' L9 `. g+ i: R6 ?$ A( P
1 G ^4 _+ ^+ d2 j& p" x6 ^7 F0 j8 b8 ~' g" R) P+ x% V
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()) V" X4 y% O- q9 ~
其余的样式同理可得。
+ [% U7 s( Y4 v* r* }- b6 H8 E/ L- \第4步:使用Matplotlib绘制简单的散点图- ) v. E4 q3 t/ D- h' S# _3 ^6 X
& ]3 ~7 I* u5 C5 ~4 j
2 n, j( n0 R8 `6 z- 0 S8 M2 P/ W0 x7 G. n0 W
- . E. J7 y( ]# b1 `; I' V: \
- * R1 s d$ i7 S" e7 z
$ d% e1 U; z; v6 R R) n! X- 4 }$ E- C( X" l# i6 ~
- 7 A8 r0 G. w1 g5 X3 S4 r
- 5 }9 A: _3 I Y* S
" o3 c c9 M; |* u0 f, j
" A. V+ M+ K9 m2 n3 ~1 v- * q/ w4 I, ~9 e5 t
6 V0 y) v+ v, o Z% ~4 d8 K1 X9 y
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()
& [0 h& K7 Z8 C& \& V 注:内置样式可以更换,这里选择的是‘fast’。
0 v" H, r( C3 I' o |