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

数据可视化之美 -- 以Matlab、Python为工具

[复制链接]
5 G8 J L2 D& I
& F4 w8 ]: G$ v8 z9 }- v/ [/ {2 D
$ P0 |3 ^4 X. i: k g: ?- \
! i2 R; _( E1 c5 P d

在我们科研、工作中,将数据完美展现出来尤为重要。

- ^1 ]- a7 e0 Q1 ?! [

数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。

) M8 O w7 X* t$ |" v- W& u

下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。

! ^* H* P9 B( a# C, o

Example 1 :散点图、密度图(Python)

0 c4 S2 P* L* Z3 ]4 F4 i* K

import numpy as np

* @; U1 j8 X/ z! ^- m" }: f6 Y

import matplotlib.pyplot as plt

8 |" [+ F# \* h! L" V. f- E

# 创建随机数

. P) D( o2 W7 c

n = 100000

J1 H& e- M {5 f4 s/ E+ `4 |

x = np.random.randn(n)

- [$ a' `9 b" R) U

y = (1.5 * x) + np.random.randn(n)

# |/ i$ a" R( ], O

fig1 = plt.figure()

; o* e3 w7 t, n3 ]& O/ D

plt.plot(x,y,.r)

3 \* T& ?8 U$ Q

plt.xlabel(x)

0 R& Q7 S* n2 ?" O' @+ E. S

plt.ylabel(y)

7 S) T5 y8 A1 e' G

plt.savefig(2D_1V1.png,dpi=600)

* N( N h$ z' H

nbins = 200

; ~9 _3 ?$ @0 ~$ N& G

H, xedges, yedges = np.histogram2d(x,y,bins=nbins)

' J9 b2 W9 Y) ^

# H needs to be rotated and flipped

L: p2 G5 d' M, V' s5 L" H

H = np.rot90(H)

9 B) @* s5 n: A$ v

H = np.flipud(H)

& a* M) m) w- e! |" U0 A' f8 S

# 将zeros mask

) y9 M- C( T) {5 Y8 _( @* m' I. |

Hmasked = np.ma.masked_where(H==0,H)

[9 S# x# T- b8 ~( C, O2 _$ s

# Plot 2D histogram using pcolor

- A/ H- _9 n: B

fig2 = plt.figure()

$ y2 U* c# R% R0 L

plt.pcolormesh(xedges,yedges,Hmasked)

8 |- i6 ^- R, t7 \0 z

plt.xlabel(x)

! ?) |! V0 K/ R* M; T% }) A8 v- f1 s

plt.ylabel(y)

; Q b( e! }7 P1 U/ x# {9 F/ ]; y6 b

cbar = plt.colorbar()

. L9 i6 S+ t/ O# j* U; J

cbar.ax.set_ylabel(Counts)

# _* n4 [9 l/ a7 j% `* C

plt.savefig(2D_2V1.png,dpi=600)

* U" t1 u) g- H, \' P5 W" G

plt.show()

( v% i( [6 m! V- X6 W
3 W0 k* x3 E! H
2 c; O- g+ k6 y% H0 Q, r; t+ T
打开凤凰新闻,查看更多高清图片
% |6 G3 n5 h1 G
/ h, u, c, g! h3 X8 C
3 ], W* G! L4 T3 |

5 k7 e- a+ H9 T8 i: k. {2 ?; U

Example 2 :双Y轴(Python)

; J x2 i% N: G4 o8 _1 H# D

import csv

, t- G+ ]/ d# j3 B/ K) K$ v

import pandas as pd

: s2 ~' F' g9 `: ~% X' T9 d

import matplotlib.pyplot as plt

6 K' P& ^* N& C( a2 G! C/ e

from datetime import datetime

# a5 m- X' F: ^7 b/ I

data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)

( I+ l( x3 S1 P9 d

time=data[date [AST]]

2 l& ~) Q- R/ c+ ]

sal=data[salinity]

. Q" I1 }2 {5 Z A ]

tem=data[temperature [C]]

% ]" f" r2 h+ k; t7 v7 y/ S

print(sal)

" Z3 r: F8 n" h$ n: J

DAT = []

) Y* W9 U, D0 }

for row in time:

& N+ d9 R3 }7 }' O+ o/ ^

DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))

( ?! T; y0 e2 E5 g1 f, c) e$ Z

#create figure

/ T4 V4 ~3 ?0 k& c6 u4 A. ? q) e5 r

fig, ax =plt.subplots(1)

2 C7 l/ W) E6 z

# Plot y1 vs x in blue on the left vertical axis.

* S. x% O3 ^6 E2 _

plt.xlabel("Date [AST]")

1 Y4 N: g: r0 }: ]4 N, k

plt.ylabel("Temperature [C]", color="b")

2 y6 _' A# Q7 c9 ~3 g6 O

plt.tick_params(axis="y", labelcolor="b")

7 \' L, v* W6 ], r

plt.plot(DAT, tem, "b-", linewidth=1)

9 |, V% p q# Y5 W- j$ E0 W$ N6 y

plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")

+ A |- [. L, O) |! \: `" N

fig.autofmt_xdate(rotation=50)

) T8 s6 }! I) t9 ?& w- k9 m7 e; @

# Plot y2 vs x in red on the right vertical axis.

2 U* b* A0 A7 X1 y" M* _1 C7 u e0 {

plt.twinx()

" X b7 L s; r+ H! [8 g

plt.ylabel("Salinity", color="r")

6 N. B3 p0 P! D, I+ M( Q

plt.tick_params(axis="y", labelcolor="r")

. |* D, u, L5 ]3 ?- N

plt.plot(DAT, sal, "r-", linewidth=1)

- S: Q! c6 V$ d: i* b

#To save your graph

& Y+ f) ?6 z) i

plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)

$ d8 Q: |3 h5 Y! R( I

plt.show()

, a! ?: v9 g2 k$ U& I7 @

_5 a- Q5 {+ f7 p$ h3 u9 b

Example 3:拟合曲线(Python)

: r0 U; D: ?. z7 F: A) U: S

import csv

- r6 {3 w6 v, \2 m$ f9 y

import numpy as np

8 H8 ~ Z0 Z3 t5 {, X

import pandas as pd

B5 P. ^. R6 ^7 m2 L' m

from datetime import datetime

! E% K* \& h- D) f' V

import matplotlib.pyplot as plt

. q. m5 e9 v M3 u( h

import scipy.signal as signal

# t6 [7 h) I: `9 v5 H v; d8 n4 U

data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)

! Y% Z, } O; z" m

time=data[date [AST]]

% p* e4 I# V$ y; H' \$ b% `

temp=data[temperature [C]]

3 ~6 W" g! s; i) w! s) V

datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")

+ o1 ]2 ~) C, Q$ L9 }. e

DATE,decday = [],[]

2 j: s: @' t! _7 D

for row in time:

2 C9 ?+ S' N) p5 s4 n- k

daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")

3 v" Y% \5 Y8 h# D! `+ t) p

DATE.append(daterow)

; E+ b* Q/ a7 @+ h) R& m# \

decday.append((daterow-datestart).total_seconds()/(3600*24))

1 S2 b: f" O6 _7 V4 J/ M

# First, design the Buterworth filter

9 E2 q3 ^- I2 p1 a$ l

N = 2 # Filter order

+ c( f: @$ c+ j% R* e, w

Wn = 0.01 # Cutoff frequency

" Q' k( T* {+ _9 V6 p" F2 M# k

B, A = signal.butter(N, Wn, output=ba)

8 q" ?+ x" H0 {

# Second, apply the filter

2 q" `5 u- ]6 P- d9 E6 P

tempf = signal.filtfilt(B,A, temp)

9 ^7 q- J, X1 P, M$ |! C/ F

# Make plots

: E, L D: q9 {5 \; e2 J

fig = plt.figure()

8 Q7 T* `8 }( o, ]& _8 c- c

ax1 = fig.add_subplot(211)

% l5 F$ N; Z7 U% y

plt.plot(decday,temp, b-)

/ _: l1 ?. X. m6 W6 e

plt.plot(decday,tempf, r-,linewidth=2)

( Y' T. C$ F% }8 w2 l6 w

plt.ylabel("Temperature (oC)")

) e! \% T% H; \: k

plt.legend([Original,Filtered])

; b$ w! R4 n8 j

plt.title("Temperature from LOBO (Halifax, Canada)")

3 i7 n( Z9 N" w

ax1.axes.get_xaxis().set_visible(False)

/ _% R: n# ~& o6 {& }1 ^* F" E

ax1 = fig.add_subplot(212)

- o( K0 ~: s) G6 I q6 `+ [2 Y

plt.plot(decday,temp-tempf, b-)

. V5 X& _$ a+ s% Y

plt.ylabel("Temperature (oC)")

; M. g. n, _) C6 L$ P% ~& ]* s

plt.xlabel("Date")

6 G/ c* s+ k; W; g

plt.legend([Residuals])

$ q, q' ]) s, R( `# y# m

plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)

" `2 a6 E2 |- C. g' B

plt.show()

2 _; N# G% p* V# }% V) R

2 C- d9 A8 ? f* ]) |

Example 4:三维地形(Python)

) M' e9 r$ I; j% M# I1 X1 k

# This import registers the 3D projection

" B2 S* H/ H2 R) D

from mpl_toolkits.mplot3d import Axes3D

. D4 `" z# Y* l9 O2 J" ?+ a: i

from matplotlib import cbook

$ _' L+ s( ]) z, Y: O

from matplotlib import cm

- ?# S. Z6 v1 c, M$ X

from matplotlib.colors import LightSource

7 i- E+ |/ \: q5 ?: H5 t' s' |

import matplotlib.pyplot as plt

2 U! N/ i/ r2 ]2 ?

import numpy as np

' _& g2 }& F) G6 O N9 c' a8 v8 I a- h

filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)

9 Z$ t# u; c& e8 |$ S/ o# ]) U2 G

with np.load(filename) as dem:

& c. E& Y2 n; m, m8 C7 @5 h

z = dem[elevation]

7 H, a# @7 A3 |# p

nrows, ncols = z.shape

# \6 \' Q' Y$ |' T$ D9 q5 G) U

x = np.linspace(dem[xmin], dem[xmax], ncols)

0 {1 x' \) T: T* u1 M5 ^8 j; m

y = np.linspace(dem[ymin], dem[ymax], nrows)

7 T2 H# K% G7 _1 v3 e2 m" A- `

x, y = np.meshgrid(x, y)

. g" s" S3 \3 A+ L$ S1 I

region = np.s_[5:50, 5:50]

5 _% f7 B; m( ?$ b/ t% }2 Z

x, y, z = x[region], y[region], z[region]

' `3 m( J) s) b; b% c# F2 Z

fig, ax = plt.subplots(subplot_kw=dict(projection=3d))

7 ?$ W; _; Y3 E0 U. U# ~# S: C8 x, q

ls = LightSource(270, 45)

, d# Z ^0 f& W( r. j# w1 F

rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)

1 f. m5 I# Z! v$ N. s

surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,

& z4 ^" K1 O1 [. r5 U

linewidth=0, antialiased=False, shade=False)

8 M5 |0 e2 ~. W4 F

plt.savefig(example4.png,dpi=600, bbox_inches=tight)

8 y9 [; D. `9 B

plt.show()

: e2 y3 f& a, D1 B i! { 0 z$ i" [# c; R0 [0 G

Example 5:三维地形,包含投影(Python)

4 Y6 s8 o0 x1 G- @ " X/ E1 ^" O6 V

Example 6:切片,多维数据同时展现(Python)

/ g( P: j3 n! i3 t$ A # E/ v% \( p# ~3 j T" A

Example 7:SSH GIF 动图展现(Matlab)

$ ?" ~3 L/ _" S; o/ | " p& y7 u0 \( ?+ q& F T

Example 8:Glider GIF 动图展现(Python)

0 {" s- C& s+ F, k 5 o* {/ N( ^9 e7 {2 z8 f3 I

Example 9:涡度追踪 GIF 动图展现

% |" n3 \( `# t ; W5 J! h4 N9 z# K; A6 r' _0 ]
/ @8 m. U# X" v
0 G0 N+ V$ \1 L
2 Y; p0 J. |* p5 i, N 0 ]) G) {5 i% P7 D% r1 F ) w* d+ ~0 A0 p8 Y ( i9 O( x* R c1 l( e' k* _1 w0 \9 v$ |6 |" n! ^
回复

举报 使用道具

相关帖子

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
為了你我愿給
活跃在昨天 11:21
快速回复 返回顶部 返回列表