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

[复制链接]
5 T% i; M- M" u* d3 E. U% o. C
/ M4 V) d% j' {4 Z6 n' K
% n0 k# x/ E5 j, O( Z+ u
6 H! J8 S1 V2 r9 _; ]& f

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

# H1 }0 b' u' n7 z# X: U

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

) A7 D: d) g t5 q

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

' v5 o: n# L! \0 b0 A K

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

- _3 c5 \) `8 d$ `4 `( i1 m

import numpy as np

+ _) X+ a3 D9 d

import matplotlib.pyplot as plt

# y2 {2 ]$ v" O* n+ }8 l0 |

# 创建随机数

9 V; y6 U& c6 p4 `6 @3 y- {: _

n = 100000

1 j; y- [ q* c" u0 E

x = np.random.randn(n)

$ E: R9 c$ h: E! m" X

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

8 c: ]: t. K) Z; C4 W, k+ i

fig1 = plt.figure()

+ \* u' J+ @- l q6 Z# B

plt.plot(x,y,.r)

. Q4 C/ u/ c2 N, J

plt.xlabel(x)

2 {7 M9 k6 ]0 M

plt.ylabel(y)

: I* Y7 t) C" v6 S) s8 k' J$ W) k

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

0 T* V6 N. c% b- o2 j

nbins = 200

9 W2 _$ A+ x* w

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

2 Y& P! l2 @, g* r

# H needs to be rotated and flipped

$ i2 Y7 U5 h/ v

H = np.rot90(H)

8 I6 ^7 E U. n* c4 U' I

H = np.flipud(H)

) s& G" f3 h: B: ^3 g+ d8 {

# 将zeros mask

7 |8 D I2 G3 Z7 x' c1 a

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

6 f+ A: I7 E# @" _0 X4 y

# Plot 2D histogram using pcolor

% }0 s' Q7 g3 S

fig2 = plt.figure()

l- {4 P1 ]8 U) q+ c

plt.pcolormesh(xedges,yedges,Hmasked)

& M8 U! N: `3 A9 d' f x# k

plt.xlabel(x)

; z; D& d2 T6 j! X; X

plt.ylabel(y)

j2 Z: p6 w& }9 [' d

cbar = plt.colorbar()

. Q) F3 d( ^2 S/ H& o+ l0 [

cbar.ax.set_ylabel(Counts)

" t' ^) z- q9 M3 v5 c& l1 b

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

5 `1 K- i9 N, R7 W2 ?

plt.show()

7 O! {7 S8 C0 K+ {6 j6 w+ T1 R* i
) n9 t3 S w/ Q1 W ]
( o7 T: G# G( J1 h4 N K
打开凤凰新闻,查看更多高清图片
* A" l7 O, [3 A7 w. H
/ O) }- s' m) m
6 V. u& r: S1 ]# V5 d% K* P

; b; T1 W7 s; m6 r: r- Q4 R3 `

Example 2 :双Y轴(Python)

9 \0 s! ~. B0 B# P3 s: R

import csv

! U8 `4 M5 ~0 a+ N I1 x

import pandas as pd

3 `& X9 i, v* D [! v9 u+ r, e

import matplotlib.pyplot as plt

- w# ?/ K6 C* n5 {! g7 ^

from datetime import datetime

/ o( c/ q* I6 A5 i

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

7 {9 A( F. r$ M8 \* D& e; f

time=data[date [AST]]

: M' k) W9 w. m3 F& {& q( }

sal=data[salinity]

3 e8 {! e x' p( K! L

tem=data[temperature [C]]

7 U* |, N& }$ n' b

print(sal)

% u0 M; Z! Z9 }1 j+ O3 G

DAT = []

. E' O' U" F( O

for row in time:

) d9 B# L% |$ ^: a" N# E

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

" M; \" {5 y5 w. ?9 V

#create figure

. D! x- e h8 f Z2 e7 C* d: E

fig, ax =plt.subplots(1)

0 N0 r: a& @5 g" R' E }" Y+ {

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

3 H# |: w# {% d, ~6 g

plt.xlabel("Date [AST]")

7 e% M3 m: y; V( L0 H) |) B

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

5 L( ?# F( |( M8 B

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

- C" z/ I1 b: [, R0 m- Z

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

4 X* l# D& M0 A% }

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

: z, v0 X, E3 Y- m% K9 {' J

fig.autofmt_xdate(rotation=50)

1 B/ \; ~4 ^5 m' S) A7 Q

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

9 u& X7 m) Y: g$ ]/ V# @5 q

plt.twinx()

# M! |# T; Z3 {' [

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

9 X* p( C! v/ }7 @( Q) e

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

5 x7 a: S( D8 L$ O7 w, E9 B, ~

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

3 Q$ i' g+ `6 ]+ u( A3 g: y6 O9 y4 ?

#To save your graph

7 F X2 w+ d* V3 \- A c9 A; _

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

& y X' Z1 H J1 t" @! o5 y* i

plt.show()

7 h0 n. d o) g

. ?4 y, L+ k5 w% o/ f4 | a9 f% p8 m

Example 3:拟合曲线(Python)

6 z6 f3 a% c7 j# Q

import csv

3 [0 K# P, O7 A! _0 ]9 s. x

import numpy as np

! s5 W; y G& Z2 A) h

import pandas as pd

6 m& b" ?8 N" o6 M6 O

from datetime import datetime

* {2 Q: U5 P. ~4 l$ g7 @

import matplotlib.pyplot as plt

. k! C, H/ c* f* f4 Y

import scipy.signal as signal

+ F# d! D2 a5 i2 v) Y

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

9 L9 ~7 K ]+ [7 i" B7 G, |; W/ M3 b

time=data[date [AST]]

7 a3 t: D, ]9 M

temp=data[temperature [C]]

- e7 h$ s" ?+ h7 n* }

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

$ }1 k1 d( D: _

DATE,decday = [],[]

9 v o" Y# l% I* P! V

for row in time:

1 ?) Y/ D' N1 l" L) D2 {

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

/ v* Y# K% N" D0 w( j

DATE.append(daterow)

' J7 I1 R/ [, g

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

/ g0 B4 M& X& K- T2 a8 n; }) s) J

# First, design the Buterworth filter

5 L5 ?& H$ T9 X

N = 2 # Filter order

1 E2 p% C8 m4 {" |( v0 n! I9 @- _

Wn = 0.01 # Cutoff frequency

w5 \/ R3 ^9 L! ?

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

% S' {) N# J. G$ Z

# Second, apply the filter

9 D3 d) r- x6 e, y7 S

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

7 B1 @. i! L6 n$ q: X

# Make plots

. h3 B* e( ]# j+ q

fig = plt.figure()

( N6 g4 ]5 X7 Q$ O4 |) d

ax1 = fig.add_subplot(211)

0 R: x; v8 ]: H$ S' [+ d3 c

plt.plot(decday,temp, b-)

" W+ q# c2 x8 P. S; _( h

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

& F0 o- k6 w# c5 V! U2 k

plt.ylabel("Temperature (oC)")

: m# \) r" Y" G( J1 h6 n' `

plt.legend([Original,Filtered])

! f; \* j1 @. ~/ H7 ?3 d* P h

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

^2 h' l- c5 Q' t8 l- v! ~

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

$ t' g' R# F3 n) P0 |" J# u

ax1 = fig.add_subplot(212)

; D2 ^' m( V/ h; w7 g

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

3 Y' R: `$ C6 C% o* \# W0 H

plt.ylabel("Temperature (oC)")

& `9 g |) @4 `" I4 b& L1 e6 M

plt.xlabel("Date")

8 P T" ]# ~/ a; _( A( H

plt.legend([Residuals])

8 `; [- D" o0 V0 _9 D

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

: P, x. N. g- w1 s

plt.show()

7 u1 Q/ c. ?" o4 z; o5 m% R+ Q+ ^

7 b6 `( r- {: f1 s8 h u+ L

Example 4:三维地形(Python)

0 l+ d w# @- b2 X1 o, ^( e0 w9 A9 x

# This import registers the 3D projection

/ o8 w/ g3 I7 n5 `; b3 E$ X3 K

from mpl_toolkits.mplot3d import Axes3D

5 D( @6 f. v S' C

from matplotlib import cbook

: D" r7 q: e2 v @; r- b' r6 ]4 i

from matplotlib import cm

& N+ {4 |- K4 {) U1 W0 W: V

from matplotlib.colors import LightSource

$ I; A4 H8 o2 r

import matplotlib.pyplot as plt

6 S: F. V; a: @4 l: @: n, ]. ~# c

import numpy as np

& q& p4 n1 z& c

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

6 n' U e4 `" K

with np.load(filename) as dem:

% M" D% W" \, w$ p% L

z = dem[elevation]

# m* d8 n4 N# p- H/ b

nrows, ncols = z.shape

) O. X. E, t6 c& d7 E8 W

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

& h! x& _7 H" J) x B2 R. M

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

6 Z! W3 \; o. H# {# S& q

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

) |8 R' m5 i+ E8 A- v3 b3 J

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

0 W! [7 @8 g- e; b# n2 I

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

8 P. U/ i8 D# s

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

8 K: i' |3 {0 S" F8 W' p

ls = LightSource(270, 45)

4 z) h& e1 ?- a

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

! c, U8 o( a, D) p% i' N7 I

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

8 n" H( Z1 M" W* s

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

" P+ L. m# k. e0 o8 W! j

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

6 s7 D, T# o' L C! ?# V7 Q

plt.show()

* R7 I% X( ~ L2 P9 l) U$ K6 ~ ; P2 x4 Z4 i/ c0 o9 S

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

" y' h Y3 N) d9 B 3 _4 {* G1 S$ B

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

' v) a/ \& a R c $ h' c" ?% Q* G: F# _& b+ d8 l

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

! ]3 y7 E1 j, F2 n5 K: U6 V- c . P2 s" @* A. e' F, M

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

) {! c7 a& A+ U( G6 } p3 U# Y* ]- |$ ]3 I1 s) j- ]

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

% L: j0 D, Z8 ~* L0 E) K' t 0 I) i* m3 t& K: i7 V$ [6 M R
# h) k! X4 I# L5 | {
! I3 o$ _+ l6 U- N. Z4 H2 X2 n
% y( Z5 v- ?. S ~ ?7 _, K % n5 O4 h* Y# K( A1 S$ O7 j; i2 P1 E# p1 S p( C: {0 C- c1 L" p0 F: g. t5 L; Y$ m ( a! \5 k [6 w
回复

举报 使用道具

相关帖子

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