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

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

[复制链接]
3 g* p+ _( _( L, g
2 R$ x* a. L1 y6 {
0 c; D Q* d1 J
+ w2 Q0 Q5 ^9 c# a- |

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

9 q% L3 A) P9 c; l( \

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

- n+ U/ h& s5 |% R

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

. s9 d7 c7 V/ P! @+ j! @+ T: q2 ?

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

; V. g, ]5 t$ N) d( R' ]' H; P

import numpy as np

9 I# W6 v" I3 `& j

import matplotlib.pyplot as plt

' t0 d$ {" U4 U

# 创建随机数

' `4 m' \" f$ p0 W2 w# z d

n = 100000

M8 H; o1 Y- W6 E

x = np.random.randn(n)

: Y+ m, Z) U; T. F6 v5 O

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

! {0 q: X# @, {. u, V

fig1 = plt.figure()

# _2 L7 f4 j8 t' B @

plt.plot(x,y,.r)

0 ^7 b% ^8 V9 W

plt.xlabel(x)

6 K) g1 v- v7 Y& _. `" @

plt.ylabel(y)

6 l- E1 M" z2 X; k' b$ ^9 o

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

4 |$ P% W o0 m. X8 H7 \

nbins = 200

! x4 E. W, [4 s Y. ]& K& `5 N

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

4 d: a7 t" p; {3 ?- |

# H needs to be rotated and flipped

5 a; L8 _/ J* `$ r! Z+ x2 e

H = np.rot90(H)

& b$ m, P9 ]. g- t1 q

H = np.flipud(H)

/ R2 ^' f5 x5 S1 L: A8 G

# 将zeros mask

5 A: ?8 I7 v S" [

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

' E8 n" P) j, O0 N* F

# Plot 2D histogram using pcolor

( ?( U% N& m0 B9 {/ T

fig2 = plt.figure()

! k2 m& L9 v$ \" F& Q. c) G

plt.pcolormesh(xedges,yedges,Hmasked)

; v* d8 m, `7 N1 |! A+ k

plt.xlabel(x)

1 U0 m. G: Z' M2 s5 q; p& i

plt.ylabel(y)

, v! c/ D# y3 O6 T4 W5 `( Q# s

cbar = plt.colorbar()

1 t, w8 }. ~) T/ c1 J' z

cbar.ax.set_ylabel(Counts)

, x1 h. ^3 L' G

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

' ]/ w. N2 a& i

plt.show()

4 D8 y: P$ D$ T, `5 M3 [5 ]
! T2 F- S6 ^5 [2 }" ~
/ ]$ g A1 g, i% Z" k
打开凤凰新闻,查看更多高清图片
. `+ g3 D7 Y: ^' K
. E4 H7 _0 s7 V9 G- ]+ H( y( x
1 h) l2 H3 V" `, k R8 r* |

0 g4 j9 P/ x' }% U0 F1 F

Example 2 :双Y轴(Python)

# K1 A9 M5 o: U

import csv

3 b3 B, j- ?3 P2 V$ r# F7 L7 h

import pandas as pd

, Y8 ?) T n. i3 D# M. B5 a2 q

import matplotlib.pyplot as plt

$ U+ {" W9 }9 f! `1 O( |7 u

from datetime import datetime

. R" \1 R2 i4 `/ G" f

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

2 B* `( m4 M$ u: L* |$ |& V+ ?8 x( m

time=data[date [AST]]

& W0 l1 a, D2 X$ c, o. O. a

sal=data[salinity]

" }; B+ J) ~8 P6 f' V

tem=data[temperature [C]]

6 | {% y# X" i% t5 x

print(sal)

; I4 T, k* H. L; y8 T

DAT = []

5 D' ]) A$ D4 |( ~; n$ ?: a' E6 Q

for row in time:

7 T& x. a6 e; M7 o' |4 ?; a

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

0 T! D8 w* i H# \& f3 k2 D

#create figure

! y7 d5 u# |4 C+ k

fig, ax =plt.subplots(1)

/ O: L' N) U1 D, J$ v1 e

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

: c- b, i8 {/ E8 c1 R( J

plt.xlabel("Date [AST]")

$ p2 f( C- W3 \) M9 x. v2 g

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

" O* m0 [2 S$ y. V" H) {! L+ w

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

9 o; c9 v, E2 o% x

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

; ^% N7 h& ?( w$ M7 a8 ]

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

( y5 a! ~, |- o2 Y

fig.autofmt_xdate(rotation=50)

, E# p. ~# r5 J; B+ e

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

/ w- L: x/ u2 A% q; J

plt.twinx()

3 N$ |6 s% d; |. g9 r e

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

8 V1 j$ s; d% u% }. A% \9 P

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

+ Q R% K9 n: \4 m8 e2 F+ L# G

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

$ v: [: j( P* y7 s

#To save your graph

. a6 f, j; ]$ b. }

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

! z; a. ]8 J6 k/ Q

plt.show()

/ n. g g) q; H% ~" s+ _

9 o. h$ A) [; A! i: r

Example 3:拟合曲线(Python)

. S7 Z- S4 k5 N& `& L

import csv

! y1 D- C0 I+ Y! d8 ~7 g

import numpy as np

- Z) S/ R5 G9 f4 c+ v- H( |7 l

import pandas as pd

/ i0 D1 s6 N* L- ?* i! t

from datetime import datetime

# J6 I9 w3 y$ i0 [( E

import matplotlib.pyplot as plt

0 S; L7 C3 E9 w' \

import scipy.signal as signal

/ T, {5 H3 I* V

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

2 B; g% ~7 z% I8 [+ P! t0 V, A; B* H# d

time=data[date [AST]]

/ |' ]% n) |6 L/ a* `

temp=data[temperature [C]]

( c0 }# ^& l# J6 F+ d' K& v

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

" Q9 d$ r; y- C* R) ^

DATE,decday = [],[]

- |; i. j" ?! r/ X" g0 _! f

for row in time:

& @+ _3 K* @, E1 ]: v$ B

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

; {: T9 \& I( R' V% u, B5 S

DATE.append(daterow)

, J2 N; ?9 r0 D' k8 C( P

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

0 O( F3 Y6 v; s, t6 o

# First, design the Buterworth filter

! _' b+ h* d Q! G: \$ F' w

N = 2 # Filter order

8 p% s/ ?& L% e/ r! r

Wn = 0.01 # Cutoff frequency

# ~9 @' V0 P% B

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

" A9 n: {& D5 F. F% o9 Z7 z

# Second, apply the filter

) c ]$ L; U& y. e) F# t) r# e6 m

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

! x3 J+ H# ^, V( {

# Make plots

2 m2 M3 ^1 H$ H' F

fig = plt.figure()

# _1 V' |$ H% S- s4 f- I/ z2 c

ax1 = fig.add_subplot(211)

, O- U+ M2 b! J3 e) g

plt.plot(decday,temp, b-)

+ H1 [* T% N" D2 M

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

2 I# f" D/ a6 C& g$ B4 B$ r( Q- r& I

plt.ylabel("Temperature (oC)")

; Y. d$ A2 a" |* _ e

plt.legend([Original,Filtered])

/ d$ H( ?3 @" X; g+ z- m

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

2 |5 W$ c! R# E* ?

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

8 v# T' q, i, s ]

ax1 = fig.add_subplot(212)

; V0 p* e- T& A

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

3 S8 n7 ]0 I) _ i) V

plt.ylabel("Temperature (oC)")

2 I6 d0 c) d' X6 i; \

plt.xlabel("Date")

8 |; f/ T% H& r; S

plt.legend([Residuals])

- L3 j8 N4 |8 U3 O2 D/ j

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

% X+ w/ r5 y, M7 l2 E( _

plt.show()

+ w6 o( ]; m, i$ i- P* d: }+ N5 E

) p! D( E2 W2 G g' v$ _

Example 4:三维地形(Python)

+ y- W8 m% h, `( _4 f X

# This import registers the 3D projection

1 Y7 g. ` A/ `1 w# b q

from mpl_toolkits.mplot3d import Axes3D

! n+ ^% V/ R$ f/ A7 w* M" a: E* p

from matplotlib import cbook

3 r- u+ V9 b8 x* q/ t. c) ~/ H% |

from matplotlib import cm

& B) f6 H' u& t5 I+ V

from matplotlib.colors import LightSource

- u4 x3 h9 h9 z- ~* E

import matplotlib.pyplot as plt

! [6 q/ i* M$ c

import numpy as np

q) X; c! v, f, b* ^; s, ^2 u

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

9 k% i+ A5 z7 X" A3 ?

with np.load(filename) as dem:

% D3 p$ J8 r0 _ m- R* r- F3 B9 q

z = dem[elevation]

' q5 {' u# O( D; r; H9 ?

nrows, ncols = z.shape

* f$ x, ^" C @7 b

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

, A. G, H: m: p' c# C& E* K4 z: b

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

1 H4 ?# ^$ ? l, y4 W5 ^

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

. z. z0 @; d' N- Z- t0 S

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

5 E3 t- u7 p4 S z: H& h

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

. c; Z* O: S7 L# J

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

$ W- y" X. J/ G. Z! o

ls = LightSource(270, 45)

* a# F# ~3 ?. _# f

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

/ ^9 S; \* {1 ^6 c" D

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

( v5 k8 U. ~9 |. y( ~6 u

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

" {, j3 n3 @ U3 r- f. Z( _

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

, E9 `$ T |& P" x3 u

plt.show()

1 x5 M. o- T7 O9 g" q8 | % ?1 c) n: u3 i$ f' P

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

7 n% L2 E2 A& Z2 `2 P 1 }1 }5 y+ ?& E8 h% q) T& P2 ~; C: }

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

% Q/ p! U1 Z8 a ' z! `, k* B' s: L, h

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

0 d3 `! W8 D/ M' G " g. e, S. m- [

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

' q/ r7 H) D$ O# F& g $ ]( G; i; [2 C( ~

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

4 R7 r, j4 g% N# x4 S: O 2 V. B% ~# O- U+ U! r
7 q) ^8 U, \" [5 K
, u# {+ A/ v% ]1 y# l
o7 @$ z/ Q( X6 x6 r$ n 1 p9 c* Y0 v/ w0 A6 m8 Z. U9 b$ h- e/ g/ I _% O8 g & D" O" e- P" j, U! o8 F$ y; s* l# i+ l3 [% R* {1 p8 s1 L
回复

举报 使用道具

相关帖子

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