|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
8 i; }: L# I8 A! F
% r: i6 f5 K9 i ^, }" U- j/ q0 @8 {0 `5 I( e" Y4 W
第一步:使用anaconda安装Matplotlib库:
# @$ f" m! K+ p. \6 m& c
' G5 H l% g9 i. `8 T
conda install Matplotlib
- @# `$ e8 Y% {. G6 M/ c
+ r, R+ M# p5 e0 s, j Q/ L( q @6 E" l/ S# m6 T1 h" r/ r% k6 m
0 N: a2 N }6 l0 `0 R
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
( K6 m1 p* t3 c) r% Z) s
7 s8 Z0 t6 q2 r8 f2 a2 j
5 _& B% ?( _ w- : w/ f% Y* L0 r- Q
6 T# p8 R+ J0 s5 A" s5 v) n- 6 d: D* k- w& }2 B8 H
- # A4 C; \9 j& `- U' i' S
" H8 P; d) c4 J- Q1 I- * K" d6 Q: f" q0 g
- : V& r9 O9 ?& H
8 C* P X4 G! m' Y1 Z9 b/ `
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()& ]5 |) I0 N9 d, }7 N4 i/ P0 C; V
代码读取后显示:
5 l' E; u6 R/ E* \3 D9 L7 P- Y
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - ' R& n8 ~# u& K% n- F
; c8 L$ T8 ~* T2 n- 5 o4 U. @: |9 n3 D- E& @8 P
- ; m6 A4 D$ ^6 c4 x' H! U" t8 ]( s# o
- ' \0 G: C Z: A% y) l9 t
- 0 a* P3 e) r; k) s! ]0 o
& C( J! D4 e( r( |2 D- + D# \9 K& R7 |0 @2 e) w
- 9 `) w8 `7 w+ E) x: A0 j
- ! E7 d4 F3 ^6 f( i% y9 `5 }
- 0 j1 k0 g+ w/ x
3 J, \9 N2 S2 M" x# k1 ^- ( D/ E8 b9 Q: @5 f$ F
- & f+ K, F2 t) o1 f# E' V
+ X6 A+ l% L4 u- J m1 o: N- 1 h; ?* ?& b4 v5 f; w, A
- 0 a: Z2 V; W+ U; |5 S# v
0 z$ m2 q4 f; n8 g8 O7 {+ A! ^, q; N
+ X( Z: o. c- c1 n, }" j% v- : e h |# G( M, m$ i) L
2 d) U1 H* b9 A# U x- " H6 g& i1 Y2 P. |$ Q" \
* H( z) x9 B, k! w
3 W0 ?* ^' @) x S/ p0 `2 r( b
, ^7 w) ]9 f: c( S' H
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(), C3 o( g! ?( D* [
所有的内置样式有(print(plt.style.available)):
' j8 A+ Y0 A) m
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - & e3 c1 }3 z: G& }# c
- * E3 Q; a! Q; F I# q: K1 K4 F
) p: l c' l) s# {* p) a( r+ }
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots() H* I; X; C0 `& b* G
2 [/ x$ w! h& [+ C如'bmh':
- S- h9 E3 B2 o& W1 `- , p* V: a9 K; g8 ^* n: N2 ~
: k4 L, Q! z; F
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()( L1 D# j; {2 j0 f/ S3 z; ]
其余的样式同理可得。
( c! `3 A' h3 m4 p, V0 K# i7 W第4步:使用Matplotlib绘制简单的散点图- ^0 W6 S2 v, l- }# t* [5 Y
- S3 Y( {; `8 {7 n9 j+ l' ]
, T/ c+ X0 P. l4 {4 m2 A) B
f- I3 j$ b5 C& B/ z7 [. _
& J9 r1 ~1 D( {& f- ; }4 J0 l; p F
5 H7 ~0 }* k; M- `. W6 u, V
" |1 B6 K! }0 d5 v& g
6 r0 {+ x5 u6 @1 W/ g9 ]
9 m# s |- X* [ U. p* H8 G/ }$ r/ h
: o+ n: \. c( c- 2 T# q$ e0 I& j' B9 X
- / w3 m, A( H' N
* a' {4 l1 ~* E& L; 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()
% B( i. L- @$ A& n5 R; g6 j 注:内置样式可以更换,这里选择的是‘fast’。
5 y( ]' q& i+ t D
|