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

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

[复制链接]
2 }& j8 j d0 ~6 N% i) v
5 w$ V; t* k8 F8 p$ ~' \
0 [6 g% m8 [4 j# U9 m, n9 i3 T
+ q' h4 Y" R C b! ]4 Z s

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

% F9 b" U6 J% Y- w

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

" p8 U% A8 f! r+ ^, L; b% g8 q

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

6 r( h L5 |$ p) x' u+ p

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

7 `- ~; W, u2 F' k1 ?

import numpy as np

7 P$ Z( n: G) j- V% v0 q

import matplotlib.pyplot as plt

; k7 N* t9 i. q+ U. L

# 创建随机数

( E. Y, Z* i& ` L5 J) y; u

n = 100000

6 l7 V- d: Q7 W; ~" T( E

x = np.random.randn(n)

1 c! p* O4 [% h) H8 r' n, t

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

+ o, _1 p3 {, z

fig1 = plt.figure()

, W7 ]# y: i: e& n

plt.plot(x,y,.r)

R4 M3 i5 q5 q! r) v( l) E

plt.xlabel(x)

' K' }: X) P5 _+ z0 w: O

plt.ylabel(y)

4 |+ r* g6 U* _( u2 ]7 L

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

9 Z. P" `# r1 I" z# x9 T: R' N

nbins = 200

- l b2 }& @" [+ _* ^- D

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

9 Y f; v- D" T2 S+ x

# H needs to be rotated and flipped

% _9 o$ J2 E6 _* ^1 A8 q

H = np.rot90(H)

& j, ]( Q; E9 e! k, i( T

H = np.flipud(H)

. ]+ b% ~* q7 V: S2 l, r, ^

# 将zeros mask

( h9 Z m1 ~5 f& `8 n

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

: j- J: R& O1 H9 p7 A8 w3 H, ^

# Plot 2D histogram using pcolor

: B! g( s+ }3 [( A) y) q/ g

fig2 = plt.figure()

I& L( j. k+ m, X

plt.pcolormesh(xedges,yedges,Hmasked)

4 f/ O: h6 _3 D/ K

plt.xlabel(x)

& V* G! g1 Y$ c* `+ }( R

plt.ylabel(y)

9 S3 Y. w, a$ V w7 B

cbar = plt.colorbar()

+ }) i2 I3 ~( W9 C' w+ C# p

cbar.ax.set_ylabel(Counts)

) u0 s8 u& D% t* z: n6 S

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

; T# P; \& N" r3 N; B- L' J& O

plt.show()

4 h7 K/ n% F' s, G: l( ~
* [/ p/ B4 _2 M6 M" t- b; A9 B7 A& c
6 Z- w* F3 z# a! ^
打开凤凰新闻,查看更多高清图片
" u; U( I1 e# {, d3 ^
) h1 n1 g; |+ u9 `6 k7 }
3 K2 c3 ~9 H2 @2 ]

3 }( q# Q/ J6 M( h: K/ h, n* y

Example 2 :双Y轴(Python)

1 ?" @! U" x* H; M* Z; S

import csv

8 {$ C9 z8 T+ O2 [

import pandas as pd

# L' @: |. y% I$ i

import matplotlib.pyplot as plt

6 k- I# O1 [1 `9 V' G

from datetime import datetime

" d/ ?, K3 {0 Y# A8 J

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

! B8 C0 y% i# E

time=data[date [AST]]

" b, `+ @. z4 A9 _

sal=data[salinity]

( f4 ?6 c: s3 F: Q3 q" T7 L

tem=data[temperature [C]]

( G- J: B) I+ |3 \0 k! }

print(sal)

1 n1 k8 y) i \& s

DAT = []

' w+ C" n* ?# g9 R

for row in time:

, \( }8 Z) r$ m) F9 |2 H

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

, Q- A- f, M6 M) S

#create figure

- M. n; t4 L6 v+ b) r; l

fig, ax =plt.subplots(1)

\- p' N6 P5 H8 C. }/ k9 k" _$ E

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

( w7 {( f* P( I# W! x% w% h

plt.xlabel("Date [AST]")

0 G/ N7 @$ e. I

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

$ Z5 I- [3 N% D

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

( a4 C" Y; P3 ~, J1 Z" A9 ?

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

1 S" i: N2 S5 _8 a% p$ u4 F" N

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

4 Q8 m6 b) u& @/ e; h2 a

fig.autofmt_xdate(rotation=50)

2 c( u3 W2 j2 w1 O+ @2 o: Y7 C

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

) ?& V( K0 B8 j7 N1 P# c& H

plt.twinx()

, F B- \- J( X5 h1 [$ G+ S9 y9 ^

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

3 Y( e- I. w$ n4 j- C3 Q

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

- I( h1 t$ V; X

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

$ A) o9 L; K& k2 C: ~

#To save your graph

# j/ Y I2 r' B. a# |. V

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

: e' H! e3 G; @ D R+ _

plt.show()

O1 D: U, @! W

& F9 Q; g0 V; E6 _3 l4 A

Example 3:拟合曲线(Python)

$ l3 D& e7 z* [% p

import csv

+ y# I/ {6 K+ Y8 o

import numpy as np

6 y% ]1 q% P$ k* r" T

import pandas as pd

: G0 [) {: F& ]3 S

from datetime import datetime

, V1 z) t5 P! w; ? y' T

import matplotlib.pyplot as plt

( k* X& T7 k8 |

import scipy.signal as signal

4 F. a& R& a% h8 h J

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

8 R8 `7 s s$ w5 H8 q! d

time=data[date [AST]]

+ G& e7 h- f4 Q

temp=data[temperature [C]]

, b6 d. A. T! L" s

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

' R2 x& k, ~' \+ D+ H3 D" J

DATE,decday = [],[]

* T. q4 I6 {& m6 M% R

for row in time:

) [" q* u% B0 U$ B8 l, p" d$ z

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

: }3 R# N- H4 o8 O

DATE.append(daterow)

1 w. n& ]1 u& f4 D

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

3 K' u* b. i B+ j

# First, design the Buterworth filter

+ B/ a7 v- ^0 @& s* d6 ~

N = 2 # Filter order

! i' E/ a2 v- d. ~

Wn = 0.01 # Cutoff frequency

& h" ]$ y5 g1 U5 Y7 d

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

6 U- L" \- G: I/ x& S: z

# Second, apply the filter

, {: |( s, ?4 v* u" N

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

8 h% C2 c, R' V2 p$ I6 w9 f

# Make plots

+ @" Y- F5 o3 C% a

fig = plt.figure()

& ^9 f k6 L+ X0 N) d

ax1 = fig.add_subplot(211)

3 O: X2 l( G- E

plt.plot(decday,temp, b-)

( n% l: Y! u! i) _# g8 E9 O

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

7 O; c7 P* q( h/ ~% G' i! O3 y' l

plt.ylabel("Temperature (oC)")

+ |# V- ?& F, A* O! @: e

plt.legend([Original,Filtered])

, V3 ^: v! J# T

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

& x* u9 t) v; C' x6 N# C2 Q3 G

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

( ]% @2 k( U: e! E

ax1 = fig.add_subplot(212)

7 `0 U* U8 u, _

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

* w& G3 z) L! ^& g6 K; e$ {& @

plt.ylabel("Temperature (oC)")

3 L1 g( w! j, H$ q5 l' @! A" Q% H

plt.xlabel("Date")

" }. ?5 m% D5 i9 j

plt.legend([Residuals])

8 J' J7 ?, O+ J6 P3 F( p" U4 e

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

. N: j% F* u/ ?5 V* W" q

plt.show()

# b; ]% V$ X/ Q( A

) A( B, B- }" \6 L; q Z0 v

Example 4:三维地形(Python)

5 }0 D: X1 t! Q

# This import registers the 3D projection

, r, a; b* J/ E' t. i2 L% n

from mpl_toolkits.mplot3d import Axes3D

( ?6 H- X) `$ _! Z5 f8 r" ]

from matplotlib import cbook

7 q8 u1 g, c8 t+ W9 b( o5 y

from matplotlib import cm

* C' N! M) P/ X- C- u& h

from matplotlib.colors import LightSource

9 D& z4 ?! l9 M! p' E& I; ~: Y3 ]

import matplotlib.pyplot as plt

3 @1 G* M7 \& C) a

import numpy as np

4 v* [* F* i3 v2 ]5 Y. M$ P; }2 B% T

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

Z+ b! I( Q; W

with np.load(filename) as dem:

: V9 Q0 N& g, L4 b; j# I- |

z = dem[elevation]

, g2 K |2 Y3 I

nrows, ncols = z.shape

8 D# ?5 X" N# }# f

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

a6 A3 v+ k0 c& Z' Z& h3 i C1 z

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

/ k# r% ^) q' Y1 b6 `! i* C

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

/ K, g' }- k9 ^. y: @! g9 z

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

1 T% K4 j. W9 j7 R f5 Z; @

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

$ x C4 s6 ?1 h2 Z2 M( r

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

9 f5 L( e6 X' L# E

ls = LightSource(270, 45)

$ K( B! S0 L: s3 M

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

8 l" Q! y& s* W2 h9 |

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

) \0 M. B9 ^2 \

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

6 n7 l4 M$ I8 g/ W: h9 E# b7 y. q% [3 o

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

4 X7 s5 S9 ^1 t: D0 M9 T

plt.show()

2 Q$ p* W0 y, j' ~/ D! e 0 V; p0 ]7 H; D2 h7 e

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

7 F' ~, f" {( V5 Q: j& @ . a1 b' n0 X2 e, N0 E+ l- P

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

& {( \; l1 @, e& u; U2 ? + E) o k! v. B/ F" _4 p

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

! Y0 a9 {0 ~; H: }) B# M , X0 J1 H* P1 w& e$ R( z: r

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

W8 r4 X0 h4 g, j4 y' ` U6 a $ T4 P' O9 _2 l; I! X( O1 E

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

5 X1 b4 t; N4 y1 ]- F 0 Y9 Y; _1 A# X, d
/ ^) Y7 W5 P& E- X" z
. o: r& p+ t9 j- v+ P/ Z3 z: y
% i: V& d" Y4 I6 J" a' m ) c: z; H0 x, V7 n , K/ `5 z. W- T) m, ?, R- z3 D8 P' a% E3 H! n/ Y2 A 4 j9 T& A2 P; g9 |- q" D
回复

举报 使用道具

相关帖子

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