|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
5 ~; S9 U* e+ J1 C% w
" y; J* \4 c* G) \) B6 W5 y; }- K( ]$ C) }+ ~3 Q
第一步:使用anaconda安装Matplotlib库:
6 g- \! Y/ V# |( f) O* e- k
' N* \; }; U# o4 f Z9 ~: h
conda install Matplotlib: t) o' c! G S( M& v
3 @5 t; Z9 E2 M& U% W, W8 ?, d g7 h4 {/ i$ z+ O# E
1 @3 y5 N" [' d( |$ i% {
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares)
9 s k( M3 C: T- " _( U r9 Y$ T2 T7 p. Z/ Z# q
! l! g* }8 W" N a" X- ! I6 L9 R N7 S, n
2 Q0 S @" o5 t% V; V7 a- . F# E9 m' C) b; y0 ~1 v! h) O
! _: O* t; w7 m0 X, `, s% E1 X
5 P( j& h, ?* E- / M# t- | S& a2 T
0 Q: u' Z4 }1 @
% }0 ^1 k9 J% k$ i% @
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()
: i1 M+ k5 l* { 代码读取后显示:
0 e5 h( l, [" y3 Q/ q! Q
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - 8 F/ }. z: w6 n: q- ~8 P1 |
& \; ^* x# r" e( A
3 y5 C+ J4 N% F5 G
/ ^( H1 b0 C0 X# u' Q5 n A, X6 a/ y- & G6 K+ ^: c9 Z5 B8 I! ?2 n
- # L+ p- i" m$ `" f: R; l
# @+ U2 K3 m+ E! V8 G- / x* z9 J% k( @3 k; r1 R
, f6 S1 |. p: I! x3 S0 l$ _5 R- 3 `- {6 d: r8 T3 Z, L; a8 d
- % F1 R9 v m) H( _; C8 f
- % o, }2 p2 [. t: n7 @& [
- 2 t+ p# U3 x! u n
- % {2 O6 ^ L: N
8 k0 f! d9 H( a" P# [9 }- ' A% n) f$ p+ t! C6 L
- & D+ G4 ~* m' z& e
- 8 [) y, j9 Y4 _& E5 ?- O- m
: ]0 f, f. v' o' ~* @/ u* p- 4 g! y6 N9 m7 r: V$ F
- ' E3 u; N( D: r( }% H
- X2 i" R6 v# O; c) z2 G; v& k# L
) s; L6 V7 ^* f% b3 e0 I- $ q$ |8 A0 H& q
8 D5 v* x' Q0 _( k& f% N
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()/ T1 A y, F, U! `; M% @
所有的内置样式有(print(plt.style.available)):
6 m9 N, c+ V6 A7 @: F5 p" i
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
9 V. ~: D4 v) C T1 P
0 X$ h3 L4 C! O$ w- N/ {/ K( X
7 g: @ Z3 @* ]3 y9 |) c
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
, U! v- S, V) i: b' G Y
. @( Y, ]7 N: R0 j' ?
如'bmh': - ' \7 F8 E' T: K7 n4 u; V6 k
! U% I$ O: V9 e+ d
4 p; C# L4 C- \2 ?; s
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots(). ?$ Y7 B' E" ~8 h! B5 d
其余的样式同理可得。
- J5 J3 C, h* q" `: e, S第4步:使用Matplotlib绘制简单的散点图- 3 _9 X) ]/ S9 Q$ j
4 s2 D2 Y2 X5 l& x" s9 q
2 O/ o! F; G: ~5 p ?9 @0 Z. x. |- 1 e+ G: y, R3 _; p. Q% x
. H. h7 k0 x5 k) W- ; ?& ^) n- i, `* e; A' G& U% p
- ' K" o" ]7 T7 l4 m4 R- G
- 9 ]0 \ U1 E" Y' `3 f2 t
- 6 ]+ K1 u$ }: x; T9 ~. E
& l0 l5 b$ t) R2 v5 y0 v. }- & h( M @% ?, q+ u" u% H5 B
- : W, O/ h& F. }
- * R# y3 }. x5 ~% i& S) w
! W7 U" k: i, s3 Y1 q" `
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()( I" @+ L; m+ @4 p
注:内置样式可以更换,这里选择的是‘fast’。
% x* ^2 R9 @* }2 c7 X' B2 X
|