|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
7 | U. [/ A& w
3 B0 k$ e' `# O V/ t8 L' F& I( F
4 N2 ?( {2 b+ x' m9 D第一步:使用anaconda安装Matplotlib库:
+ o" _0 G3 o9 B2 F" u% E- l7 j' b2 P) E7 D( b" y7 `' e
conda install Matplotlib
% _+ e4 |* p: t" p
* {1 Q0 t: I) _ {# O7 @. l2 j' m2 W$ @& Q8 S7 Z
# W& k$ z. P! ^ \' e) c
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
& Z$ F$ S! L5 k; T; W2 r+ U- % O* X$ g; P" y& P
- % M. v6 b2 _$ n0 q
- 2 l* T9 L' o4 G g6 L. f
. ?/ B6 A4 P! Z$ j( S/ M- * w z, i: Y6 e: ?# U9 l
- 0 q5 }4 R- e8 p
, W* O# f" ?1 {3 S5 |- / o" ?2 _# m1 h/ Y" @& F
; g+ C! p- b$ v0 |- M
- s# O6 T6 g% \& x5 K8 g7 V
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 Q9 `6 j% j$ y7 e! W+ R" V0 d 代码读取后显示:
+ ^6 k2 Y. @% [. n- V' S
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - " Y; N0 V1 E, M+ g( _& |" j4 a0 x* T" s
4 F4 ~* z! n- [- j* ^
* B w! k5 e* F$ `
8 D( J& ^1 |5 [5 X x+ a- 3 \( B9 e0 M0 S- n* ~( e& N
- 1 A+ e5 k2 k7 Z8 F7 `$ U
, a% n( }) z# d! G1 P- % l* ~; H v. K. R2 A- T
6 Z( b$ J! n7 H* L1 D, _* ^
+ I- m" I: G* p4 M- , g; {, p) h8 l+ E/ ], S& i) D
0 O2 R4 t+ h) j: {' y
0 f# p: x1 g/ {$ ]! b4 u
" B) c# w$ [+ B$ g1 L& F- e7 R7 x! p6 W1 T- P
- 6 s5 M; W2 f( L2 p
- * e# M: \/ @. Z5 p4 f# K
7 g" R$ h# T8 N% I2 ~- 8 H3 w# z1 d# O' N$ j3 B
2 W6 e8 _; f6 y/ d- S; L/ e
) E) X1 } |2 m6 l# ]4 {" w. |- J% S- j! T- 2 y( n# B. }6 }8 p( }
- `0 F' V B& W$ t" d9 c
- ' F0 `4 w: z Z, N
* E# |+ K0 G- F' \" ]& S
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()
+ S$ h5 p3 I; I: t. Y 所有的内置样式有(print(plt.style.available)):
7 H/ H9 n! y( M2 d' I
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
$ u1 F5 T9 _8 H! g7 m8 `& I4 i
: l6 G+ e) W& w- ?! B! B6 f/ f7 V9 a0 g9 f
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
' t: W& ^8 q: B- n: M ?0 P
# R/ z; Z6 b2 q" l B X) J如'bmh':
+ c8 h9 G# }1 E. J- d% J$ `- - J5 c$ y# W! x, Z# F" }
6 T: u# \' ^/ H0 o6 a& I% c
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
6 q- {& u7 F) P) `, [
其余的样式同理可得。
% C: b/ _" b( ^( i) V第4步:使用Matplotlib绘制简单的散点图- : Y4 w/ @) r& w5 L
- $ c7 _" Z5 F% }" c' z6 X' o; S
8 Q/ Y0 E5 |* k1 u& J+ S( i- * x; G& a5 K' t- B2 l0 i0 P
5 m" v) e% A8 D( Y" b$ L3 t- & a3 E2 ~5 a1 v% J2 N0 `
- 2 Z: N# ~6 F! Y/ J9 i1 ~4 I
- ) Z- m+ k: @4 n8 L! F0 \
- 3 s$ m1 L8 u5 L' p/ X' }& h
6 ^5 s# S* E8 G& T5 Q: h0 u
/ A5 C" b) X2 h" u8 g5 i7 m) E
- H4 {1 D# x% \% [6 d& ]6 J
w- |; O0 p' c* a4 U! P% }' N3 ~, ]( E0 h3 N d
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()- `1 N0 H M/ \- ?' F1 X
注:内置样式可以更换,这里选择的是‘fast’。
- B# q( I8 {; F8 F3 ^# V |