* B# X. S3 p, R% Z
在我们科研、工作中,将数据完美展现出来尤为重要。
1 M* J" {, k0 ?8 z
数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
+ \8 T& [: f! g _' i
下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
. v" p. ` q. X5 O/ Y. E/ H, H5 A9 X
Example 1 :散点图、密度图(Python)
' t/ M2 g5 ]- O6 \
import numpy as np
+ N( o5 G6 h% q) U, i% \& g import matplotlib.pyplot as plt
' u5 K" f, z$ c8 b
# 创建随机数
2 v+ k9 f7 V/ p' F$ _3 _+ M8 N( v4 v# b n = 100000
; ^3 }2 a' K3 }# \5 V* ~0 J% m x = np.random.randn(n)
+ g+ S' c% V3 G: j$ O y = (1.5 * x) + np.random.randn(n)
& [. ], _% e6 V @# H3 R
fig1 = plt.figure()
) d6 V% q' B6 \* F& Y: W5 v
plt.plot(x,y,.r)
8 j6 B' W- y0 \' c) u4 I
plt.xlabel(x)
/ D1 y# Q9 Q5 Q- @" j plt.ylabel(y)
1 g2 T+ m/ y s plt.savefig(2D_1V1.png,dpi=600)
) F5 g. M ^ E8 s
nbins = 200
( J( d. w1 b, @7 C0 [5 W
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
8 d; a& }. j% r9 \ n/ a # H needs to be rotated and flipped
^) M( ^/ y6 T# k H = np.rot90(H)
, e3 a! t& ?3 {6 Y4 F H = np.flipud(H)
7 U1 t3 ~, J# I* U
# 将zeros mask
; ]) {, Y9 C, M" @. z/ c8 E+ h' q
Hmasked = np.ma.masked_where(H==0,H)
$ ]3 R9 N7 {; U5 N3 x: E- |9 [
# Plot 2D histogram using pcolor
, j/ E3 g2 ]! {/ v l _( U
fig2 = plt.figure()
& `- n" N6 g7 @6 C+ t6 p plt.pcolormesh(xedges,yedges,Hmasked)
& k* J1 q; L1 b' i# t
plt.xlabel(x)
6 q4 n7 u) O" c2 D' P
plt.ylabel(y)
# e, W" w9 {& l' P& p4 F% h cbar = plt.colorbar()
3 T9 _" v: @* }7 \ cbar.ax.set_ylabel(Counts)
. ?9 Y Q, ^$ C- ^3 i5 Q plt.savefig(2D_2V1.png,dpi=600)
; i9 y* k0 H2 E; l# x9 d. }* z/ r
plt.show()
0 ?8 _* D- ^( s3 @5 W
; Y' v1 F) R$ U5 W4 J! _ - O; O2 E) ?6 a& ~. L% c6 t$ h
打开凤凰新闻,查看更多高清图片
' a/ e. L! m& u9 {0 @) C
* z& [2 J9 G( t: u+ @ 3 E6 U& L" E5 m0 s8 S

! s5 M A3 k7 _" p) x
Example 2 :双Y轴(Python)
* t4 v4 Y0 N8 m. j6 ?2 m import csv
& E6 S4 Q1 V" f8 G, ?( R1 H2 C; Q. X
import pandas as pd
! t' b* w' ?2 w6 Z
import matplotlib.pyplot as plt
+ m) n+ `$ r8 |' s, _' z" C from datetime import datetime
1 r/ E i; h7 m% Z6 T data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
1 L* h6 P( F2 x# H5 B2 n* P
time=data[date [AST]]
* k1 p ~4 I" B* j l' D
sal=data[salinity]
?- p& x0 _) a6 v' G0 ~7 C# d
tem=data[temperature [C]]
. `- N5 T+ P: r print(sal)
! @) }' F( ~2 H& x; s1 j& E
DAT = []
7 u1 Y( v8 x/ {/ Z. t l" H
for row in time:
/ U& v9 `2 p" C( u
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
/ U* N& ~8 [* ` #create figure
7 i5 \" B0 q$ r6 q
fig, ax =plt.subplots(1)
8 i; F$ _/ A$ o6 F' \+ v. U # Plot y1 vs x in blue on the left vertical axis.
) h5 p" K1 d5 Y" ^ plt.xlabel("Date [AST]")
" ^9 l1 s- D( C" t5 \# y9 ^ plt.ylabel("Temperature [C]", color="b")
2 f4 f7 ~3 K1 J5 W. m) `
plt.tick_params(axis="y", labelcolor="b")
# f; A/ Y& H2 ]- h+ o% G
plt.plot(DAT, tem, "b-", linewidth=1)
9 J; H% I' Y4 Z5 _, r
plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
1 t" D# h) }9 p
fig.autofmt_xdate(rotation=50)
+ e. o7 t, ]4 w C! N2 r
# Plot y2 vs x in red on the right vertical axis.
4 v- I. W- [9 r0 I plt.twinx()
5 O# M, Y9 _0 q, G/ k3 [+ R plt.ylabel("Salinity", color="r")
( `5 ~( ~& d+ J9 N5 h4 M6 F plt.tick_params(axis="y", labelcolor="r")
# k1 z4 y9 W+ ^" A+ M- U: J
plt.plot(DAT, sal, "r-", linewidth=1)
" z" ?7 N4 X+ ^) `" C+ h #To save your graph
0 K7 W, t: {3 V1 f& {$ I$ H
plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
0 ?7 S# ]5 P9 Z( I& S2 k plt.show()
- e! U7 J2 g# F& L5 o. \ ?5 g& ^ 
2 ]$ R0 e2 l. L* \$ e Example 3:拟合曲线(Python)
, ]5 g5 A2 S; K& F
import csv
& l( S* q7 S- x$ Z import numpy as np
) g0 D( Z) y: O1 c
import pandas as pd
( y; H! m- e6 g k" I7 R# G from datetime import datetime
2 W+ L. _- k1 U }, }
import matplotlib.pyplot as plt
* ^. B4 C9 k) Z' C( f1 Z- Y
import scipy.signal as signal
2 x( h) B m) }5 J data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
0 i6 x) {- ?$ N% N- L time=data[date [AST]]
- A6 j) h3 l0 i, r% i1 z
temp=data[temperature [C]]
, W1 j1 z4 X2 b9 v& J
datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
" L9 ` U9 _) m" A, _: Y
DATE,decday = [],[]
W; J* X- j' _" ~! k4 G4 E2 B( q4 M) K for row in time:
' T( X! P+ J4 Q; |9 ^& T, m) s daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
! W% k: ?- b3 f& I( Y3 Z
DATE.append(daterow)
( {( K& t/ u" B9 C+ _9 W
decday.append((daterow-datestart).total_seconds()/(3600*24))
1 x% ]) t7 {' |) z. |- X2 l5 d
# First, design the Buterworth filter
+ [# ]3 {! v! ~; M; q* e' b N = 2 # Filter order
# K" q" p) b/ |: h" y6 X/ q
Wn = 0.01 # Cutoff frequency
! [2 @ _) l4 a5 f6 `7 ~ B, A = signal.butter(N, Wn, output=ba)
/ p4 k6 b. j+ K4 I; \ n
# Second, apply the filter
: c' @" \- o) e
tempf = signal.filtfilt(B,A, temp)
4 M. }: u3 J- d) f' [( f
# Make plots
9 [8 E% L+ x8 _8 A$ U fig = plt.figure()
$ J! I! q9 T0 w4 [& C4 s& Z8 O
ax1 = fig.add_subplot(211)
, e- c7 }, \' k
plt.plot(decday,temp, b-)
! L/ k& S/ |: I( S n
plt.plot(decday,tempf, r-,linewidth=2)
7 m; w6 P# h4 X- S5 B& I' M
plt.ylabel("Temperature (oC)")
4 Q3 r1 x" g1 F: I. w& ~ plt.legend([Original,Filtered])
4 O m+ h6 P" K; P/ w* x% o plt.title("Temperature from LOBO (Halifax, Canada)")
$ ?3 x. [; Y/ b/ D8 p
ax1.axes.get_xaxis().set_visible(False)
- i) C! ?; ~& p1 z
ax1 = fig.add_subplot(212)
+ D: p0 T2 X: Q6 B. t
plt.plot(decday,temp-tempf, b-)
7 N7 s" f6 Q/ _! |# e- T) U- s8 x
plt.ylabel("Temperature (oC)")
8 |: K5 i( _1 c+ T
plt.xlabel("Date")
. V! `9 C& J9 F3 e* I3 K
plt.legend([Residuals])
9 e/ x: f# @# }4 [, B plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
' j# w& P4 U! p$ m, Y# N
plt.show()
9 g: P. g! m2 l4 M/ @

, ^0 P+ x; v# p! f
Example 4:三维地形(Python)
/ e$ b% a) W9 g9 ~: g! n$ z! j # This import registers the 3D projection
, Z2 X& k# g3 V1 [+ Y' L
from mpl_toolkits.mplot3d import Axes3D
! [1 ~. ~6 h+ p( _8 f
from matplotlib import cbook
, Z8 g* D4 r* j; {9 H' V1 o7 a
from matplotlib import cm
' Y* C) s) m a( n: p3 X
from matplotlib.colors import LightSource
; z( Y- Z' G: Y+ e" | import matplotlib.pyplot as plt
% v4 b! E. d! g& M' m J
import numpy as np
& n2 k/ E6 `' `9 C0 B. J' A) q filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
8 o4 O. Y# }1 M! |( g$ {( ~ with np.load(filename) as dem:
' D* _1 s- E- ?9 t- V z = dem[elevation]
4 u. S/ z$ T$ ^2 C
nrows, ncols = z.shape
2 E: Z% j, ?) X s! i, U W
x = np.linspace(dem[xmin], dem[xmax], ncols)
- w* N4 x4 t3 u# M7 V! I% j2 P/ ? y = np.linspace(dem[ymin], dem[ymax], nrows)
( i+ X* F9 b: g# b1 P: T
x, y = np.meshgrid(x, y)
' v5 {2 e5 c0 {. ]
region = np.s_[5:50, 5:50]
( u2 h, x w B5 _& E E2 Z" b- _
x, y, z = x[region], y[region], z[region]
, f- r, l8 ~7 O: a$ W: N fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
4 h9 G4 U% k, B9 ]+ \$ }4 _- r; d/ V
ls = LightSource(270, 45)
4 ?- c. s6 F# Y( [& C
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
: P0 P! ~8 {* h/ ^5 G' j surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
; }7 r: x2 D. K: ?* `4 Q: i9 @
linewidth=0, antialiased=False, shade=False)
7 U3 E- m& Y; v, @( ~: _ plt.savefig(example4.png,dpi=600, bbox_inches=tight)
, i$ H$ F- U, L& Z) n: g" t4 x5 j
plt.show()
. w" N0 E5 T: b
* i5 Z. t& @3 Z Example 5:三维地形,包含投影(Python)
7 u* F# l0 @5 _& x
3 I6 C- t6 Y5 n6 k Example 6:切片,多维数据同时展现(Python)
* q D4 s1 v; P8 C6 P. C
T2 c6 J$ d* M Example 7:SSH GIF 动图展现(Matlab)
# X( C+ P4 z, O
! R" X0 j7 D2 l7 i' Q3 C
Example 8:Glider GIF 动图展现(Python)
( w0 t5 d Q. e# o5 r
3 n8 F- E8 p& e" E, {. Q+ @ j
Example 9:涡度追踪 GIF 动图展现
$ u! U* b* |3 N- O* i
P: ]! E- w& N2 v