|
气象数据可视化主要依靠matplotlib做绘图,其他库包为辅助,包括数据处理的,地理信息处理的等等。 绘图主要有六步(“六部曲”)(不是绝对的方法,视具体情况使用): 引入库包:import matplotlib.pyplot as plt 设定画布:fig=plt.figure() 导入数据:之前已有介绍导入nc文件格式数据(【气候软件】Python读取气象数据 NetCDF文件(***.nc))和导入txt文本格式数据(【气候软件】Python2:读取TXT文本格式的数据)。 线图命令:plt.plot(x,y,lw=,ls=,c=,alpha=) 出图:plt.show() 存图:fig.savefig("···") ! F7 I* G0 t) K: C, P
只要按照以上六步,基本绘图没有问题!!!
+ m; \7 v# s& g( Z+ d$ f+ f
* R) D1 Q4 f& q0 `! Y; V! x& z5 ?6 G绘制简单气象数据图 - . y2 A4 M# ?/ X" Y5 H) }
- * W8 q; }+ M! P) `/ l" d
1 \: J0 @+ z! H7 C9 Z& F# j
% D# s/ M2 n, p1 b& U! a
: ~) S2 t% m; I2 `( s0 C- 4 x4 h$ s* m$ @5 k; }7 L
- 7 q: ^+ `! b0 ]' f: s% {5 T( }9 A
- 2 u( y, F; B0 J
+ O: Z5 y: z9 ~% q6 F- ! l8 k) d! y& M& M0 ?
0 g' h0 C, r; @
8 q8 F0 v6 X5 A6 L+ M" q- 9 ]- U8 F. @8 Z9 Y6 a# l' @( b
- ) O8 C7 T1 i9 D
0 g3 f) F8 T+ i" ^
######1引入库包############import matplotlib.pyplot as pltimport numpy as np######2画布############fig=plt.figure(figsize=(3,3),dpi=200)######3虚拟数据########x=np.linspace(0.05,10,1000)y=np.cos(x)#####4线图命令#############plt.plot(x,y,ls="-",lw="2")#####5出图#############plt.show()########6保存图片###########fig.savefig("画布")
# A8 e: c, D4 Y8 Z* E
备注:创建了一张画布,figure里有两个常用语句,一个是figsize=(),一个是dpi. figsize=(a,b)语句里面有两个参数,分别设置画布的长和宽,单位为英寸。 dpi为图像解析度,过低的话会使图像模糊不清。 基础的常用绘图命令有折线图(plot)、柱状图(bar)、条形图(barh)、散点图(scatter)、直方图(hist)、饼图(pie)、箱线图(boxplot)等等。
这里的数据引用的【气候软件】Python5:绘制气象资料的多区域子图里的某地1979-2019年平均气温为例绘制两个站点气温时间序列。
2 R. V3 y; W8 j7 T3 x- * o( I8 |( D4 Y, ^
- 0 D5 v3 j+ J' h* r
, H3 e7 C( r0 Y G6 a- 3 n# {2 }) \0 l/ H8 Y8 p2 B
- ' g$ H% o# n9 t( \
- - E2 O7 m! c2 |) a# p
- ; t: V' j. j5 L' r0 f5 n
- 8 h, S/ l% Y9 D$ i, U0 B
- ) s7 z% n4 p7 t/ C/ `" y$ z
- # i. x% w( _) O
' i: p, P( |, r- 9 e4 `( s% j' s7 J
4 H+ e- r7 g! g: h2 y- p- U G- 7 y, X# s r' q# i1 [
- & \; c0 `3 h" j
- 6 F( V; o. \8 C5 @# \& C
f+ t! `; C7 F7 h
9 i: L2 Y) [2 Z3 d1 L- m- : e0 Z% T/ `3 ~+ t" U0 Z5 r
3 B/ U5 _% \3 g. [5 W6 Y, w$ [/ O- 2 B5 }6 M' t- b. V/ o
; R" O6 T) t, d8 o+ O
3 k) V) W0 B K8 V
######引入库包############importmatplotlib.pyplot aspltimportnumpy asnpimportpandas aspd######虚拟数据########data = pd.read_csv("annual tem.txt", skiprows=1, sep='\s+', header=None, names=['year', 'sta1', 'sta2'])print(data)x = data.yeary1 = data.sta1y2 = data.sta2######画布############fig = plt.figure(figsize=(10,15),dpi=200)######划分子区域############ax1, ax2 = fig.subplots(2, 2) #####画图#############ax1[0].plot(x, y1, ls='-', lw='2', c='k', alpha=0.5) #折线图ax1[1].bar(x, y1, ls='--', lw='2', alpha=0.5) #柱状图ax2[0].barh(x, y1, ls='-.', lw='2', alpha=0.5) #条形图ax1[0].set_title("plot")ax1[1].set_title("bar")ax2[0].set_title("barh")plt.show()- b6 Y L/ j, S8 _
! h4 Y' {& P2 O3 } }8 z: c
2 U, f8 }8 G1 ?- V% j* x+ L( ~2 `; l+ H 登录/注册后可看大图 0 u9 o# z# s* d" C3 W9 j
例:线条样式不同:
- 6 |! r: H; R$ U4 ?! Q& I
- 8 D1 P0 j5 h1 N# r d
5 ?1 D& y" E' k% h% A j
6 V8 Q9 S. K0 G/ |
. a/ d1 P" [2 u: i$ i* d7 \4 o" j# d
+ Q9 ?) w G$ @& Q; |/ B U+ O; T- 3 U& [& _' ~0 @( l% V. D
! | C1 F6 n- l! w1 ]- 2 [7 x! Q- M$ I6 L' M i# K
. i4 m& u; S: m8 ^
# L; Z: a! E4 {6 ?& g
0 a4 W5 H8 ~9 t, A9 i! ?
" ^" P& N4 ]* I1 p; m; i! F
$ ^: c% M. ?( e/ S: K: Q
. O; x z, M$ [ x% `. p( q. S- 8 |# M- j! H. N
7 ?% \2 s5 s4 P
2 N8 ~1 Q- O2 S% O+ n J1 C3 c- / t+ H& |1 b/ k3 A- U* v; u
- , A( |8 E. {5 J8 M
2 d, @- g0 ~ ]8 n2 g/ W- $ W* y7 |8 ^/ h4 R1 E3 _$ l; t0 ^
7 \ P) a' Z7 R' G8 L! z
######引入库包############import matplotlib.pyplot as pltimport numpy as npimport pandas as pd######虚拟数据########x = np.linspace(0.05,10,100)y = np.sin(x)######画布############fig = plt.figure(figsize=(10,15),dpi=200)######划分子区域############ax1, ax2 = fig.subplots(2, 2) #####线条命令#############ax1[0].plot(x, y, ls='-', lw='2', c='k', alpha=0.5) #线条为实线ax1[1].plot(x, y, ls='--', lw='2', c='k', alpha=0.5) #线条为虚线ax2[0].plot(x, y, ls='-.', lw='2', c='k', alpha=0.5) #线条为虚线ax2[1].plot(x, y, ls=':', lw='2', c='k', alpha=0.5) #线条为虚线ax1[0].set_title("ls='-'")ax1[1].set_title("ls='--")ax2[0].set_title("ls='-.'")ax2[1].set_title("ls=':'")#####出图#############plt.show()
+ g" l- Q. S' s; j
' n# [ O, Y! f( i- ]
0 c I! T: x- L4 R; o例:线条的透明度和颜色不同:
- " ^7 y! `( a/ A$ \1 h$ s2 e4 g
( A# r* k9 i) Z4 @
! Q; c0 c1 @ n# `$ O% U
b) U" Q+ K: ~8 x( {. V- 7 N& U5 \4 L5 c/ p) Q4 ?
m3 I `+ R T$ ^& ^+ W3 X/ f- $ z5 a$ c l: y# n& s$ S
- 6 X2 ?* m( g- z1 n! v% Z' e3 H
: Z0 x3 R" w, E$ }# @0 {6 F- D- % F0 w+ h, @7 b; v
% V3 m* L$ K: |4 g8 s
/ p. u8 n$ h" i, @7 U5 w
/ ]) ?7 @; j- l
( B6 _, E2 s4 z0 o5 Q- 2 B6 p |" i* Y. b( V* ^
, I- x/ s: M' x! @! K
2 J3 `, }. @. H$ G9 \- $ V& _! ?. Y# q, D
- ! z, c( X* K0 b- o7 b5 F
- . {$ I; w, A0 |. }, o
' A3 V( g h3 D# Z6 p: T2 d3 d( ]) W
######引入库包############importmatplotlib.pyplot aspltimportnumpy asnpimportpandas aspd######虚拟数据########data = pd.read_csv("annual tem.txt", skiprows=1, sep='\s+', header=None, names=['year', 'sta1', 'sta2'])print(data)x = data.yeary1 = data.sta1y2 = data.sta2######画布############fig = plt.figure(figsize=(10,15),dpi=200)######划分子区域############ax1 = fig.subplots(1, 2) #####画图#############ax1[0].plot(x, y2, ls='-', lw='2', c='k', alpha=0.5) ax1[1].plot(x, y2, ls='-', lw='2', c='b', alpha=0.1) ax1[0].set_title("alpha=0.5")ax1[1].set_title("alpha=0.1")plt.show() |