掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
& f m9 @+ Y' g: `; N, n7 m
$ A; O& w; {5 ]6 f4 c& I! M, o% E) S; E1 [
第一步:使用anaconda安装Matplotlib库: - ! i; e# e' p1 _# a8 o X9 T3 y
5 p& S. Y/ W% l! P, `8 I/ Q, d1 u0 l
conda install Matplotlib
6 F' i+ E5 u7 X- y; R6 `
( [5 i. Q/ P' P$ O) g) b* O/ w4 V
/ z! h- ~( T2 @1 X" z4 Z7 f第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
% {# {5 e7 }9 l4 E# z8 _+ m
% X$ k0 p/ K; \3 K8 G& c
0 e8 R) m1 M9 S$ V$ Z; |1 S
# h' N2 s& I: P* D; c+ S
& E+ N/ _2 e c
# ?4 w# J5 J: J& N& t8 u) h6 a9 l& C7 G) n- ; S( V5 p- n1 m' s, |9 [. c
- 8 o, C. v( I R& r" c' a
. ]( @7 ?+ v# r* d- ; }1 E- I+ f% r
9 s# P& E5 x' `& H% y8 d y1 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()6 i3 i$ F2 g' F7 i [% x5 ~' K
代码读取后显示:
$ j* y( \: F! K! S- t% T: O0 }) s
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - ' `6 c. E9 w' _! f
% J5 x" r! ~0 Y3 E/ F& p1 c
- @* Y7 O2 }4 Y8 L/ G- + b; p: a) E* u! H0 A
) ~ i& [2 c# o/ a6 m v* E# D- Z
& f$ y# v. Q: U* L
3 I7 a/ q+ W# \5 e( y& Z- . B; I$ Q; b) e
* ]8 P8 p! F- R( ^
! q- P/ w z2 X- Z' @
9 N; z5 }; p! p3 Q" n
+ L" w# t0 p% I3 M1 X$ c: J6 X( p+ C
7 a2 B+ W! d' i M! e2 R
% U8 w+ m2 `3 o; t& _/ {0 ]
# W4 g, j6 O( T q" H, x" G: ~
3 ^- P+ A+ P6 B8 d" _5 A! `
# G' f" B9 ~. l, I% Y% s
: D0 q. h2 n( t1 [' [
, B$ t7 F- l" ~5 e6 |2 r
: D' _+ V8 V p6 |2 u- 1 m% @3 p8 _% V* H
7 p% D3 S; _7 b
4 h7 s5 ^ p* a5 t V& _3 S
. K* E, ^ e, I
$ t' a" F2 r! x5 p$ 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()
! {6 A Y# {) r 所有的内置样式有(print(plt.style.available)):
* \8 b8 ^( ?! s, F% l* ^4 }
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - / @: T2 A6 Z; T# ~/ l/ d
- 0 O# d; A( ]( r& ~; l
) T, F- ? `! O$ k) w! V& {) f
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
" B# i7 J+ @4 x" {5 r
4 @. F$ P' e( \ _; A" i
如'bmh':
y9 b' ?2 k5 ]. F7 I, v- $ U4 S8 M: h8 e0 [- P" J) _7 W6 d& M
7 l( D. X8 l1 A, K6 X2 n
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()2 `( |. }% y. l" B( J6 @, H
其余的样式同理可得。
6 ?' r4 R% l8 B7 g/ z1 O+ V5 X第4步:使用Matplotlib绘制简单的散点图- 3 Z: y, j$ L7 r: E' V o
- % E4 x5 a$ @$ \7 c' h; `
- % c4 s) {0 q& e! g4 u5 {" g2 J
* Q: @( D4 `$ @. J/ F
S: U4 r' }0 I0 x7 ]
1 _/ M4 I8 |5 R6 U+ H7 x* u! k7 s- 9 ^% A2 ]4 v/ \$ a
- % x ~, @. i2 q, ^5 q/ T9 Y) [5 A
- ^+ i0 M9 d5 G- O& W* k1 a# o
8 Z4 n/ y# V/ W- 7 W* o2 f3 M$ c
- ; K' v: u. D0 U
* B1 I" S% M$ ]& B% D1 ?+ i1 C6 _; |/ w+ w4 W3 x' P% }2 ]
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(): ] U& g7 Z+ R: I- J% T
注:内置样式可以更换,这里选择的是‘fast’。
" f) X- P9 _/ {/ B" Y! Y |