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

[复制链接]

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

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

6c25f72746ee53e9535e73a42eb7c456.png


$ o; c7 G5 ?6 a6 f7 z% {9 r, c0 p2 J+ x

4 f6 ?) w8 m$ u

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

  • % ]. I. U4 g! w
    9 q. g6 W+ B' R2 n" n3 Z2 K

conda install Matplotlib0 b; \5 g$ ?* k, _7 G5 M: S

1 {8 ~' [' t9 N: z
$ }+ _  l- s0 h0 ^1 J
# m/ t" }' v6 o- u7 C

第2步:绘制折线图

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

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

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

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

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


  • 2 Q0 ?, n: v% Z* x$ g
  • 1 P2 C9 s, r* i9 X- k6 r! H/ S
  •   ]6 e2 V: s* Y( ?7 }
  • # C- m4 Z) W0 K+ J' s

  • / I9 h) x6 P% P0 W/ O

  • , X; i5 q6 ^9 _" u2 `
  • 4 Q) n$ Q$ }! _: u0 \6 }8 Z
  • ! T" b8 a8 Y7 K# T( _
  • 7 c: A% S$ W  Z- b1 o) V
  • & u$ ^* p( l  t& X8 o6 d
    , j5 d1 h6 V% i$ k" m0 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)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()2 \: R" S$ ~3 o! ]" M6 S

代码读取后显示:

4cb0033425d1e6233c1ff8177f04baf7.png


( q. e" Z- {; f" `7 f

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

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


  • % ?5 O* x& x" m2 x5 d! t* h* c5 w

  •   ^2 K) A$ e9 L8 P+ G

  • - `  f6 S! Y  R# n$ O8 e7 ]

  • % \" K  j6 a3 {3 @' P# @5 @

  • 8 `0 ^$ x5 z5 a; Q: o& S2 ?: y
  • " C( c9 b" H4 D  B

  • , ^& g! g% I: o; h. p8 J) n

  • - O$ M" W2 ]6 h7 b6 }
  • / i3 P% P- U: g# v& @; a- M

  • 6 N9 C$ M& x% L, S" N

  •   u; \; M3 S: @8 v. g  g, B
  • , f, f9 D  A+ B, N: ~5 P# V

  • + s/ G% E% h. K/ Z
  • 7 ^2 b; W" v& M5 x1 V+ y

  • 4 z6 U) x5 l8 c

  • 4 p4 _4 c6 @  b# b- V9 j' [, W; u

  • + E. R+ ^% z6 y5 j* }

  • . F5 a' S0 Y( c6 R

  • 2 ?1 t$ X6 x1 c8 {, |. j; [

  • ' C# u2 F+ c- c+ }6 T9 z4 S( q
  • - M& Z% x  I& }0 b
  • ' u% }# N" D  e) y
  • ( c+ p% Q! _( J" F9 Y5 Y: S
  •   y! O( R8 e/ J
    7 ]- X4 z  w$ |6 P$ t& a

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()! I. @& q* G* b: I( }$ b! I

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

d30e10d4b7d3d13cbb050ee8986765bb.png


; T; H( r* k1 l! h9 U; u) y  I

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

如'Solarize_Light2':

  • 3 B6 f4 j) P- w; S6 k
  •   n: L! L3 v. B8 R
    ! G8 t7 I- x' Y

plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
2 p# l# ^  w) ^6 _0 c  I

9a17c4018c1e024a1157ea1211dd7280.png

2 L7 \- b* n5 _8 D- X5 K

如'bmh':


  • * _# ]) o* h2 J/ p

  • 1 p" |! ]3 d+ n9 `; Y+ i
    " P- S8 t) x; s* |& P

plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()
. E3 q$ y8 Y" U. `2 V% L

56ebedcc5ca84f69b09178876ebf0b03.png

其余的样式同理可得。


% j8 l4 O8 l3 m) w/ |2 `6 l第4步:使用Matplotlib绘制简单的散点图

  • $ N& v! {2 E" Z* V+ k% q

  • - K4 ~$ x$ {3 V# C

  • ! Q1 i* j$ Q1 u$ W* [/ }/ Q" i2 t. O+ Y
  • 8 F0 \+ a# t) `7 J# J" Y/ I

  • . K" s( u% k7 D& i
  • ' R6 K( H1 D) z9 y/ N2 b) M

  • 0 `1 p, u+ ?  F" h

  • $ \3 [( N$ n& S2 Z( d' E0 j: |

  • ) n, b' P. [- `& ]

  • * I- s" Q; Z* K* G0 O# S7 w4 E4 f
  • * K! I2 @+ G( r9 L- }. D, _
  • ; c2 a+ W- ]: h5 j( M

  • 8 T% S* u2 P6 v* h
    : [0 |5 f1 \9 p+ n

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()  B4 T& v9 `" O2 ]4 g5 l& X

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

32efcf65a4d194fc66072d2c903297f6.png

- j$ P+ }6 ^) g3 l% D/ V
回复

举报 使用道具

相关帖子

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