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

[复制链接]
* ~ c6 v2 k. V1 ]8 {# m& U
! J, x. | x d5 M. Q* l9 H" ^
- o/ s7 M! o) v( V
3 j% Y5 F5 C& l$ x: K* W

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

: n/ w6 o8 }' s- x# }. y

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

' O2 A/ {" z' `+ X8 R3 \

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

# U5 M6 m l/ c9 L& Y) ]+ s$ Z

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

0 U( ^) z. V. P+ q

import numpy as np

$ z( y3 Y9 y& y/ [6 x# T5 {6 W

import matplotlib.pyplot as plt

" B3 e4 i. K* z

# 创建随机数

! j1 X, J* O: t2 K1 s) e

n = 100000

+ o& c( |7 {. t( t8 s) Y

x = np.random.randn(n)

! y* i7 d% W9 e. C M

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

8 W- ^; w" T* N |

fig1 = plt.figure()

6 [9 e9 p1 i& ~

plt.plot(x,y,.r)

$ ]1 l! K5 r7 W3 S' o6 U

plt.xlabel(x)

) H# K& l" ]4 m9 N: l% U& z, r

plt.ylabel(y)

8 C, ]( q" L9 u: ~4 D

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

3 E4 N" L' v$ y, j6 j

nbins = 200

^6 M. R' m- e. S& D* L

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

9 f" Y! I8 q, I; Z1 T/ Z' E

# H needs to be rotated and flipped

& v8 K* L. w2 H6 D' G

H = np.rot90(H)

( h3 H/ `& b. v3 i3 V

H = np.flipud(H)

3 m) L8 u$ a8 m1 C' U- y

# 将zeros mask

# l% W8 q8 S I I/ R4 U" c! X

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

; c& [1 A$ _3 n) P7 d

# Plot 2D histogram using pcolor

$ y! F# I$ \* @+ N7 T1 T# Q

fig2 = plt.figure()

8 {* F8 k; @2 E8 F; t0 w3 I

plt.pcolormesh(xedges,yedges,Hmasked)

+ `! \5 A' e& B$ w+ V9 V

plt.xlabel(x)

% F5 W4 q& J4 I4 `8 |

plt.ylabel(y)

; S v" }4 O- f3 M" O' Y- U

cbar = plt.colorbar()

" G- ^ l- a6 a/ P: Z7 q% B$ _4 [

cbar.ax.set_ylabel(Counts)

u. z! A8 h. x" ]6 r+ i/ C

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

: I5 ^% t/ K9 n i5 d6 \

plt.show()

5 W) l/ T" ] ^5 Z6 {' Q7 \" g5 _
! e( m/ `; e0 I+ ~4 h3 C. U
1 z5 x4 P5 a2 c5 Y
打开凤凰新闻,查看更多高清图片
4 s2 ]7 ~, M/ [3 g+ [' B9 b
3 u* o7 N& K# m; |9 F; g2 H" Z$ c% r9 K
" Y i1 l8 e' k, v7 l3 e

5 d9 p) ~3 V4 h

Example 2 :双Y轴(Python)

, I3 A2 N# y+ R/ U3 C

import csv

5 M2 H5 i! f8 J2 J

import pandas as pd

8 U( e2 m5 ^& f

import matplotlib.pyplot as plt

3 V8 C3 Y/ x- ]4 Y

from datetime import datetime

' @, v D2 u$ J: d0 {% b8 U) G

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

/ _: M$ [2 C9 B% g) x

time=data[date [AST]]

0 z6 v$ j2 I3 a5 N! E; F2 @! R

sal=data[salinity]

% D8 ?7 m5 W, v6 |( e4 c

tem=data[temperature [C]]

, a( J8 a. N- k9 b# O6 S' d- Y# o

print(sal)

7 ^1 H& y6 n& f

DAT = []

% }8 ?7 [' F" c4 P4 N

for row in time:

+ H8 V% `+ \( o U+ Y

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

6 I [0 T8 i: t6 q$ Q I2 \

#create figure

1 m, {( q6 [* R4 B

fig, ax =plt.subplots(1)

) j0 f4 x# r7 [- T2 N4 Q" P8 h

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

! R$ n* e: p6 [$ l% { h

plt.xlabel("Date [AST]")

( Y$ V5 D3 J1 E n: g& {/ L

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

- r; i$ m; O5 J! i* e) p

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

9 I" y! l7 I9 X- e( J

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

) H2 |3 G2 l) n; m4 r8 N' n

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

% G( b A# r$ Y0 D) E# m

fig.autofmt_xdate(rotation=50)

+ Y" Y! V+ g" v$ B# z! v/ U

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

, H {1 W) X6 r2 Y6 D3 e

plt.twinx()

4 B7 @: K6 j1 K# m5 i+ V4 i ~, l

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

$ C8 l9 l. X- c1 l" U6 n. n

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

: o/ b h5 c; z2 R( a; N3 `

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

( ]6 b) w: G' w% M) ?

#To save your graph

9 Z3 U# C! b+ Z8 Z/ O' W3 @

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

* v9 c; Z! ?' P* @

plt.show()

- c0 Q Z6 x% ?7 x4 e

( S: e* m7 U. O9 G' p% y# m9 h0 w

Example 3:拟合曲线(Python)

! ]1 V, K6 w0 O; f* q

import csv

% p0 f6 U5 `& a/ ]* `4 M

import numpy as np

7 I s6 Q/ G- }7 D; a& _

import pandas as pd

( b% }) a n0 g8 V; t: [- v

from datetime import datetime

$ J. w$ `: ?& ~- e* s1 U/ N4 ~3 H

import matplotlib.pyplot as plt

+ l3 _* G1 r8 [# T! y

import scipy.signal as signal

+ q2 U) [6 x7 x) e2 w$ L* P6 e# ]

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

* R2 n/ m7 U3 `1 I% l! C

time=data[date [AST]]

0 y3 v2 s8 P5 u, k; i0 ~, F

temp=data[temperature [C]]

; Y* v9 @$ ]/ R# J9 @' w

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

1 j* _2 n* ]( u8 W6 V+ L4 v/ p3 z

DATE,decday = [],[]

! y3 ] Y& y. y; K& C' F1 N

for row in time:

6 x# W) i( r/ p/ B% Z. o' a4 j

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

$ Y9 n0 I" q' Q

DATE.append(daterow)

: D0 `1 {. y3 D9 Z

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

2 X: M' N+ d& Z0 H+ ~

# First, design the Buterworth filter

. n/ A2 t( J6 N! x6 R5 A2 K

N = 2 # Filter order

+ }4 Q5 W1 P% g- F

Wn = 0.01 # Cutoff frequency

g: v+ ^ Q4 I1 a6 P) V n4 F: s

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

% {9 P7 \4 E9 q) h

# Second, apply the filter

0 s6 Y# r l0 @( P$ |

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

' w; [$ F7 J# f+ p4 u3 }5 Z

# Make plots

) _+ ~" h, d6 N" s, u

fig = plt.figure()

* U7 h5 s/ c$ C+ a4 R# E

ax1 = fig.add_subplot(211)

) c; j! V) n5 a9 B- C

plt.plot(decday,temp, b-)

0 g, n4 a$ R! z

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

( d9 j+ C% r: \( p9 m

plt.ylabel("Temperature (oC)")

" O& u& y) M) f3 L

plt.legend([Original,Filtered])

. A, l2 m3 C! U

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

6 G. l$ z; e* `. `! Y% y; ?

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

$ F% u% t# \7 j% g$ w

ax1 = fig.add_subplot(212)

. [6 P9 y% K( P2 q

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

: O. `* X8 ^1 B

plt.ylabel("Temperature (oC)")

2 c; v9 W( D7 x

plt.xlabel("Date")

- G- D# |! q# z4 o$ \8 O b

plt.legend([Residuals])

2 X: _4 o/ a% s3 G7 J

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

; L: o4 `* f- \

plt.show()

- F. b9 u- Q1 H; j

' x0 [2 A6 f/ Z* f& Y

Example 4:三维地形(Python)

! M, c% R' m" n" O4 ]$ L* m0 C5 w

# This import registers the 3D projection

; ]$ l, Y( X8 a( W7 r4 C

from mpl_toolkits.mplot3d import Axes3D

h: g1 z0 k- ]* D8 k: U R

from matplotlib import cbook

6 W4 r( A$ f9 S; D! ^+ ?/ e

from matplotlib import cm

2 k) E5 x5 @( @* T6 r4 s1 `

from matplotlib.colors import LightSource

1 a @- a# b* E# r4 G- i$ a# X

import matplotlib.pyplot as plt

/ o* ]! A! P1 p( c" p" b

import numpy as np

: [! Q6 g, [) l1 a( p

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

; m7 ?" A: \& J* `$ X3 [, [

with np.load(filename) as dem:

/ t( o# u- b5 v9 \- H( P( t+ P: ~

z = dem[elevation]

9 T6 Z. U5 q, }1 T

nrows, ncols = z.shape

) q$ ?6 Z( ]. y) `

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

! i/ h! b* X' o/ \

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

+ R& ]& E" Y2 C

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

9 t' v5 o( K0 ]; _) g5 B

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

1 K+ Y1 i& E2 m. r

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

. }1 C- o7 B ?) i+ y, N! f

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

" c' A" W6 K5 X9 K- J( p

ls = LightSource(270, 45)

) u. Z; r5 Q+ u% @" \# P, M

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

; B1 A* F. B5 O5 W+ I

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

* y! l+ T7 j8 P7 b4 P, F

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

2 b- l9 o2 `' G1 {: _) m

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

# E `( F' Z$ C8 N& b

plt.show()

# D5 p9 {6 r; c$ D0 C0 A & r) X6 }! |: @0 j% S

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

. e6 f7 U& `( q0 m 9 Z* t% v# @1 W i( O

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

P4 S; m1 R6 X 1 e6 j2 V+ c# G2 v7 U: F

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

4 A1 V; s( } ?+ {# n; v% h 9 m- t& M6 V m

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

& r( R6 _. I$ O' p6 O8 S" l0 Y 9 O4 @# C3 }% F' L

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

4 u C! J3 y/ d( S# C | t# j6 J. }/ |
6 _# h2 P$ s4 ~( r- l
/ i% g/ V: g8 {) @3 f# b" ~
8 K4 \9 g- u7 C7 l! U: a! ^! T7 D% k% @, N & g. L& K- i& q& D, J2 w5 h2 v# G6 H6 G' ?: q ' H4 a: r: j, r! W4 p
回复

举报 使用道具

相关帖子

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