|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
0 A* p1 l% h- V4 y. T
" b1 f/ ^3 p$ q7 n6 m
1 C/ G% R: [$ i第一步:使用anaconda安装Matplotlib库: - 6 s! }' U% g8 n. T1 B7 E7 W G) Y6 |
$ y! e" S" l# R8 B6 C
conda install Matplotlib/ T' W; M+ _. G1 A5 }- {$ G. W0 H
" \' Z. F5 X+ [. K, Z
7 b) Y, l4 b/ o5 D
2 |/ [* @9 X, { O& `2 p5 h. [第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - ' e( g4 V( Z: h. \5 ?8 ~
- 5 ~( |0 H6 Y: D; L
- 2 K" W: i6 y* h0 u$ y
- , ~; O! I2 F9 B% C; {# s
- ) v* `0 d! K8 E" A+ }2 s d
- " }/ p- J) s4 e" C4 c& D% g
# \! u% w e4 O2 Z9 P
* _, s+ j& t" W' b$ B. w6 A
' g( H, h7 F5 \+ G' |/ T" A! K( m- ' I! e! D7 Q, J/ J1 H; }- g
8 K, A3 A9 \0 w
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()" M$ m2 P+ S* p' b: c
代码读取后显示:
. h6 C. ]) d, W$ {& G
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
7 C9 T- g! \. H$ R
; x- s# V/ |( d- - @( f6 c7 D- }. V( U5 t* h6 d) x
- 2 M" e2 R$ I R; N% g% c# _
- - k+ F0 }( x# n2 W- x
- 0 m8 i/ m* g- d* Q, R
- 8 r" i- S% n$ w) y0 T, |
- ) Q& g6 y. ?: {+ s
- / O; a, S3 B9 |2 I8 V& h3 e6 S- I+ d9 F
- ( a8 S' r, H+ U# q
- ! i/ y8 |# c$ B# }! ?3 g
5 \! i/ |- r- T. ?6 F
+ b- y [0 _3 s, Y" E
/ V8 N9 n+ \9 H0 C" `* k5 j
# a& @" k4 i, U& Y" @/ {6 m- 8 c6 ~$ }: \4 \6 b- \# \! B& U4 {
0 s! ]1 R5 {9 {0 w
J3 L! O$ `' J+ w- I- ! _# L" l$ U I: k( f5 H
) Y4 q+ w% E5 h- j
6 n# a; {6 ~' d& V, Y% h. U3 ~
0 U4 x7 A3 r2 e+ r/ J$ z- " D6 @6 n# N# _& s+ g/ M
6 U6 d. s6 G6 B; \- n
# b; }2 D, P9 Z9 O$ E' H# 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)# 使用内置样式画图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()
* c7 X* x5 N: c/ ? 所有的内置样式有(print(plt.style.available)):
0 w$ J1 Q* B% K& ?; L" M
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2': - 3 j- d M! _: f' N2 n( }
- % E( }0 l! D; I; m6 x% A! Q
$ ~: H+ Q+ S# w
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()( K- J; R, Q4 q+ b- ^+ w
/ ~7 _) S3 u; e2 W. I; N7 j% b% I9 A
如'bmh': - & ?7 g# [1 K" y4 y3 ~
5 }; G9 y3 U* @; x* ~+ n: z5 S* i- T3 W& t& k
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()# L! K3 R2 Y# [2 Y2 a
其余的样式同理可得。
7 s( s* ?3 ]: b" q7 N8 K5 B第4步:使用Matplotlib绘制简单的散点图- * ]$ Y5 Y# c- N4 ?7 ?5 y1 R
7 B! _, }2 a8 K2 K r& X- 0 J# W4 g& P. R; i; w
- # y6 y' f) d! F! Q1 H
1 J5 i% t. N% i# ]5 I5 j
7 L2 t" k( o: D& J- 5 o9 }% F: s. k. p6 L
7 n) x: D" Q. `' L" z9 g- E
/ Y# y+ o. D! u- C- J& y- $ Z8 q3 s4 w& u- s% V& D
- 8 K( h$ {& Z4 ~
- 3 y* p7 s7 A- E1 V; v) a3 t
8 {7 }4 |5 f: b) K4 m# D4 w: V2 V7 V6 Z
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()
: F' {: b! F( {) Z( p0 k; E 注:内置样式可以更换,这里选择的是‘fast’。
1 J: {0 a1 C, k |