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

[复制链接]
+ H/ k' O) f6 U: w
& s% p# {, V& s& M
" z3 ?( ?) r- k; s x
, C& [+ t L; e& A4 l% s+ s0 d

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

( c5 x7 ?! k, i3 \, G! i; \6 p

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

( Z$ j* N! L- W4 B1 C

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

% r0 x; k/ {3 E

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

. U- S1 U: M9 ^% E9 z: {; ]% u

import numpy as np

: N5 V, t& d4 `! \$ V

import matplotlib.pyplot as plt

$ M& i7 b/ ]6 W. K

# 创建随机数

3 Q/ l0 z5 Y$ M' e

n = 100000

7 r v/ }2 y# m

x = np.random.randn(n)

" C0 }+ H/ O9 a7 U: F( X

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

, Q! d1 M5 G, [0 {) L: E

fig1 = plt.figure()

' F/ A1 [& r# h' m$ K

plt.plot(x,y,.r)

( T$ Q; {% n& y

plt.xlabel(x)

* [1 h a, v3 Y) h6 v) n; }

plt.ylabel(y)

i% |" S9 H$ y

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

! h* n1 `5 J: p9 [$ q6 ~

nbins = 200

# C* R1 ?8 A, {. R2 j

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

5 y0 I0 n6 b% P/ t

# H needs to be rotated and flipped

- e$ X9 Q3 E7 a1 n6 d& N6 V

H = np.rot90(H)

3 S, \# _/ o' ^; ?0 y# {

H = np.flipud(H)

8 ~+ K5 a+ t) q+ H% M$ B2 L

# 将zeros mask

, f/ S/ E, ^, Z+ [- i8 j4 W [

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

, _# l' s" B0 I* A5 c" P0 ^) M! V

# Plot 2D histogram using pcolor

+ Q5 h* M! [* _" ^9 D8 {

fig2 = plt.figure()

3 R5 S% Z5 k+ j0 S5 f

plt.pcolormesh(xedges,yedges,Hmasked)

- S* S% k3 o2 h" Z# _ n, Z

plt.xlabel(x)

/ J8 K8 a2 K6 v% D- w8 p

plt.ylabel(y)

, ]$ |3 T; P2 @* s& t3 g v

cbar = plt.colorbar()

; m1 C" m: ?; I4 {, Y9 [) ~. _! z

cbar.ax.set_ylabel(Counts)

! H3 v# J& b# Q4 Q( W7 Z+ ~

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

1 U; Q+ L+ y2 x5 J) l

plt.show()

& @; {8 w6 c7 [$ o8 _, x# ^/ @. n6 J8 C
! u' w2 ]" Z% K+ i
) i$ w2 n) P) B6 H% ]" D
打开凤凰新闻,查看更多高清图片
9 e" \" i4 @1 d6 M# y
2 }" {% m9 N) e( N+ b& i% L
& l* b7 a3 n" u1 N3 N/ @

% I0 G6 X: }/ l( R6 l

Example 2 :双Y轴(Python)

4 d- h! M! C) a$ C1 E5 a1 a& s

import csv

# }2 N+ ]8 y" S+ P0 b

import pandas as pd

8 b& _- Q5 T! \

import matplotlib.pyplot as plt

7 T; z$ U" w( R) F5 o& u' c

from datetime import datetime

$ Z% N, r8 H+ G

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

/ z7 p* d9 t+ J; e& u

time=data[date [AST]]

; ?# K' \0 |" U0 G

sal=data[salinity]

2 m. T1 N; q0 a# p

tem=data[temperature [C]]

" L4 ` W! |9 Z5 b, O) m. E

print(sal)

4 A: c8 V8 R& D5 k( \% b4 k/ s

DAT = []

% l" m' x. q) J6 J

for row in time:

9 h; g* ~ Q D# C& g- d6 f1 D

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

' E5 C* Y# }$ q( v- A0 G

#create figure

- Z: m I4 w- _! _( S

fig, ax =plt.subplots(1)

$ U8 Y+ x/ M2 p7 W+ |+ w7 Q

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

6 q0 K$ v. w' z3 n

plt.xlabel("Date [AST]")

- z- r% g* R# L0 c0 j

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

' e5 \+ }9 d$ N- U8 u$ J$ l

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

) \( l$ u4 F! C7 O ^, I

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

- q$ I& ^3 B( K

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

. ?! r/ L# w i# x

fig.autofmt_xdate(rotation=50)

, H7 O1 I0 Z2 c5 E- L5 M/ z

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

_, T* Z. p* S% X& v" X& |# u

plt.twinx()

( S: i! C& F. Y

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

$ T2 N5 [5 J# r w+ E: x# e

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

- f2 |, H* i0 X

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

8 J' [2 X; ~. `, P: b) m( y9 @

#To save your graph

/ w' D1 m& o# ^7 Q9 r( Z

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

$ w0 `5 ^3 O! L& }( B8 w

plt.show()

, e3 }/ H9 r( T+ j$ |

3 T* B# a' G! j7 h/ b

Example 3:拟合曲线(Python)

8 x/ f5 P: s6 M$ h7 t. X% P

import csv

9 {9 X. ?# {4 F" f1 y3 _& }7 P

import numpy as np

9 ]" \* {4 _4 @7 {. @

import pandas as pd

& Z' X. S0 L2 s2 f+ C; n/ i; g

from datetime import datetime

: G5 a; d! V: f- ?2 w

import matplotlib.pyplot as plt

4 O. g6 ]* C9 d; [: K. C

import scipy.signal as signal

, \+ n4 E. I# T' L- T6 S1 t

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

+ L; }' E" P! @0 ?

time=data[date [AST]]

7 j; a+ n) L& r f

temp=data[temperature [C]]

1 q" K' s5 {6 N$ C; N k( l6 S

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

& _& o" b" u X9 b* k. o( J

DATE,decday = [],[]

# V4 q. E4 C) E7 k2 Q

for row in time:

* c- m6 M4 H8 K( I

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

|/ h+ ]5 H+ N3 P: b4 D

DATE.append(daterow)

8 \% n9 N+ }) ~% E! ]. ?

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

) x2 s/ ?' V) i0 U& R; \

# First, design the Buterworth filter

( _8 S9 C& n' O

N = 2 # Filter order

2 \4 h3 f+ p D

Wn = 0.01 # Cutoff frequency

) k7 i( C8 [7 H9 f

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

5 x1 p# X6 j) _$ ?) n3 u W* m

# Second, apply the filter

0 o3 I! e- g3 w& C7 a

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

& |/ o X+ V9 K/ \# n

# Make plots

8 v. l* R! M5 f y, I5 E, f

fig = plt.figure()

8 X& I. u# m Q( g) M3 w

ax1 = fig.add_subplot(211)

' e' Q+ u/ V4 b* ?% V4 y; ^7 E* ~

plt.plot(decday,temp, b-)

6 W5 @. W: i( Y6 O) G2 ]$ ]- `

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

1 p2 ?# u0 T0 x# V1 Y+ M. a8 Z& F

plt.ylabel("Temperature (oC)")

" h C* @; Z3 j6 @( w( j1 z

plt.legend([Original,Filtered])

7 u/ H) T1 e! x! k% r( W- p

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

?& t2 J0 s! e( A# l6 R" m

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

4 ]/ B5 l) L2 R5 w' A( G

ax1 = fig.add_subplot(212)

& W+ K* I9 W& w4 s4 B) P

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

" g$ n! [! |0 w" b+ M4 o& V

plt.ylabel("Temperature (oC)")

* y+ X0 }( t: W

plt.xlabel("Date")

- p% B* o3 u+ x

plt.legend([Residuals])

: K3 B# |/ h2 s$ ?

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

) g8 Y4 x( H e6 Z9 r* d

plt.show()

( e' @; _; N) L+ W

3 q* |6 T5 i# f( A8 X+ b

Example 4:三维地形(Python)

+ R0 J6 Z% B/ }8 O! a7 Z

# This import registers the 3D projection

5 T4 {7 b6 O$ Z# A2 w- c

from mpl_toolkits.mplot3d import Axes3D

4 t1 e# y6 y2 @+ i

from matplotlib import cbook

+ L, V9 v4 P1 b

from matplotlib import cm

, g/ x8 v3 C* \8 G

from matplotlib.colors import LightSource

& U7 t* C$ U. F u4 u6 { ^

import matplotlib.pyplot as plt

) n2 z; m: E+ u1 {2 O& r0 C4 b

import numpy as np

0 k" k$ J4 }. x" j9 v; b w6 n' h

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

( F# m+ H6 I9 `( l9 G6 q+ d

with np.load(filename) as dem:

6 D8 A1 M5 K) x% ?6 c" Y% W5 E

z = dem[elevation]

4 u/ [, U8 o( C

nrows, ncols = z.shape

% ~7 v# @! k% b, [8 M( a( n

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

! T+ S2 g! d3 W, H

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

! l- k; ^: n7 I* ^ C

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

& _3 m8 o' ~. ^( i% `: P

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

, P3 i$ c' p- f

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

; O3 E2 l4 b0 V

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

4 S% ]: e5 P' [; k' J: }/ ?$ n

ls = LightSource(270, 45)

2 M7 W' r+ b3 \- _- v8 e9 E# e; O* D

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

; Q4 ~# z/ S! j, m

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

$ }- h/ e. N1 C1 D7 K" ^

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

+ u8 N# n" n) D/ F

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

- O& i0 _- I" X5 Z8 s

plt.show()

6 K% q7 D! N, I5 x % P* ^* V" W) ~; F" v

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

$ f& ~3 t8 G( d# M# W ( J) h% Y) ?7 ~5 g

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

* e' R* S8 f4 C % I, e w$ w, _- g+ {. P7 H

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

4 a4 L/ {$ z6 h" y * R- \8 x( j5 g% k! k$ k$ z! I

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

& y+ Q( |9 C" W% `7 R9 ? 6 @' @, U5 L9 K( U% g9 t9 v+ b4 B

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

5 A l0 n+ n) ^ ]& T 1 |% U; ?( s6 L, h* c' X
, Z: n: e3 Z: N- A& e
" j8 V/ O# C/ d& ^5 `
* K& Z9 f2 B- E/ Z" B * \" h* \* [5 u ; {- M+ N5 W" o( m" J$ k9 Q. W 5 { s! l! V1 ^0 a1 d1 T5 H5 O, U) k, x9 z

相关帖子

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