|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
: ` K4 l o! P9 ]/ h
2 f2 l, K9 D! S4 C2 ]) h* c r
2 P& M u; b* u0 |9 N* N第一步:使用anaconda安装Matplotlib库: - 8 B1 ?: ^1 h5 e9 }) v' x( f; @
* C O4 A/ A3 Y7 \0 j# |
conda install Matplotlib
7 W3 \* i- N3 ?/ B / p" y7 o; o" ~# i1 w
$ g& J! i8 p! P! ]
: u0 _8 N8 n% e0 k
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - * E9 q* W+ y l* G
- 3 D& x9 k4 p j( @0 }, X8 R/ b
- - h5 t6 @) g( `$ V* T0 U, Y
, R! L3 F1 s9 ?6 }- 3 n# a _3 g# V. p2 g; t0 f
8 K& y9 `5 R$ P1 I% ?& l- 0 s' x5 {; h7 \: J( p; y$ i
- ) Y( x" x0 ?. q% `
" w. T$ ]8 r6 U! S& x/ ^- ; r* s8 f" Z- r" L
0 {: j, y0 ~! ^' E
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(): Y! B) A8 I' i Z X$ z* E: n
代码读取后显示:
% F' Z! L% @/ f: p6 ~1 Q" h$ q- _
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
2 D$ v, @/ j1 h
; d* q, _/ E* Y
5 I/ z0 a8 u* I. n6 ~- L; M9 J' R1 Q$ Y
3 @7 p. }1 l/ b$ T% ]6 Q
; @5 a. S! ?# }' Y- " c3 i- C, W' f6 X0 n4 T, Q
9 G L: Z& Z4 ~' l$ a
0 w$ {* Z$ V, D5 A- 2 Z1 V; h1 x6 e4 j
2 I1 N, U1 g; R) d- 7 x7 O2 e1 y8 ^# v# I% p
( b& g2 q: Q" M6 ?* H3 c
$ g3 G4 w, C; P) ^9 J9 P
' k7 ]9 D3 V5 x) s- 9 D5 m1 f0 Q8 g- }9 w' s F5 b {
8 R# a5 W9 R- H1 a1 R- 3 L2 b) c! g5 w6 w. a1 g* E/ Q+ n1 W/ H
" g$ {; v0 d; q9 o {" \- 3 U7 a3 l+ ?4 G$ `8 O8 _
- : X/ s) N- c5 O; Q* U0 H# B
- $ t! v: @; S& ^3 P( K
* m1 T5 w1 k( P+ R2 @7 Y- : `6 F! H0 K' N5 `) h2 A
+ Z; e9 O9 Y' D3 @8 M* p
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()) z! i) J, l9 ~7 ]+ D7 V
所有的内置样式有(print(plt.style.available)):
& S. ^- |& F9 O# n$ Y9 M1 i
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - " e' z4 Z' W0 U; I L- M# O/ m
$ n5 X# M' L" X) Z
7 {+ t7 s/ b# C# n
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
2 ^7 b8 I* h. b# c, ^) i
) E9 g. y1 u+ h8 g: O6 \
如'bmh':
W2 X0 q5 U" j6 ^3 Q" o+ d9 c- * ?4 h+ T0 D; o) o! w
# X; {3 i; C; i2 d7 C3 o0 J
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
: B, s1 j& {" `4 _( ]& x
其余的样式同理可得。
+ ]2 n" a$ S. D: b' ?; j6 H第4步:使用Matplotlib绘制简单的散点图
4 u! O! _/ }0 {- $ {" y5 R1 B. \: `2 S
9 l u$ y- |4 W; o) u; B8 m- 8 v9 u1 m+ k6 Y/ K. ]
8 h, O9 T( e. M8 n' t; |
- i2 N) T. A/ s. o( G8 j! V
; P6 U$ y( w3 Z! N
& z5 t+ q! o( K- }; f
) x P% {* M, G9 V7 m$ E3 K4 y- ; h F. H8 G4 t* P C2 X
- h$ j( X) E6 h( b' w
( q N' [+ J% P7 ^0 _5 R
# N. e( N: X& ^% s3 d I' J$ m
+ Y9 U' v! F6 P6 h. C
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()
; v' T. p* m2 ~! h4 \& ] 注:内置样式可以更换,这里选择的是‘fast’。
& e- [) F$ n/ J |