|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
. a) u- w; t! ~3 u& v. z0 E; p4 V/ u5 t! R( w+ l. p/ F _, {
% \5 }7 n7 K `第一步:使用anaconda安装Matplotlib库:
' I: j7 M! Z: |! f
6 N4 E% g# c" H+ l
conda install Matplotlib) E# H0 e; J1 E- @/ Y# I u U m7 z. R
, U3 Q3 _! O; b
# t2 c: z9 x$ l' C" ^: _) Q) R( G9 Y# ^; s6 E
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - z4 Z" {& C( M9 b& A
4 e* a+ f$ S: c
9 x7 d7 |- N0 s! @, x! P! h- 2 q4 h' Q4 u! o3 o
- . m4 S3 q# g2 P7 E7 ^8 ~
# J' d) ^. G5 g( ?+ o- + ~3 H0 s+ U1 d
8 q* N) T# ^3 H/ _0 @/ X, d& [: q- : A3 |4 h, J3 `% z F5 S
- , B: j1 B; D+ r7 u5 K: P
9 f" O5 n, t9 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)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()
, Q5 K0 k& S# a { 代码读取后显示:
& U* O: n, C* \) C8 |8 v% b
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
' `+ h; Q- [4 R# V$ {8 O- ( Y; y/ o: D/ F: H
- $ T. Z. r: P& @
# u: U/ I8 F7 Y4 Z
$ V8 A6 {' u4 J, m" s: H4 I
A* x& I. C; b: S5 H: s2 j
3 b; Y. U- d' _5 g6 @2 ]' C! ?
) o- [, Y2 A" A; O9 N- i
2 P; ?8 G0 ^9 N( G! f+ ~7 S- " k* S9 f& h' F5 Q! I; l
6 b. Y" }" r! s9 j/ I; p) _* n- / T) h* ]' Y6 E. L
- " m% Y8 q H5 x: {1 g# X3 o/ ~
- ) ?2 V( S' r$ m, k9 A
- 1 |9 h( j. C/ u u1 O
! t: E' {- E% G! ]8 M- % U+ H1 O5 M+ P4 U( Q$ k5 e5 S
* K+ V: t3 G# g6 U5 t" Y! U9 g- " Y0 K6 V0 c6 q& j; K6 S( b
4 D( D- W. I; X4 f
! z6 Q: v) @3 o4 g- y
1 `/ b7 L: n+ U( N Y/ }& u- ) ^6 C6 Z' Y+ }( p3 A {
; m6 g" P+ Y- H& B& H8 B8 u' p4 J
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()
) O% ]# d& I" s- O5 Z) p S 所有的内置样式有(print(plt.style.available)):
% J! G+ r$ y. T
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - & p4 s( o4 S6 b( L% a
?) S3 e% ?! p7 w. b5 I9 I4 h- \! Y( i4 b- q. I+ ~
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()* C* g# s) j# T# W
; [* z( Q5 }) w8 H! v$ i+ l/ A ]
如'bmh':
3 R" o- I# P6 k5 x2 N X6 o
/ C ~' D( X0 d8 u/ p' c2 K2 B: b
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
a, Z! ]) E2 ~8 u3 H7 w% h
其余的样式同理可得。
7 ~7 f8 R' C i6 u% n第4步:使用Matplotlib绘制简单的散点图
2 j) S, `/ m/ D9 ]# K2 K/ r# F$ w- " V [' R$ m2 e2 f, W
# k( s; o( O) N1 t$ C) I# L- " M# V ?/ ]' W6 h0 \1 w4 n Z+ ^
- 0 w/ I. \% ]3 q- |, a8 O# Q0 w
- 0 V4 T) _5 l: p! t5 s6 i( u
- % l8 {5 z# g- P+ `
! G& F0 h% o. V- - W% i+ m7 H0 r3 E, k$ l$ i
- 8 T3 z0 K9 ]' s F! @- e
- ) V. X& r4 f/ Q5 C1 n4 c7 M/ T
- 7 \6 n; `2 I& \8 ?. K+ v# M
# D5 _* l/ T) ^; N3 |' T
8 O+ }/ b% Q6 p6 j. I
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() E. T4 _9 h/ _! X5 u) Y
注:内置样式可以更换,这里选择的是‘fast’。
`$ P/ F& r. x W. R% B |