收藏本站 劰载中...网站公告 | 吾爱海洋论坛交流QQ群:835383472

[Python] 【气候软件】Python3:数据可视化绘图(折线图,散点图)

[复制链接]

掌握了数据读取的方法后,就需要将数据可视化,进行绘制相应的图形了。

1. 使用Matplotlib绘制简单的折线图:以一组1981-1990年的某地月平均气温数据为例(数据信息如下图所示)。

6c25f72746ee53e9535e73a42eb7c456.png

1 p/ I( k& s% A, N1 E4 b
0 @% P! ?4 F6 F+ h$ }: }
5 z6 c, p& r% B, D$ J3 s

第一步:使用anaconda安装Matplotlib库:


  • 1 n& c4 h$ f0 w$ `/ _0 \% D6 \( l" N1 Z5 O% D9 H; M8 f/ p

conda install Matplotlib
& }/ f& \: d3 e+ \% L5 b# J

! ?6 t9 V% Z% {4 g4 `

$ A. J! D/ x3 Y$ k$ \; b- w
- L% I5 A4 v. _

第2步:绘制折线图

subplots()可以在一张图片中绘制一个或多个图表

fig表示整张图片,ax表示图片中的各个图表

plot()根据给定的数据以有意义的方式绘制图表

只输入一个列表时ax.plot(squares),假设第一个数据点对应x坐标为0

同时提供输入和输出ax.plot(input_values, squares)


  • % i) x5 m9 T7 C& T* i( p4 B+ B

  • ) I) J3 Q' Z  [5 S9 [2 o9 l9 [
  • % a; m1 C7 N/ Z
  • * B- f% Q6 U) I) w

  • 4 `  M: k) D: }- l/ I2 H
  • ( g. k9 s+ X6 b3 l

  • 5 y' m) M( L2 S: m' r

  •   B7 n& G" T1 X$ |
  • 5 t, t6 d/ V9 }% @- I0 q, P
  • # w4 d/ S  E, q9 W9 A
    2 ]- Y0 Y' a% P* O

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()$ s$ _! N9 T. ~( ^- [

代码读取后显示:

4cb0033425d1e6233c1ff8177f04baf7.png

' P; b  B/ A" n2 _0 W4 C

第3步:使用内置的不同图形样式绘制折线图

1、显示所有的不同样式的图形,共有20张

  • 2 \; N7 g& h8 d  J* g. p& f; V6 X

  • 9 u& A4 [$ @5 I7 g% N

  • 5 W6 s* h5 s. N/ [5 r9 X5 [3 e! d
  •   b: R: f$ `! S
  • 9 T0 m# ~9 _/ k3 R+ V# c
  • 2 a( k4 `& K. g3 ~# n  |
  • 1 G( d4 p+ W$ h
  • ( g2 }) M/ C9 ?/ X+ E! u
  • 4 `3 P  x5 \) h. [; Y. A

  • 5 P) @. O3 ]. r# E* S, ?% O

  • ) ^9 F# o/ a% J& b* N4 Q) L% ]2 p% p

  • ! _9 s7 D3 L' n" `
  • : b- c2 e. P) D9 D, g
  • . V; I3 G: s% J( C
  • 3 E! E+ I1 v- h* E
  • ! r: N  `) W% O0 h/ \& F* f2 M  h
  • 1 N/ f( q, g2 b% C5 \! G" Q3 e0 u

  •   _# r4 k) ]9 J4 t* d( X- H
  • % D" @6 J; d1 H9 ~
  • + h8 D2 z/ a, F. u

  • 4 R1 i* N2 z! x8 R& I6 t" w

  • 0 ?9 i3 N3 e# ?1 ?# Y' t' x& J

  • 8 g: u: s+ Y! Y" x! z8 ?" O2 ^. b

  • 4 a0 ^$ Q+ k5 X3 X7 l6 B
    , J' V9 \8 @+ e6 b/ c3 |3 ~" 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()3 J' M7 r2 m9 g8 t( `

所有的内置样式有(print(plt.style.available)):

d30e10d4b7d3d13cbb050ee8986765bb.png

) d# V6 P/ m6 ~% G! }# g; ]

2、选择其中一种样式(plt.style.use(‘样式名字’)):

如'Solarize_Light2':

  • 8 ?9 E1 ?) u8 N2 q3 o  R0 ?
  • + h0 B* G# w# ?6 H4 m; j
    5 L" Y. v( H/ _4 G$ x

plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()7 R8 q) n; O4 J

9a17c4018c1e024a1157ea1211dd7280.png


4 k* }, Q/ |  o1 V; {

如'bmh':


  • 6 q0 Z- `9 i( \. N! d

  • 2 C2 ]4 q* @& Q# y% J1 @: G( i+ ^1 {" \! z5 k8 a) M" A

plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
3 A! h( x& \+ y& F* s

56ebedcc5ca84f69b09178876ebf0b03.png

其余的样式同理可得。

4 l8 B0 `1 j7 ~6 R# o" g
第4步:使用Matplotlib绘制简单的散点图

  • 5 E8 }5 A5 g# Q4 P) D
  • " K( g$ P$ F  n0 I5 J5 c
  • - t3 O7 |1 V9 A1 ]: t1 E5 s
  • ) K% j8 t( B. l* D3 U" X
  • 0 W! p: O- B; q& S; D: I) r

  •   R8 C% h. h: {

  • $ o$ H6 C! M3 ^$ U5 K

  • 4 n' T- o& ~* `6 }. V
  • * w' x! f! g1 l5 x
  • % h$ u% q: k) L4 p3 v* ^

  • 7 D* i! K7 ^' a2 Z& d1 P1 Q8 A

  • 6 x# G+ o) C8 j) `7 u

  • 5 n+ v3 o& L9 R. A( G" I9 W* L6 j) A. ?' R1 |/ ~6 o; ]8 t

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()
& V1 y, e0 I' v3 [1 a- c4 e( M

注:内置样式可以更换,这里选择的是‘fast’。

32efcf65a4d194fc66072d2c903297f6.png

' T* @0 a0 q2 Z; ]. g" e
回复

举报 使用道具

相关帖子

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
有风
活跃在2022-10-29
快速回复 返回顶部 返回列表