|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
( ~0 d! _) E& d! X& X* l
& ^3 g/ S7 j; ?" g
. J( Z4 P& x$ J
第一步:使用anaconda安装Matplotlib库:
5 q; \4 N% \+ ~8 M5 K% Z) s9 e* V: J2 b5 D7 z
conda install Matplotlib
. a- |4 Y/ h) m! H7 e r/ n7 I8 ] 2 Y2 \ D, Q z G
. K1 K+ _' D5 l. g" A! N
6 F# h. `7 o7 f& D9 g& i0 p3 Q
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - A& x0 V; ], ] b, o
- . m7 D/ ?4 N6 d: n
' b3 u0 Y* l+ P& s* s- w
. F x2 G6 A5 [( u S- t- . b) U' ^! A2 P! Y2 f' E, Y
, _; l4 b3 r/ g
# u' i! S- G/ M% z0 I j5 K- U
5 F% `: a1 w! l) o% S f- - q9 H( e) T5 B2 _' t7 H" W2 L
- / h" L, o3 Z% o+ ?
8 M6 h7 f1 b9 [& r
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()
: Z7 s; C# Z' M& W& ?9 Z5 B5 i& D; E w2 | 代码读取后显示:
, m+ E* K" X1 s- m2 N
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - # A; B* q. c n# S6 A
- 5 B$ \+ }3 e3 e1 l
0 Q0 k" c& @0 \" D
. S# w- @& P; I, x" B- 5 V7 \( j- W4 f) p5 Z" t z. j
- 7 v0 M0 y- K. G; k3 D
( m9 B% M! I) {+ i/ ~- , u9 k3 q$ p R) Y7 M$ v/ V2 j
- - ~, S9 D+ G0 k
* T3 b+ O/ L! f+ U5 b- / G8 n+ F+ P1 _# O5 M+ C! F- b! H0 D
; M6 o% H5 K8 z. t# Z- U2 U5 ^1 R$ E- , V" T7 h! O3 q- B$ M$ V, i
2 A& ^3 X$ W5 W2 G( k' S+ }" k! a2 F
; k. t, u. m$ @+ U1 F; l$ s. e4 E
! b% M1 H6 d, J& f6 [$ N+ B
9 A" P/ L# w0 h6 ~2 Y
: I& r5 b' A" ~- L% y* q0 v' C) Z0 r- ; r! S" X" Z3 T* u: l5 L _
. `, G; m9 O- {4 m0 H- ! K% a0 L5 D w a: V' ]# p! {3 S
: W2 h" m) V5 J f
) N) Q; B# d; f( i0 |- " u" M P* t* @$ ~; J
/ A: k$ N3 R" J8 [
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()9 x& Y* o5 [, o$ P2 o# n
所有的内置样式有(print(plt.style.available)):
. ^3 p* X/ l% [+ ^( W: \5 H6 |. c2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - ' p& u1 y5 w9 @$ Z
- % T# a2 \; S+ X8 C8 ?
& ~% f5 H# U# k( Y# R
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
2 o0 e4 m" g. j. p
; t3 X0 ?, j# m" ]) o5 |: n
如'bmh':
. Z s- \0 u2 f: T E
8 `- S- _" a$ X' p* {4 ]% C1 j3 z
5 ?% h9 b. T( v5 z( G( S
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
: L3 D8 m! H+ |( c6 C5 T- k+ [3 w
其余的样式同理可得。 % g# ]2 T# O2 d9 X- o$ x" C
第4步:使用Matplotlib绘制简单的散点图- ! c0 d4 ^. c5 y4 v7 s# [( \
/ a& E$ U# k \ N2 ?% e9 r2 N
; d8 m# x- Y8 Z- * M* q/ r2 n% F0 S$ A6 S
2 C- x: Z, B0 K* X! Q- * }; A4 W' D3 d( k5 ^9 V( P) r
- $ n7 ~4 i7 J/ B! s" q+ t9 y' a
% ]7 j/ {4 J$ a u% L6 x) z+ O- ~" i- + j6 n R4 _+ ~: x% `! ~& T% y
- d5 V* z# G1 o& B- # A$ x! a/ ^6 S# O# Y
- ' ^9 V# G; s* `9 J0 f2 A
6 M9 F! Q: a% O! k& D. K- E8 u, 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()
. e1 g3 }& Y/ V2 y 注:内置样式可以更换,这里选择的是‘fast’。
- ^! u/ ~. P1 s6 q8 p |