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

[复制链接]

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

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

6c25f72746ee53e9535e73a42eb7c456.png

: d2 P* L2 m) p9 M# i
; y. N- k7 w4 ~+ W( T
; d9 I- ~0 x7 O6 f9 O

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


  • % s+ R: T9 {5 P( |1 [$ q5 m) `' n5 N, [, o' C

conda install Matplotlib" p) o' g/ }# p/ F6 N/ e% x* Y

3 W1 I8 l: o$ H2 N4 c) Q
1 f8 @4 {) P5 R, B
& i# U$ o. ]7 w$ d4 {

第2步:绘制折线图

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

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

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

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

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


  • * ]8 |1 X* P! ]/ G
  • 9 N* t; X" ~2 [7 ]
  • * h7 O+ Y* K4 W7 ^6 L; F5 {4 u
  • ; R6 s; e" A2 K* ^1 x

  • : x+ S: S' q& Z* ]1 c. |

  • 5 d3 O9 l4 \  B, k- W2 |2 g

  • 3 a, X- ^( u" R0 t

  • / y. \+ ]# S" E& M

  • 8 \! W& n, ]. z9 m4 {

  • " I4 O7 H; s/ F4 z: R* B% C' J- c: ]) o% s- W' z

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()
5 @- F; U& \9 V) p' x

代码读取后显示:

4cb0033425d1e6233c1ff8177f04baf7.png

; p1 K+ Y/ r0 l- b7 o% W1 g6 E

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

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


  • % a. @0 \! E" N# U: r; v
  • " Z  C$ _' A" T% B) H
  • ( e( f( Q2 \7 ?( |) c
  • 3 Y+ a% {3 X# J6 Z! w7 M# x& F

  • % C+ Z0 @, q2 E4 Q# n
  • # C* Y0 u0 j5 p+ a+ c5 X
  • . u/ ]; f5 S" ?3 {! Y5 c5 \
  • ; p2 U9 n/ i3 v. D6 T- R

  • ( J% C$ q( d) `: B; D

  • 7 e% j, Y& ]# H: m- n  V

  • % N( k) |: L+ p2 P) R" J3 r

  • 1 D0 i0 }; ~8 C/ G* ]5 o+ f7 I  M
  • / ]" L, f( R3 d1 Y3 ]  y6 x5 N4 O
  • 0 \! h9 B. c/ L

  • : s" f/ d1 L5 V8 o

  • 1 y! a2 o: i+ L3 m4 s3 ~) M) q
  • ; N$ t% B2 f6 o( {7 t+ U' Y3 i

  • 3 ~% A, P7 i) }  L
  • 3 q4 v6 S8 p9 @0 F# S/ ]

  • 1 p  v; {: ]. q) ]

  • 4 s0 q8 V2 I/ A
  • 4 k& Q' o+ S) ~8 [

  • : e6 S0 q6 r1 b
  • - N6 f0 x' V* W# U; Y( n( U
    7 G( W; |9 s8 z& p, A- Z1 w) 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)# 使用内置样式画图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()8 f5 ^- ], Y* A$ O6 k

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

d30e10d4b7d3d13cbb050ee8986765bb.png

7 [% x- y3 T) N5 N- M5 b5 b3 A& c' y

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

如'Solarize_Light2':

  • " ~) k) _# D4 k$ h' p0 j4 T$ c

  • % }" K' {+ B' e+ u. i: @; h( R5 g7 G: P2 h7 }4 ?

plt.style.use('seaborn') #使用内置样式fig, ax = plt.subplots()
8 A- ^: _3 q6 l' \& Q7 Y8 U

9a17c4018c1e024a1157ea1211dd7280.png


- g# Q/ x( ]7 A) ?

如'bmh':


  • % m- L& g# `' N# d0 e: o
  • ! k, ]. e" v5 k

    1 N' _7 }: s! E* J1 J; q: ]

plt.style.use('bmh') #使用内置样式fig, ax = plt.subplots()7 G+ v1 u! w" U

56ebedcc5ca84f69b09178876ebf0b03.png

其余的样式同理可得。

9 H' \2 H- v1 C0 t" f, g
第4步:使用Matplotlib绘制简单的散点图

  • " W% E3 M) K; z4 Z4 w$ h, ]6 S3 \, r

  • 5 i* z# N7 u1 P7 N1 E- M6 c$ }2 v
  • * ]  p! {$ s) @) f+ S: s$ z2 g% e) o
  • * y3 f# j" j$ ?4 @1 V1 z; g
  • # x# b, U! W+ s6 V* D* y4 x

  • . H+ C0 G" Y5 Q% t( a

  • / ^& V. r! y3 K$ q1 |8 k

  • . {3 I2 s" }3 N) Z$ M+ _/ }
  • # Z  V: A2 [: d6 m& S1 l
  • 6 j. ?; J6 n# w$ b! k: Q5 L( c  ^

  • - S; Q6 Y4 y3 q+ v

  • ! S% @( V8 l: d/ a( ~$ z/ a
  • , g. e0 _- q  ~/ L9 z5 V+ s  s; I% `
    1 I- _; Z) w' A- L, w1 j1 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()
' a# r* g- z9 g  D) Z$ Y3 Z% H: e

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

32efcf65a4d194fc66072d2c903297f6.png


; |& _  F- l3 _( V" D! S
回复

举报 使用道具

相关帖子

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