|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
9 h0 T4 ]- O6 B0 o' W3 K6 F' J0 c2 K _+ L* N
8 ]7 }# l3 r9 a0 C9 Y( p' Q: e第一步:使用anaconda安装Matplotlib库: - % t2 M! f2 k; |% \6 ~! t, [
3 s' a9 j2 M: J. R
conda install Matplotlib
, a* T, c! L# X& e; d8 L9 J 8 M; x6 `+ o1 p( w
5 R4 J5 Q& Z- H. o0 h
2 J. _7 r) [0 d3 ]0 |$ w
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - 6 |1 m1 ^/ e8 j
4 |9 G" k, j0 l- , Z" Y8 p: `$ v- _" }
* V# Z' a }3 R8 X( r) w( G
) R9 V ?9 ~0 _8 d& y9 \$ l% F9 v- 7 s+ T7 a. V. _; p7 P5 C5 E1 J
- 4 B: ^) h" o+ ?; o2 v- i
- S* h8 ]; l/ s+ q8 n- ; F) o* j. c3 V3 r# x; w
- 0 Z5 t' ]( ~' T7 v1 c0 j
3 {/ X% C; f. j4 v7 ^3 s2 {
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()
! X7 a# ~2 U( G$ ~* B- r6 @ 代码读取后显示:
' d) L5 v; q4 J) r
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张 - + b, h4 P/ p7 L8 m
/ S3 j4 |/ c2 f6 Z( Y- 5 v% t- W* F$ J( F$ J/ \5 R
4 @' g% w, y" G& s+ ~( C. ~" D1 Y& {- ; c7 l6 m( A9 `6 \
- . \' O3 _6 I0 ^
- 1 V% O' E9 `" v3 T
. h0 o; N8 M% N2 I! j) j! g- 5 D' c8 q8 K' Z" ]* W
: F3 `9 Q+ I$ }7 D+ Z. V7 q# u4 o, G4 N
& q. J( ~( p. }& C# Q- # o" i5 @' X( o9 |4 s- N
- S' [7 d( _! o) A" W
- 9 {# {7 a9 W+ A0 A. u* Y5 {0 T7 s
- Y, K4 [& Q4 z0 B- E
. P# ]' G/ m5 f" q p
/ \8 S/ I& r T. n/ J7 t- 6 B# r9 ]; @' c+ \5 R1 F6 H
- + P+ K9 _1 Z2 Z1 \
/ w) ^/ A7 [! H6 f0 q* V- 7 |4 x8 H9 E. ^$ k$ E( j9 z
F+ R; ~. i: Z* p' d- / _7 K H5 G4 P2 D: S; R
- 5 [7 T9 D! @5 F
2 t8 S4 Y2 y! |7 X5 R& V
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()
6 }, I. Q6 d$ N j) H& S7 H 所有的内置样式有(print(plt.style.available)):
) c. n; s M* h
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
; ^) e# T6 d4 L, G! ~5 v
8 H! l( w- t. O5 ]* ~
. E2 q1 D/ M9 v3 ~+ c$ F& g5 ^) X
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
7 I) A4 N. x8 t
5 {6 O5 L* L+ d% ^
如'bmh':
7 M. O: ?2 Z" ^/ H
) O/ E, D0 z4 Q' @% k: X
/ W! P1 H! V! u E2 v' N
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
1 o, W8 b4 x0 A
其余的样式同理可得。 * |3 ]% q- u; {% M- ?1 g
第4步:使用Matplotlib绘制简单的散点图- 6 U+ a/ T0 ]# q7 f. m
0 A, m; @0 r6 q9 o5 X u1 t
6 I Z# g* y" v: E3 l- : a6 F& E3 ]' k5 @# k- ~
- 2 Z9 \% ~% P. F
5 g4 D0 d# E# O- 6 z6 E {' @; O2 w. e
\; C& `, L* k; [
* b0 L4 X$ g2 _! ~; F3 ~- S; C
' R! l, ]1 y' G8 e0 a
. Z+ T6 \! J6 n* w* ^* [- - Q7 R9 z6 b$ F
- h3 s, V7 G" B
, W2 v! Z" w( G/ u) k
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()7 R0 E6 K! X7 P+ l0 j" s
注:内置样式可以更换,这里选择的是‘fast’。
/ Q8 y1 L. [$ o7 O' X
|