|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
5 q. T3 {& c; A {4 C$ _( z: Q4 F( d8 z- P9 [) W7 D4 a
! w; z' O/ U ~( B$ |7 Z9 ` ?第一步:使用anaconda安装Matplotlib库: - + ` ~ \. c3 L2 e% k# {" k6 N0 }
4 _. [" j ?: [
conda install Matplotlib
/ w$ ~: E% v# y ?! K2 \
+ R: u Q! c9 R6 O) _1 U8 _3 t1 J- ~+ }
; [1 `- W( B" H3 @9 U, d第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - / G- `9 U2 E! A7 B. |9 O t/ L& G( E
4 r) H! G7 u) \8 g* w0 y2 D6 K
9 Y3 K6 s' Z8 X0 I" r
% l2 n0 E4 i# U- , i# z) t3 W. h; Y( [5 V& {
. \- m0 b9 B. z6 x6 W9 R
+ D0 J; t4 V h* |3 f( {- 5 t1 `- {0 Z2 |4 @6 a# t
- ! d4 B9 \4 k3 |, O: N
" j' g: \! S2 i/ ~; B0 J# ^ x3 u* N+ U" q" T3 |
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()
$ |* e! j* p/ r/ z" p! T 代码读取后显示:
- A" X1 b: }- i
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - 0 O" t) S! c/ L9 V0 r# R
8 C1 h3 u0 T/ c
$ k" p* r8 I1 F- ' E: V6 h- A! g4 N6 I$ g3 P0 }
* t r, N3 B2 [" ^+ Q; ^- 3 ~! m5 B1 F, F/ }9 n/ V* w* l
- 7 ^: F, s& t9 D5 [; k) ^' w
- 0 u3 S6 v% m! v: |
6 M& H( R% B1 M& k: D: |% {; k, ^* T
( e$ N* d0 }, i- % t' F7 j: v8 i+ D3 `
9 j+ h( p) G8 ^1 h+ {- ! w3 s6 t u. t) |
- C. s6 ?# O6 h- w) A! e
$ v8 K3 c6 C, }& _7 I- 2 a8 z- r2 N3 q5 `" G
- 7 S/ F- D+ H- [* ]# \
1 A* k- C) {) Y/ G* ?0 t! w1 B7 |( L- " s7 j1 v" s% c' `' |$ i
4 r% l$ k) Z$ {$ {; b" K; S2 M; F
& s+ c5 K% g, M. s; u! E- 7 {, Z1 Q" h! i& j0 y( H! p0 i
) P* N/ ~( C2 p9 c' l- 2 ]3 Z0 @3 O5 ^
, \2 R8 _. q3 k- Q# e. P9 Y
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()
, V" n' P/ o! ~6 H 所有的内置样式有(print(plt.style.available)):
& a6 X5 k8 w- {6 J* J2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
5 R' {. v" f: H4 r9 U- / o( j+ g- v. @% C% q/ r5 @9 H
2 w9 P% v( n( d
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
( ~- S; Z+ |2 Q
( _4 z; W7 @1 e$ Q7 f2 Z$ v
如'bmh':
6 g$ Z3 r% p+ s* _0 U( I8 X N- 4 B0 R3 e6 a% d7 C2 j. I( l
6 }# Q( ?9 e" U& D% g0 s
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()7 I a ]5 R, Q4 z* s \
其余的样式同理可得。 + N$ k+ y; O" ^2 }9 h6 c8 D
第4步:使用Matplotlib绘制简单的散点图- 1 \5 Z' t* Q3 {" G8 S
- ! o) [, ?1 v1 \) b6 n
" v: N0 y, e# v
, i3 v8 X2 {1 n7 Z$ Z- . g( W2 c, b |
- ! X- t, _- s7 V# c+ M3 f; y
- 2 n/ j$ ] s) ~2 B" @( n
- ! _/ Q. _- a5 q7 n3 C5 I h& `6 m
- " _7 v' z, e, p4 V
& {4 I6 Q/ L$ C- $ k" x; h: B/ {) v- A; M
! S7 M( @& ?+ C3 f3 s8 M- 2 |9 t, V; I0 ` `1 M1 _+ x
6 `2 S0 q. D" O' H+ x# K2 j
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(). y, V+ w0 }( p# c+ [7 O4 N# g1 D; T
注:内置样式可以更换,这里选择的是‘fast’。
; H; f+ h3 }! s1 z+ [1 `
|