|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
: o4 a! f( f6 n: {/ _$ o8 U& ]
7 e# w' z; m+ ~" @0 K
$ ^, O* b8 T% t
第一步:使用anaconda安装Matplotlib库:
$ q; ]/ }0 C: p3 v* X! i& W0 w
0 F1 p H& M6 p# d: d: S0 G
conda install Matplotlib3 m1 ^, x( G, j% O) p1 P' A V
n c2 q/ v. w1 }& ^6 k+ ]1 f$ I. n6 W9 t. c2 w E
! @6 w6 A1 G4 i j
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - 1 c1 t( i% Q" U% d
/ V) g; r; k" d' z4 \7 |( M. N- + S1 R- N: F0 \6 l5 o- S
- ) f! v0 i& Y- R0 P' _4 W
9 p% ?9 m0 @) h5 [) |* L- 3 K- m1 O- \9 v9 l4 B: p% N+ J
0 T: |$ B, g$ O- ( R: v3 r g7 ] o* X- ^# n/ \) Q) u* S( ^
- ! d) p9 w8 e, e7 [: {# a
- 5 I. W% E7 t( v. j1 L
/ A6 X, A- B/ u' w2 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)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()% X" }; q, D: `' h6 T; T) Z
代码读取后显示:
$ x+ s0 e5 w$ |
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
6 M1 m! S& P7 p
2 q2 T9 c. D, S4 v
! x$ I; I# u) R6 I* G! J
8 ^! m# A' ? \8 z. n0 C1 R
0 m; U7 f7 j1 |1 O/ b- , D, ]: P4 L4 z4 r/ k: m; a0 @
- 1 {: J9 S4 b/ T Y( j3 \% X
. h% a$ }+ r( d6 Z
3 F+ n; E" i: H& |
5 {& x8 ?/ j% M, m5 B
, F; g, O' w, Z3 U6 A- ) h* u3 U% R7 ]; B0 A
1 |) u3 j, O+ Q- L- M/ U" \
8 y% G- z5 h8 x; m# M3 s; `- " o" ^5 _+ U9 z) O3 r
- ' K( ?' ]" n8 I" N G( T7 Z5 t) k8 F
- ' e" I1 f$ T, h8 S! L: l
! l5 X3 G4 g+ J) ~1 j- + N8 ?; d8 ?, X
- ' b0 i. n2 A- b& j7 {4 Q
- ! _. {( ~/ q: H* M1 F- I; u
' Y. M) X1 K9 _+ o+ {* x- 1 \9 f: o9 }1 e
- 5 u4 P& f0 L4 |% Z- e2 N$ u
0 u& v1 U8 \% E+ n( E; 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()9 ^0 Y! B. ]: l
所有的内置样式有(print(plt.style.available)):
% `5 m$ T0 t! J4 _% P$ L
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
* n5 k# o- l- D' G/ O; S: B) L- G5 K' K) Z+ X% Y
* o& o% Y3 ~: p5 f" W4 N2 a& K
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()2 C7 h7 T! l" d- g+ p/ |! J, `+ @
- c: y) ]7 @' y0 c0 u: V+ w
如'bmh':
) s& j6 {, c$ C# p: T- r- # V+ C0 D- ^) x3 J
2 E! H; a# z0 v/ Y3 l6 |
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
; u- ~9 U) l9 F9 A: F6 j
其余的样式同理可得。 " n6 l; l+ r* w
第4步:使用Matplotlib绘制简单的散点图
$ ^# R% m; q5 g, a u# D ^$ g# E3 B/ I$ r- , x. V- J5 d7 c4 c
- j2 ]4 w5 ?" A; g# W8 W- % Z% d9 c& j, m
8 ^4 h/ |% ^2 t, b* l$ M- 7 Q2 e- x5 K5 U: m! i
- , T* i$ x8 C' C* a, G; ]
( {+ a- `/ e3 i* b/ y
" P& L1 s1 }6 g* U& g- ( }) L) y) Z4 b6 C
% ]( ^ O+ x; d( Q/ J) C9 N- . G6 f9 T r/ P$ }6 e! F
- 9 c6 K1 F9 e. s/ J
- c8 D$ M, r' x! ~
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()
7 S6 ?* k9 t! j. t+ D. Q2 V: ?8 o 注:内置样式可以更换,这里选择的是‘fast’。
. B* f( w& v% d) F. Z, z' Q) j
|