|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
1 C* T" v) F0 D; K, h, |* O: Y6 k% g5 R% Y w4 X" z. `3 F2 {/ f
t6 W7 m8 i8 {+ f1 c第一步:使用anaconda安装Matplotlib库: - 9 I" M3 P) V) A$ q; w6 R
" x: B6 c" a e$ O
conda install Matplotlib5 g! T& B/ t0 h: y8 e
# p; ?/ e2 U* x% F/ L
# i6 }/ J( ?1 N; i% y$ b5 h
0 X! a. w$ o# _5 W& L/ r第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - ( n: w: Y+ |& a( Q
4 R5 K# C# E: G! Q! R5 d+ ~/ K" |
d$ t4 L6 q2 F% _3 ^6 C, \9 S
! W8 x5 y, e+ M2 I0 n+ y- \- " t" x0 ?- {5 s( c" V) X( p# b- w
- 0 n' _. `* o6 p
- ! H4 Y I9 C1 `3 [
0 e8 {) `! G4 P) h5 t3 Y4 }8 a- $ X" U* P# M, w3 M
) W' v% H+ T. H
! a0 y& `7 w3 I/ u. 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()4 b( u8 t) y; Q" ]
代码读取后显示:
1 d9 G. K$ M: b# n- L. x+ a
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
% b1 A+ U# {( t: J/ g7 t7 y, F- 2 J6 B+ ]1 {) L" X; G6 @0 [
; c- m" |0 `& ]3 Z
' N: J# h8 d8 X6 t( Q- ?- ; Z9 ]" s- Y# B
- 7 ~9 C) t# Y& Z3 E- x0 t
- X" ]" F8 \9 o6 ~" t* L* w! \$ V
! ^: u6 p- H+ z8 T: n7 N- # J7 e2 W. U; N6 ~% _
1 J# H- _- f( r0 Z _) J& _, M' H; e% t
" p4 W5 u% D1 ~- ' B( \( k2 Z8 z* K0 G! R: T4 I
- ! a+ @# M* a& A, M/ @
$ t- u4 D" E( m
: E- F6 k4 N A- 2 c: m+ R7 X: [
8 {4 t3 k, x {/ ~0 L2 t3 [$ S) U- # d, x6 W- j, P, U3 @7 V+ ~7 M5 A5 P2 P
- 3 r9 W+ L7 u3 z3 O
- " ^1 Q) h& }6 u% l) [# W
: R6 Z# S2 h7 V3 B4 b) E
8 p3 ^4 q' D1 V8 M) o& S$ `- - _. V- J$ E8 Q+ P- j6 ~: h4 g
- 8 |) C4 W/ E7 y
$ `' c( J2 Y# c. T( A# A
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()' `+ ?4 ^5 u2 `4 H! D
所有的内置样式有(print(plt.style.available)):
+ ]) y0 {3 g/ Y o+ d' `* _
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - , S2 C3 N1 y. ]5 P( T+ _3 V; t
. R I6 I' T; l; n: N( l5 t x" ]' [/ O) s) n0 f/ B! s2 @1 J0 v& E
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()' ~7 Q7 l- D n ^3 V
* J3 B3 Y R2 {* d& \1 Y1 h6 F+ J如'bmh':
+ L6 ~5 e, B' Z6 L
f4 d9 @! A4 J. N! {2 e6 ~. q/ I7 `3 P
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
/ Q3 ~5 r, }8 b+ n
其余的样式同理可得。
' G* u8 P `! V" C; \第4步:使用Matplotlib绘制简单的散点图- 8 N$ _; ^7 ?- D: i8 ^; X
- , o# S6 J$ s* ^9 d( n _4 ]0 V+ J
- 7 M) A4 m) P3 y# U$ m2 ~* e
7 X$ z8 _1 x" E, ?
( u G2 O; T* x; Y* o7 x% U! ^
% g" f) x: A; B
; d- k* ~5 W" e0 o* ?5 }- 8 Q/ `( s4 c2 p- Q) s
% V" W7 j" X: o: d: E! y1 `/ p
- \; f, `( T5 e. f; ^- V- + `; X) R3 }! c$ o. t2 d1 f( G
- 0 V) r. {$ ^( F: d M( `
- . k9 P" u* y7 ]5 ?1 m' N
2 Q9 Z" F }# @# H7 L2 r
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(); H4 B- C; J7 p: \1 J }
注:内置样式可以更换,这里选择的是‘fast’。
- X, [" i e& y5 p$ K
|