|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
5 M; g) \( L& q; ?5 Q6 L# H2 D1 W$ }5 X# H" Q
6 U- e+ R% `, Y$ @第一步:使用anaconda安装Matplotlib库: - 3 H ~& I: @6 @3 p; e; P2 ?
* o4 r. i9 a% i7 ?5 \0 T5 F
conda install Matplotlib
: A, X2 ]- Y6 `9 T2 Q9 c) u, k+ q
. o) U5 ]2 _& a' {* l
6 R) X( f# }: c# m& t6 d$ c7 s( N
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - 5 W5 Z0 ^: ]$ F% s2 i+ d
- * U3 |2 n. v, b
8 \! N0 L. B3 C+ S% G3 b8 i- : c/ F; o0 Y; O
- 6 {/ Z7 f9 P7 @0 k/ Q% m# M' b
- # e% C; w' E+ S8 E1 s4 D; z
) I* [5 x D: Q
) o. F5 w: C: p8 C, J
1 {8 \% h& ]3 X- ) u* ]9 F/ f, b, E7 E
2 d1 Q/ r6 p% Z/ \6 ?- e1 X$ 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()
0 L! s# s3 O m4 b 代码读取后显示:
( g: W% ]+ D6 C) T2 N
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - % K; W. ]+ \* `1 t8 o
- 8 z6 @6 l* |- ~# S
5 l6 h+ [" ~4 p- . e* M) v7 y8 X8 F
9 l5 w$ Z8 I& r5 b
$ I: |: u$ g' C! N2 H- * B4 n4 s! y6 X, C! `. |1 k, N
- J- R' ]0 J$ S- " p6 v/ q D; j3 I0 t" ?+ w) n
4 M+ y" h. C8 O- ]* q N3 p
8 u* A; |: q. H
( }. B' l! k- c X b; V
5 V6 A# j3 H% A2 @( d: U( p6 m- }8 b0 ]* F9 r" J5 L4 p2 x/ p8 S
- / t6 r8 H% V3 W9 B! f, M
- ( O6 E3 [- y' q' O# ^ o2 y
- ; y5 t7 X8 A: F8 @+ b0 ~
) f/ O% T9 z8 g6 a8 {- 6 x h. y- P7 M0 m& _
- 6 z5 y- b/ [9 R6 H( f% S2 f# H
- 0 p5 e, d# ~9 |' T, A! b5 a4 L v
- ) Y$ l4 Z( F% K# K! y9 K/ G5 x
' \( O# _' ~8 X; t4 d/ ?- 1 z# e* @& D6 y, ]# ~# @ P# x
9 O0 ?9 v6 F1 n- v# d; @+ 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()% p8 g5 n6 m/ R( p' n7 P Z
所有的内置样式有(print(plt.style.available)):
% U$ ~4 t7 b1 d6 P7 v2 K2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - 4 u# q8 v/ t1 [" w' U3 j
) m6 F7 S( A+ |7 N! `' e0 v5 P! x' c
& @9 S: y: s+ J( R8 w" K- J
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots(): l6 g5 A# z9 T1 T. L
, ~$ W+ l/ X+ f7 A
如'bmh': - 4 o/ F0 x. A b- t- g) t
- 5 l/ H7 }- V1 W
d( W* F, H) x
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()) x6 ~/ g0 R( d7 X; P3 { ~% b8 W
其余的样式同理可得。
4 j; J0 V; D1 V第4步:使用Matplotlib绘制简单的散点图- 1 g& ?' ?" e& G
M0 i4 {& [# j
" N1 Y, l' v" _7 [( ~
: r' p( z+ X, U% ?
; e' N1 Q% i# E" ~
0 Q) x3 o( ]4 P: Z: [
9 ~, ]: v" Q& `3 M- u8 J- ) T1 D+ d$ r: \: D: i
- / u# A; p6 I- v3 K3 ^. w8 P! ?8 t
1 k5 D# p e: J6 A4 k
0 u3 N& W6 _5 E! ^$ P- s4 Q' O5 W* ^
$ { k2 ^# p% s! p) {% p1 H/ i0 X$ H6 A' l
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()- M7 ?7 U; I. y8 B7 t
注:内置样式可以更换,这里选择的是‘fast’。
/ i( R4 u5 }# t |