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

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

[复制链接]
" L* W- ?8 n& s; b* F
0 y: L2 b, K, {
+ s, c, T7 l; N; O0 A+ n; Z
- B( L- _: U: K

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

2 s# |$ I6 W+ q( p

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

1 `6 {9 \5 J- X' a# B# z" y

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

! q: B0 d2 g! I8 `0 W

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

6 R6 R. q5 x! ^% J$ q# h

import numpy as np

- ?1 a8 c7 \( [) ?) D) z

import matplotlib.pyplot as plt

; f4 ~% u8 p; p" ~0 B( q4 |

# 创建随机数

' f4 `' y, p# n, d/ N; }2 t" ^

n = 100000

; |0 |4 P- V5 W6 @. w' `

x = np.random.randn(n)

3 s- J# d. ^5 Y( z, Y% D& [

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

! Z! D! V8 j, d* a

fig1 = plt.figure()

2 I) y# k# a7 m- t$ {/ \9 T4 ^

plt.plot(x,y,.r)

. ]1 O5 ]$ T2 ~3 f" `6 |

plt.xlabel(x)

( L* f8 G7 q4 U" L

plt.ylabel(y)

* S; e2 N8 o) ]3 M

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

; y( ^" ~# A2 Y# u/ s

nbins = 200

9 v1 V" M& ?$ q8 j8 l6 _( e! M. N4 w7 i

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

7 \, r! G% e# ^2 x! K8 _9 W

# H needs to be rotated and flipped

5 [+ x2 {9 p5 l$ o

H = np.rot90(H)

6 K" Q2 h9 M2 z# X

H = np.flipud(H)

7 q6 {7 H% `8 V. w% V- d! V$ M: D7 M

# 将zeros mask

1 |+ R1 L- _1 y7 \% H: {/ e

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

8 {7 T7 W2 {# q S. K

# Plot 2D histogram using pcolor

" L( z! i, j2 g9 q/ P

fig2 = plt.figure()

! a3 I' ? S: u

plt.pcolormesh(xedges,yedges,Hmasked)

$ m4 I- [+ e2 h0 Q K9 x" U

plt.xlabel(x)

: }' R( z0 J) m/ H; `' ^* x5 I

plt.ylabel(y)

* l+ y8 b: u' i6 f( p/ r2 m$ _/ F I

cbar = plt.colorbar()

\" x# W6 K; l6 u

cbar.ax.set_ylabel(Counts)

. }7 @7 R% s3 k2 \

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

. Z4 X& T' q3 j; Z1 @9 `( C- [; [

plt.show()

D5 l5 w! [0 g9 o' j$ Y
5 G" U0 d6 L9 z# ]
3 B8 d9 ~9 A6 Y2 ?( ?
打开凤凰新闻,查看更多高清图片
, D& |; c* R# w1 P% P
0 b1 i, Q/ M5 A D+ K" x* L
) C% z1 |# {1 K( [. E1 ?3 \

7 i S' ^) k" v8 |8 O! V

Example 2 :双Y轴(Python)

: t* p" M& I& Q' I7 X' [+ l

import csv

) E2 y! Z& p7 X( g0 O4 G

import pandas as pd

/ Y" Z9 i5 Q( k v6 H1 k7 ?

import matplotlib.pyplot as plt

- A, e% v% Y! ^4 H% A$ }

from datetime import datetime

2 f4 i* y/ b2 Z) ?9 c8 N

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

% A* o3 r2 {8 G0 b% L

time=data[date [AST]]

! v: r* L4 ^0 r' Z& J4 }/ h

sal=data[salinity]

% s! v) w: C4 r1 s! }( Q

tem=data[temperature [C]]

1 m) U+ R8 N+ V. p( l

print(sal)

$ e( ?$ `8 W) _. A! K

DAT = []

9 \3 e, E( s" d3 {8 y

for row in time:

# r% w% {4 ? N$ Y* V

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

3 k* ^( n4 l) F" V' T

#create figure

5 V; A/ n2 i7 y r" Z

fig, ax =plt.subplots(1)

$ r$ b4 D2 R- ~% c

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

& y4 n2 n$ I4 z2 e

plt.xlabel("Date [AST]")

' {4 q* c9 o; C- G

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

2 z2 r+ Y* @. c, q v: @

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

b8 r! [( x5 [; r* C$ `

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

' G4 j# c; w4 T. }" z

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

# V' I5 O" P2 n9 W; m

fig.autofmt_xdate(rotation=50)

; N/ Z7 h5 ^- A0 I) ~: @/ W' @

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

; z5 y/ y* Q. G/ Y

plt.twinx()

7 u5 g! {2 k7 H! X# P' k

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

3 a( u6 m: K9 u# b

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

. y3 j) d7 ?4 P. s+ Y1 a% `" f

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

7 v- v8 }! y5 n6 k- z

#To save your graph

3 q2 t, l% c$ J2 {$ _9 z

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

* y7 N x( ~ V1 t9 c

plt.show()

- n; Z: ]9 S g9 E8 c- w

! M' S; L/ {- ^

Example 3:拟合曲线(Python)

% ]) a9 x! k, Y3 h& P% v) j% [* H) L

import csv

2 X, y( _2 U$ c

import numpy as np

3 g/ G" e$ \) i

import pandas as pd

& z5 {. i1 q9 s- E' \6 r# e

from datetime import datetime

+ O& V% g# h c N& W3 p

import matplotlib.pyplot as plt

; E9 U* S4 h/ ?# s! p

import scipy.signal as signal

4 R1 g/ X" R2 L1 X/ _* A# D) o& _

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

( F R! ^; \! Z a1 G

time=data[date [AST]]

3 U& X& Z& h" g( K

temp=data[temperature [C]]

s. d& M$ }1 D0 z7 G# f" D# y# L

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

7 Y* j* e/ F4 v3 Q ^& N$ o

DATE,decday = [],[]

) \3 {: k2 R% q# X

for row in time:

) N, R9 O: _# j: d1 h; ]5 S5 a2 A

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

7 L4 f( ` E. ]) G6 y7 ~

DATE.append(daterow)

1 S" e. b( p/ h& {8 ?

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

( G9 H% J7 V. n

# First, design the Buterworth filter

. r5 D8 S" I& k

N = 2 # Filter order

( \7 J' H+ s5 W+ l6 {4 Z5 g

Wn = 0.01 # Cutoff frequency

( k l$ {& ^. t& c+ s Z$ X! ^

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

- s' [9 C* j" E5 Z9 J/ `0 [

# Second, apply the filter

4 @/ Z" J0 B: M' [: G7 m

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

, C. k, a6 N2 O; G' P

# Make plots

/ H( b7 m. y/ B! Z7 J% R

fig = plt.figure()

4 o. g$ R; i" o2 B3 X% o

ax1 = fig.add_subplot(211)

1 f2 n' C6 y5 Y. o

plt.plot(decday,temp, b-)

% M& e" c8 N. M$ f

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

0 N& ^ z- P% D3 m

plt.ylabel("Temperature (oC)")

* I4 ^" G+ ]- ?

plt.legend([Original,Filtered])

4 {0 W- S* N+ g' _- w, p7 _

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

% G7 g/ ?+ q4 A% u3 l

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

* e, W' q& `. L1 Y3 i0 a

ax1 = fig.add_subplot(212)

9 h8 N' W" \4 {( U

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

4 z" I+ H8 p: t' g6 M7 Y+ S

plt.ylabel("Temperature (oC)")

. \, i; |& Z6 U2 K ?! P

plt.xlabel("Date")

( O8 @3 J6 w4 Q" n- ^# t0 f

plt.legend([Residuals])

+ o2 }/ e) ~' o- e: l& U% o& u

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

# N C7 L( e h& V9 X

plt.show()

+ b1 j$ Q& H- d8 t/ ^( s

9 |' x/ g* L# |' [8 m7 Q4 _, J

Example 4:三维地形(Python)

5 k6 n+ ]& y( c! N% [% s/ J

# This import registers the 3D projection

* ~+ ?' h( e2 d7 k

from mpl_toolkits.mplot3d import Axes3D

- J# W5 k% U" }' C/ S: o. u

from matplotlib import cbook

4 y3 t7 s: L7 z

from matplotlib import cm

& }, a' A# ~" b1 z) `5 r q

from matplotlib.colors import LightSource

& l! c+ N6 H" k4 w7 r" K" x

import matplotlib.pyplot as plt

, w9 ?! v/ x* k, }; i. p) I

import numpy as np

& h% @4 T) ~8 r, g' ~

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

& J. [& t. _# U# V

with np.load(filename) as dem:

0 ^; w/ N: g' I; x. E1 m

z = dem[elevation]

7 Q% L( C5 n4 O, l

nrows, ncols = z.shape

' p1 M. p& }% f9 ]0 X0 W, i. \

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

1 l. n$ `9 R" \! c

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

5 Z( T, V% P& {; |4 s, K" E' d5 B4 i

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

4 C0 ^% i0 }6 ?, x- m7 M

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

2 j4 |6 T$ o+ @2 Z+ |# C, T

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

, ~, i4 ]/ D5 F$ s3 O1 Q+ i

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

1 z* o9 w6 F! W: _7 r/ V5 p" h) R

ls = LightSource(270, 45)

; F. q$ I3 ?, j5 ]. b% M/ |9 I

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

' y1 Q1 D. l+ `# j7 h

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

' P4 n; P* d: a0 }5 t( {

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

, g$ n! p$ N/ N. W0 Q/ K) N

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

& e. B+ _, F6 }

plt.show()

% a+ ?8 l8 p( B. g1 k " e& Q' y* a _

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

0 a( A1 L5 ?8 F$ B! N 2 C6 _3 `3 f+ x& x1 y- L

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

: a [4 | g2 b) H4 B - ]- c" C0 D% W% g8 \& V

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

- M8 t" k! g) q" G+ i6 ^ - O3 I6 U7 r% m I3 w& _

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

5 Q- }% n+ n1 L3 h / X- a6 X; W) J4 h4 M, |

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

/ F$ J% p5 M: d* z , i0 w) }$ }+ ^# Y- Y8 C
# W) Z" J; w! U. h; {' U+ X
% n p. m, s* r& B% W" H
: l$ m, i: r% w b5 U5 @* `, k" E* ], u8 u, h4 I \; o! S / K+ z/ [2 D& `# q, M' N3 V9 z5 t 5 E& U3 E& s" X: f/ U 4 G* @: K/ B5 g+ z
回复

举报 使用道具

相关帖子

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