|
掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。 1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。
: O) Q) Z$ s2 b& t, Y% E/ \* O- M& t6 b/ e; @/ C
y* x; R- Z& @" ]6 q& I+ Q7 k第一步:使用anaconda安装Matplotlib库: - : L# i ? Z* A
2 ^( g4 g0 s! Y* \* [
conda install Matplotlib% g, w _$ j. ~+ |& E- t
, Z, n$ U# Z' n$ Q3 s" [) P2 l f; z! e; b, Z! h, ]8 N4 |
6 Y; n: S- n" b; S7 R) }9 X. a
第2步:绘制折线图 subplots()可以在一张图片中绘制一个或多个图表 fig表示整张图片,ax表示图片中的各个图表 plot()根据给定的数据以有意义的方式绘制图表 只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0 同时提供输入和输出ax.plot(input_values, squares) - ( \) o9 U; q" o7 b* f/ `$ p- ?
! {$ N! t4 L( A) Y- & y3 e- `$ K/ I% X( ?6 r
: c- Y6 W: R l' q! b) t+ I
. K+ x. n( J0 @; b3 |1 d- , T- j8 s# i P1 i1 U+ E
% ]. J, d. `' Y( z% w
5 i& `& h- _( D2 A2 u) }- 4 z) a: O4 B% V; N" f
: i9 b7 q# Y& c7 v# w" p
8 m% A J2 s6 a; k
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()1 R R9 H& [3 x5 l
代码读取后显示:
4 x' H* q6 B. C( a1 U
第3步:使用内置的不同图形样式绘制折线图 1、显示所有的不同样式的图形,共有20张
. S( R! j& Y; o" W- " k; ]5 H% _( j5 y/ d" b
- v. B- ?+ }: K* s
# q6 v! X3 S7 z) ^5 U& |
, f! k( t( i$ R+ E+ C8 |- + t! V* |/ R; @. H; C1 k% ~. [
3 w+ \# j0 `) n- ) S0 v# Z* B) j
- R9 `9 J u m: Z
, Y7 N' M1 P4 ~1 _& A+ |: s/ \' r
c H( h( ~) {
* m( i. E1 U0 z7 b1 A
& n4 I" T% e8 A- I& O7 w3 J
" G2 P( ]3 L! i+ |
9 Z. M) [ w' o+ |2 {
& b6 L! R P4 g6 L2 T- 9 |( p: W9 }* @
7 x' h& ^6 {9 K# b- p) H- 9 N$ Q( ]: H; k3 s6 Y
1 P* b W5 H4 ^" l
8 X/ R6 S# O( O: l- E
6 G# E+ v( I, c$ G _- 6 ?+ i2 N; E% {5 Q
- ( Z! T( R2 t( C7 d) `. \
8 Z( O; B; N8 n, l( C
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()3 K" ^, n* Z' x- V5 E
所有的内置样式有(print(plt.style.available)):
1 s, ?5 H( P1 I. J3 u% M
2、选择其中一种样式(plt.style.use(‘样式名字’)): 如'Solarize_Light2':
* L( e- d9 h0 Q1 A6 d7 J- 4 C: C) G0 L# T1 R# w! p( e
+ d- Z6 o9 z/ d8 Z
plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
1 }7 S( O, c" g% |1 w" g
3 F0 t- f: Q0 p( F0 T( L如'bmh':
; y7 b, u" Q1 G: V, }9 z) d
3 A: t0 T6 o6 x& `4 Y5 i; ?1 Z3 Q' s& ~& G
plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
% v6 M5 }; g. V, ?: Y& r& y, @
其余的样式同理可得。
; h9 J/ x* t" L3 b0 X第4步:使用Matplotlib绘制简单的散点图- ) P4 m, o6 K( v1 v- H
- - T- m) u) |+ O
; M( F, X9 T/ Q
5 X: h+ g1 P# u- ) g* M7 p. a! ]% L3 F& C: z
- - \1 K) u+ E+ j' r0 ?3 E) p
4 z9 C! x) @* P/ p n) y% K
3 N( x. ^3 z7 V4 _$ v: A* i- 1 B Q# }$ Q. C- Z) g6 S- ~
, D# ?7 B4 A( i1 p. Z8 c! W3 x- 8 w2 I) |9 k3 g2 ^( q
- 5 |* i7 v' V6 U' O
- * J) a: z2 ]5 {% T, H" d
, Q( g1 \# N$ d: ]- T. B- h# M
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 t4 T! R$ p" e1 H, B& ]% Y( e 注:内置样式可以更换,这里选择的是‘fast’。
0 m4 y! K# e/ g/ h
|