: T8 K+ p% W% t2 U' ]) t/ a$ a 在我们科研、工作中,将数据完美展现出来尤为重要。
% m8 y: R+ H/ H. I' z 数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
2 c4 X+ @& l; l% U! T: J$ ?- S 下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
# m# a+ ]+ O' {# z
Example 1 :散点图、密度图(Python)
7 r" G! E5 A: Q8 W
import numpy as np
3 W) W! E" A- \+ P8 v5 | import matplotlib.pyplot as plt
3 H# c8 j/ B( ?4 t! r8 O4 e& q # 创建随机数
6 V; _9 V' E1 k8 j+ r
n = 100000
% z& `; Q: K) V. P6 z# j
x = np.random.randn(n)
8 }0 y) e. K4 C0 s5 q* W
y = (1.5 * x) + np.random.randn(n)
+ v! I: H( V- S: r9 u- b
fig1 = plt.figure()
# U; G p1 L ~0 Y: X plt.plot(x,y,.r)
' e! P4 n& V* W) W6 I3 c6 Q7 T! q+ } plt.xlabel(x)
' ~; C- l5 Z+ e; F( ?) w
plt.ylabel(y)
6 N, L2 u9 n3 O& R9 R. H" |- M+ u
plt.savefig(2D_1V1.png,dpi=600)
: |( d8 F8 g, N+ `9 ]$ j3 _, Y nbins = 200
) y8 C: M2 g- `" m! c' i# o! g
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
; i0 w7 T$ e5 v
# H needs to be rotated and flipped
8 C3 d; a5 y) [) j* a
H = np.rot90(H)
" u( [9 x P: S! ]
H = np.flipud(H)
, y! p' y% Q, n8 Y # 将zeros mask
0 T& W2 p* F8 O8 C9 H- P* j+ g# s
Hmasked = np.ma.masked_where(H==0,H)
C6 t. w: s5 I4 n2 Q% j. K # Plot 2D histogram using pcolor
. }- S* W6 _' @! y# ` fig2 = plt.figure()
8 @8 i* }& v' Y* S9 ]+ s9 x- Z' Y
plt.pcolormesh(xedges,yedges,Hmasked)
. S0 s5 q: t% G6 K plt.xlabel(x)
4 A/ d" e3 A/ K2 B# W
plt.ylabel(y)
& x: J! I+ C9 v% d; ? cbar = plt.colorbar()
1 W9 {6 A' X; u cbar.ax.set_ylabel(Counts)
: r3 o) v) I7 [# @ K: }
plt.savefig(2D_2V1.png,dpi=600)
1 e; h4 S! H2 B1 l4 D3 G plt.show()
( s& I$ x7 b1 p0 C# s, r1 J3 j
+ X; z6 z6 L: o2 H* o& x e2 d U6 d6 }
打开凤凰新闻,查看更多高清图片
& Q |# U. N6 c2 j & _; _7 |& W" l- |% X
. B* G3 l' `# N. p$ G- Y$ E d9 } 
. i) C* E, j- s0 a# A Example 2 :双Y轴(Python)
) W4 f* n! y0 F9 J import csv
/ p4 `; X3 V" E/ \3 d+ U) H
import pandas as pd
) Y. ~+ [7 v! m& } import matplotlib.pyplot as plt
7 o. n0 @0 q$ Q
from datetime import datetime
: D) p& s& U- F% a3 n" L data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
0 W/ n9 V9 Y0 A) B0 P* W6 \
time=data[date [AST]]
8 j. r J) \" [' `/ c0 @* M
sal=data[salinity]
# ]" g9 B# k0 q tem=data[temperature [C]]
/ A7 c0 ^' P1 d2 |$ L3 o |7 X
print(sal)
# K9 e( w- B1 v3 ? DAT = []
7 }, J: m% r/ N for row in time:
$ F* g, M0 C9 z* q
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
3 D# J9 w* A3 h2 ?& \( u; U #create figure
$ d# h, V' D! ~/ n7 B fig, ax =plt.subplots(1)
8 e+ ?- @# P% l2 i # Plot y1 vs x in blue on the left vertical axis.
! g/ n! n8 F7 S! ^! X
plt.xlabel("Date [AST]")
8 i6 U+ j H! h% x+ L4 L5 i1 h8 j3 \ plt.ylabel("Temperature [C]", color="b")
. i) k+ j' K; c: Z$ j1 o& u q5 D
plt.tick_params(axis="y", labelcolor="b")
- s: g4 B. V8 b1 _ plt.plot(DAT, tem, "b-", linewidth=1)
# k# d. Q% _3 \& y6 n H plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
; d% M/ @1 P. N fig.autofmt_xdate(rotation=50)
& x, e4 f; w; e # Plot y2 vs x in red on the right vertical axis.
* k/ _ F7 U' h# O9 _! r0 x
plt.twinx()
/ W# b$ {2 N0 s. C, w plt.ylabel("Salinity", color="r")
0 G, [' a+ t& c
plt.tick_params(axis="y", labelcolor="r")
* C& e5 k* W- M, s; ~( A$ ?/ v) r
plt.plot(DAT, sal, "r-", linewidth=1)
3 v6 [1 v- U2 _5 i& R7 v #To save your graph
( Q+ S! O5 r* x1 o. X2 N plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
g! C1 o0 s2 r7 B; _5 d plt.show()
; \2 b9 h6 ~0 C1 P' w; s; H

9 W9 b) n( j( y ?& ~7 T- o8 Y& w
Example 3:拟合曲线(Python)
& v+ P; A/ _5 u9 c6 n _* n import csv
8 n" b2 k! A; n; x* r n
import numpy as np
' K& ~+ q$ f! @, C( L& y) l import pandas as pd
1 Z/ o/ h$ r& d8 W3 \; x7 H from datetime import datetime
) I* H* P/ n7 R! |; b0 { import matplotlib.pyplot as plt
9 w3 I- F6 k, w7 W, N( O" R import scipy.signal as signal
! j8 }/ O7 N& }8 \8 R# J3 ~0 s- E
data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
8 s W5 o2 E9 R3 R6 R( k time=data[date [AST]]
& A+ L, r% e* ]
temp=data[temperature [C]]
! h/ B4 u" h4 k. B datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
. C* V7 V5 T2 [9 v# k9 y( N DATE,decday = [],[]
. f/ Z, E# O) i) f {8 o" N
for row in time:
, B. ?( d4 z& a, a: F& Q: K
daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
. }+ `8 R [9 l, n0 u6 i3 P DATE.append(daterow)
/ D% t- I$ C( {# P6 X( A
decday.append((daterow-datestart).total_seconds()/(3600*24))
6 M$ z% ]2 y) f& r' }
# First, design the Buterworth filter
1 d! h5 e0 R3 `
N = 2 # Filter order
- Z5 e! L/ c6 h; }% `; z
Wn = 0.01 # Cutoff frequency
r. c/ \2 S9 Y* C
B, A = signal.butter(N, Wn, output=ba)
! t: J! h: L3 Y7 z # Second, apply the filter
$ U( G( v! Y4 r$ F; I1 j tempf = signal.filtfilt(B,A, temp)
% I$ a: \7 I# q5 i! n Q( N9 a
# Make plots
7 }4 D/ u8 N) z+ M fig = plt.figure()
3 ] T# X6 C1 F
ax1 = fig.add_subplot(211)
: w" b, r# C9 K+ S2 c& c
plt.plot(decday,temp, b-)
i3 _& H4 U2 S( V8 t. o plt.plot(decday,tempf, r-,linewidth=2)
) n: i4 \* _% C8 k; S% ]4 |" } plt.ylabel("Temperature (oC)")
4 M9 O# t. R" ]$ N( V% B plt.legend([Original,Filtered])
: c7 Q' B0 F# d% ~
plt.title("Temperature from LOBO (Halifax, Canada)")
% |, x7 E% X+ `: I4 h; h ax1.axes.get_xaxis().set_visible(False)
% m6 {' L! B+ r8 n ax1 = fig.add_subplot(212)
" f, x) r7 m( z6 M0 v7 b" r% R
plt.plot(decday,temp-tempf, b-)
) q9 ^8 ^2 `# I& f
plt.ylabel("Temperature (oC)")
1 v0 R/ _1 i. E; _ plt.xlabel("Date")
/ r$ a: o: T9 r* w! S0 Y0 j2 W S
plt.legend([Residuals])
7 e+ A# b- L1 S2 d2 M- ^( E plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
) `+ Y% Y2 Z; Q! }4 W5 G: q plt.show()
" I# U& [( _4 E3 B0 g9 f/ l: p( z! ` 
4 T/ o3 e. z7 c6 p8 ^! N
Example 4:三维地形(Python)
, j5 c& o7 ^) N" C6 o- {1 T
# This import registers the 3D projection
7 {$ A8 C% N! j( V6 y( } from mpl_toolkits.mplot3d import Axes3D
$ c5 J" @: \* l, U9 o+ B from matplotlib import cbook
8 a8 d0 L6 I2 k' ~! y, u* T: o from matplotlib import cm
: L3 d, ^4 ?4 h3 u from matplotlib.colors import LightSource
/ u5 f" D8 r9 L6 r" A O0 Z import matplotlib.pyplot as plt
* i I7 u; x1 ?8 @% D* J
import numpy as np
* ^" t3 g# s9 S! l0 f1 P
filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
4 N! t1 m; {0 j. u( d with np.load(filename) as dem:
! o2 _$ W2 M E2 j7 a" Z, C
z = dem[elevation]
& J& @% f a7 a
nrows, ncols = z.shape
/ v5 g1 Y) G! k x = np.linspace(dem[xmin], dem[xmax], ncols)
! H! ^/ T5 c9 ?
y = np.linspace(dem[ymin], dem[ymax], nrows)
' J8 ?7 j% t: j$ x4 q x, y = np.meshgrid(x, y)
5 v6 D9 ?5 f- Y$ T
region = np.s_[5:50, 5:50]
2 F X5 P) ]5 Q5 f/ B x, y, z = x[region], y[region], z[region]
1 s+ s) k% J1 L4 z: l- T( [6 D6 R
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
- J4 {9 K( I U" S0 R ls = LightSource(270, 45)
0 H' W$ c% z5 p' G a
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
- b3 d$ m# C* P1 l1 z- j. g* r( F
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
+ C5 f7 `) R% E) {" }# ~/ p
linewidth=0, antialiased=False, shade=False)
+ p4 F6 S* U% |8 m; h plt.savefig(example4.png,dpi=600, bbox_inches=tight)
8 ?' a% A" h! H5 a1 R# k% u
plt.show()
# a2 W" Q! M% x8 S5 U( v ; q. O: J+ {% ]! W; c
Example 5:三维地形,包含投影(Python)
3 P6 p$ {) G- @5 o$ P) G! ~
% L2 U6 S, s% i2 a/ Y" w2 p Example 6:切片,多维数据同时展现(Python)
" r! {) `1 H0 B' C( t7 m. f& Z$ K/ ^
% y$ x L$ r% H7 q/ b- p Example 7:SSH GIF 动图展现(Matlab)
8 h3 p1 }. N0 ]* F7 C9 {8 M* L 1 ], j7 E9 ]0 Z
Example 8:Glider GIF 动图展现(Python)
* {/ A+ G5 j1 m8 n' N6 r
1 H# k+ [- b3 K: U* B# A
Example 9:涡度追踪 GIF 动图展现
9 E+ ^& m1 D' }
. O! k: [5 }# `) c- k. `- c