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

[复制链接]
: a4 z4 h/ }& Z3 X- k$ J* w' Y+ p
! d0 e0 C5 v4 T
, X% S2 D5 D* D6 _! R: z+ G
) G( H2 @1 d# y! R# [. T' [" L

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

1 H% Y' u, M+ k0 D$ D

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

! F2 K# u# k: \$ Y

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

l- Q! c7 y. ^$ Z m( I

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

0 O6 v5 D8 u5 I+ Q# H! B/ W

import numpy as np

, a2 S5 r( V7 f

import matplotlib.pyplot as plt

4 P0 Y: u" ~% l( T- Y p

# 创建随机数

: F) w0 S* }! _2 ]- q3 f

n = 100000

/ h, F, K) S# }* J2 V" P/ f

x = np.random.randn(n)

; Y# C0 w8 N. X' j! f4 e/ J

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

8 v" r3 t; }/ V2 V2 `; [

fig1 = plt.figure()

5 f! ]0 S0 S' I% a3 k- k) t$ m

plt.plot(x,y,.r)

0 |0 y h* n+ u4 i8 K" t

plt.xlabel(x)

+ \3 A; n3 E5 U! q! F! ~# \

plt.ylabel(y)

& x0 o8 X( o" F$ q

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

/ S, U& Y0 `- ?4 B, b- ?( m+ U! V1 v

nbins = 200

4 _ ^( E4 l# }9 q

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

7 {) ^/ J, ~% |% i+ e1 u& }( O

# H needs to be rotated and flipped

: G' \3 j6 x/ h7 T/ N" }; W6 ?

H = np.rot90(H)

+ m6 `1 e$ W0 s, f& w) d: G8 f

H = np.flipud(H)

# E0 T: D% @- R# c. g

# 将zeros mask

0 E- w7 j: J9 f& b+ |4 C

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

! w& s* |9 x$ Z/ `6 I) |

# Plot 2D histogram using pcolor

; R* `6 s. @. H

fig2 = plt.figure()

8 t5 F8 o; r: E9 f- t

plt.pcolormesh(xedges,yedges,Hmasked)

" z/ _, x. s6 D# R2 d

plt.xlabel(x)

8 L9 }; M6 ~' }9 m" B C6 y

plt.ylabel(y)

; N6 i# ]; l& }, F0 I/ R

cbar = plt.colorbar()

) e& O. {4 d& N$ R6 U

cbar.ax.set_ylabel(Counts)

: I5 z% n4 J# l) r% K2 t

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

+ W# m" [ b- {4 }+ K) b6 c

plt.show()

; r" x$ s) C/ ~- \5 \7 W4 I: I
* g- X8 U: F* m( M6 ^' K$ y7 U: M
2 ?, Y$ }% b4 q1 r2 ^. c
打开凤凰新闻,查看更多高清图片
3 X' U* F& m$ D. w; [/ R* _
+ ~* W) I' {3 w. O1 V; u& w6 S, ^/ M
* ~: C4 B" F% [5 `* ?2 l- A

+ `8 k1 a$ ?3 m' I4 ?

Example 2 :双Y轴(Python)

) o8 W a. u! i3 O, v

import csv

$ ]8 }3 j9 D X, B" r2 ]

import pandas as pd

9 P0 X% M0 c- t, M0 H

import matplotlib.pyplot as plt

3 X9 W# E+ h% W" d8 ~. ]

from datetime import datetime

; |8 P2 I) G k

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

- k+ }. n$ i1 m3 A; Y

time=data[date [AST]]

$ w3 e) F; N7 i" [6 k" K$ x0 B

sal=data[salinity]

1 a. Q$ N' N) P! z& I+ p6 g

tem=data[temperature [C]]

( e# ^( [8 ~& e5 K

print(sal)

9 @: v2 T0 |, |( H+ i4 H

DAT = []

. Q% g, R/ p; u- P2 i3 O

for row in time:

; ]' h3 B' p9 {& g) I' g8 V

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

7 {* A: A3 U3 H6 {5 f* ~

#create figure

8 z+ R2 z, J. L5 s

fig, ax =plt.subplots(1)

( O6 ^# l4 K8 ~

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

1 B3 z+ O8 u1 l3 ^) p1 K, n4 J

plt.xlabel("Date [AST]")

. e6 q# e. W* ? x' y6 M$ N

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

- d9 l, k0 {; Z

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

, Q- {6 X3 W' u

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

' ~% F/ ]4 I7 o

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

* N* H! i& A6 w: H1 C

fig.autofmt_xdate(rotation=50)

2 J% F& X1 N" Y+ V8 d

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

, i5 G/ R/ q0 i9 \: {/ O7 H+ O

plt.twinx()

1 F _" W/ t' M7 e# p% E. |3 V; K

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

. r/ G: _$ e/ J3 }8 i, ]

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

; W. k9 e3 r9 k$ @0 h7 D

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

0 F' H- } N! [! \

#To save your graph

, r5 I" \/ G+ o

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

7 K* \7 M$ ]$ Z. h' R2 ]" S

plt.show()

1 ^- \' s* m& K! b& ]

* B/ A7 k& N$ f. a8 |0 @/ h

Example 3:拟合曲线(Python)

- k- T. c+ U* X, w" |

import csv

' c) C4 z0 n1 q/ F& k: z

import numpy as np

# ?0 c0 C8 g/ Q- h% H3 k8 d

import pandas as pd

7 z1 u- G/ R- |& K$ g& ]1 A2 D

from datetime import datetime

0 ~5 r' ?, R5 O

import matplotlib.pyplot as plt

1 e: w( ~ `' }4 o1 E$ @

import scipy.signal as signal

$ V) n6 \% h+ E6 S6 r! ^" @8 `

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

1 R; Y7 K; c3 c) w6 B6 A( c$ }+ @( Z

time=data[date [AST]]

$ l' L- P2 [1 L' B' c% ]2 T% t4 j

temp=data[temperature [C]]

7 ? U" t* j6 K* \5 `$ T5 h

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

, p5 e7 C# { m* a' e/ ?9 C+ ~6 v$ m' W

DATE,decday = [],[]

) M w$ C3 w* N+ `2 h" K1 d

for row in time:

# G t" l2 e) I* N# X

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

! W# R, R1 f* M6 s

DATE.append(daterow)

: Z/ k/ \9 z# h; P3 x# x

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

. ?- |7 [) E' I# n% X

# First, design the Buterworth filter

; }7 N( j' V' _- P- X0 d8 G' J6 ?

N = 2 # Filter order

/ i) h+ `. C) O

Wn = 0.01 # Cutoff frequency

5 p t- U9 N$ q

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

" Y( o& @5 ~$ a8 t' \- X, |7 x+ n8 i& Q

# Second, apply the filter

4 ~( w. `% O: }7 C# t3 C6 S

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

9 I0 f( a2 T% y1 E: R3 r; {5 S

# Make plots

; P* [. M ^! \' ^

fig = plt.figure()

2 X3 `4 u& U' F# V2 f

ax1 = fig.add_subplot(211)

9 D' q* y! L7 t+ d% d

plt.plot(decday,temp, b-)

9 u& e6 o& Z# b, O1 q

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

* S8 g0 ]6 w! ?5 Y5 u' f3 |( M2 `

plt.ylabel("Temperature (oC)")

k; K# ^* m: J

plt.legend([Original,Filtered])

+ ]0 U( p3 t# U0 J' J/ }4 ?

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

% L, c9 r: N& ~! Q9 T( w

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

1 F& e4 A, S4 H3 G/ h: t

ax1 = fig.add_subplot(212)

; M6 q2 v+ N) s- j: h

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

) Z/ E- L# ]' w1 I

plt.ylabel("Temperature (oC)")

( c# F j4 y. S& d- X+ G. v

plt.xlabel("Date")

3 g$ A) k$ N" f+ _7 w

plt.legend([Residuals])

/ y* }3 f4 T; D9 B

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

4 Z7 L& [4 Q+ i: }7 [/ s) v

plt.show()

$ J' ~3 |4 u) u0 s ]; p

% a9 a3 D: W' x J# u

Example 4:三维地形(Python)

/ D* S; ?6 O2 `

# This import registers the 3D projection

& F7 Y( a3 X* a

from mpl_toolkits.mplot3d import Axes3D

2 B. E% d1 N% l+ J N7 o

from matplotlib import cbook

9 J! \+ G3 t0 `

from matplotlib import cm

8 x1 M+ E' F" | }- w

from matplotlib.colors import LightSource

, J# c8 [. ]7 I. H _( z

import matplotlib.pyplot as plt

7 A+ r% d3 S8 l- H$ I

import numpy as np

; i* Z; c" x, z! \1 \3 F

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

. {! f# R2 g% b3 b# i" N

with np.load(filename) as dem:

# ]) _! @* X. b P' S) Q2 O4 t

z = dem[elevation]

( N& |) n5 y7 R1 [( t! [

nrows, ncols = z.shape

4 Y# N6 Y8 s/ I( |8 h$ v, d

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

' Q4 e/ Q, V G7 w1 s

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

" b! P( J( A& q8 R% O+ K

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

% @' x) U# a; {3 U/ N: K

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

$ g( f$ q0 j; G3 C2 Q$ D

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

# j4 S) y7 G/ C( }3 V

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

$ {4 @* C- l/ ]4 y

ls = LightSource(270, 45)

$ c l$ ]8 H6 |0 W% \

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

+ c* b7 d( Z t

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

' T+ `9 y* i0 G1 `& e% @6 c0 q# y% u

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

" p3 c/ r, ^0 i$ g# w- G. V

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

1 C% t+ `- m8 `4 V

plt.show()

8 Z6 I+ o+ h( P8 e8 A$ C: o; W 3 a8 K4 q* e! R7 L4 d

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

' P0 F1 T, r8 o/ S 7 Y8 M+ `$ u% x6 M+ K0 t: f3 k* \

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

) G7 Q3 U$ Y2 j7 H , W8 i+ f, D2 W6 ?9 |4 M

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

* ~4 _" {! k. W0 B& G( H! T$ w 5 s; X& D. m7 ~; `" ?

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

1 h" g* X+ K. h8 { 3 n, ^( `! k; P- t7 w; P# A4 `

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

% S7 x, x6 [5 C 3 u2 a. h/ @4 F' s) d% S1 E1 w
M+ K \9 t5 S; o' d! y
( x l* B$ Q; @/ _
5 U! u6 _( r" z4 U; N* c9 X! f ) e2 F. i* a) [7 d; [+ J; \& k # C, H% Z2 x7 E( a; N+ s$ ?+ [4 j; ~- G7 h# o$ ~$ I5 H4 a( i" q 1 H5 e3 {9 H# K# N2 p
回复

举报 使用道具

相关帖子

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