; |9 C% W7 x( I; M1 | 在我们科研、工作中,将数据完美展现出来尤为重要。
3 T, k6 W, f, @- g: h 数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
* v+ R5 \% _8 d- n8 s! S/ _: } 下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
' A! X4 [+ u3 Y1 N$ w1 L
Example 1 :散点图、密度图(Python)
& p. i- g$ y$ {" R1 O1 I import numpy as np
. j2 }( Y! r+ S* y5 i
import matplotlib.pyplot as plt
( N) P. R, U/ X" Y. f1 i
# 创建随机数
( V @2 j6 J7 A( @, P' {3 ?( t
n = 100000
S; U6 A* R8 z& X: m O; M4 t x = np.random.randn(n)
- u- m( A5 W) q$ X* l
y = (1.5 * x) + np.random.randn(n)
, `9 }) R! j( _ |
fig1 = plt.figure()
8 z0 d4 E+ @& d$ G2 A
plt.plot(x,y,.r)
9 e! I* G9 b+ x2 F9 _3 V
plt.xlabel(x)
5 H7 ?& f# O- g
plt.ylabel(y)
; B9 t: e! O# c$ ?7 M/ A9 x- a$ u$ _
plt.savefig(2D_1V1.png,dpi=600)
1 a: T M" y( s. Q2 t1 }
nbins = 200
. m* E* M; U) p. D; W' q/ G
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
/ V, N& f9 g& H g% Q# N
# H needs to be rotated and flipped
6 N5 r- v' Z( U& P, S/ M H = np.rot90(H)
5 r( g8 f9 p( ]" Z, t1 J8 Q' k
H = np.flipud(H)
" b$ t, e: ^* W7 I; ^ # 将zeros mask
5 Q9 {; I0 k& m' C0 Y( Q5 [1 @ Hmasked = np.ma.masked_where(H==0,H)
% Z1 ~2 I- R2 l0 | x/ t
# Plot 2D histogram using pcolor
% d) z3 r; x& m6 |7 Z fig2 = plt.figure()
$ b5 v- Y' f3 G/ k: A# n
plt.pcolormesh(xedges,yedges,Hmasked)
/ S) D: s' ]* ~: e) B) @ plt.xlabel(x)
, L9 G3 ?. l1 C' C plt.ylabel(y)
; R* d, L+ a/ n- X5 n- U
cbar = plt.colorbar()
, _2 @. K$ |3 }1 J0 F' A
cbar.ax.set_ylabel(Counts)
1 \ @. Q# s% \9 i1 ?5 u. g
plt.savefig(2D_2V1.png,dpi=600)
$ s0 e$ Q! Q* C# a0 i plt.show()
4 m m0 Z& |" y: B6 C
5 l8 c7 m" q8 V5 b) v
1 |5 _, t7 B4 ^3 G% _! ~
打开凤凰新闻,查看更多高清图片
6 a, v1 _0 q7 p* V/ n
, M% q9 i4 i# O$ Z
0 }, o. c) S& p7 d% h6 H 
& L! Q1 h0 y2 ]! A% }1 O
Example 2 :双Y轴(Python)
& b8 g$ o/ Q0 v" R% L# ]3 v import csv
- h3 y" f* r" M6 q import pandas as pd
- O2 V$ }. T' g' i p7 L
import matplotlib.pyplot as plt
+ M6 |! U+ W' K- h v# y1 } from datetime import datetime
: \' g3 E# j! r H1 {2 q/ a% @2 u data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
6 g. `& C) X. ]7 M
time=data[date [AST]]
B) B% R1 o4 W' T5 I7 h sal=data[salinity]
' p7 M; y* j! D3 y7 l3 I tem=data[temperature [C]]
7 {9 O4 N( m4 m8 G- e print(sal)
9 C9 r5 v7 O: ^; v5 \0 S: W DAT = []
# z1 L- u: T- E9 i5 }
for row in time:
. O7 A P3 V& X
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
7 }9 y. o8 q' M4 ~5 L( W
#create figure
; z2 B' a% ?% R1 X. K' x+ i fig, ax =plt.subplots(1)
4 Q7 T, ^6 F# D; O# f8 K, a" k* b( S # Plot y1 vs x in blue on the left vertical axis.
& I, m0 a: W$ O plt.xlabel("Date [AST]")
. Z( s4 ~ I: P, d3 P3 |2 m plt.ylabel("Temperature [C]", color="b")
) k. W. G% |9 Z% N, p
plt.tick_params(axis="y", labelcolor="b")
2 S' k! a: n+ Y2 h% E. h plt.plot(DAT, tem, "b-", linewidth=1)
8 n6 X- h6 s. f0 R' S plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
8 A/ t7 Z. _% h5 t) X& v
fig.autofmt_xdate(rotation=50)
! ~# c( T. \5 O& o
# Plot y2 vs x in red on the right vertical axis.
' d8 j0 a1 V m' y" ?) ]7 Q- X plt.twinx()
. B/ x; n: z, i plt.ylabel("Salinity", color="r")
7 ?# w( [/ |1 ~0 ^) W$ a. R$ O plt.tick_params(axis="y", labelcolor="r")
# w: m6 p* r4 W/ \0 t: c; U4 E
plt.plot(DAT, sal, "r-", linewidth=1)
7 e+ I/ g7 ?* ~2 m# O$ i! |7 ?0 ] #To save your graph
g: P W+ }+ }2 x2 | plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
7 g, }- I2 A/ G( T
plt.show()
& H5 w% r: r% [' r4 x" `3 k6 [

) h" Q. D! |* _' a8 s
Example 3:拟合曲线(Python)
1 p! a% o9 E2 o. } h
import csv
& V5 ]) W# p0 R; k$ ]" {; c9 C. o
import numpy as np
- C% N, G, U$ \2 p G
import pandas as pd
8 P. m1 C% ]* E6 |% A
from datetime import datetime
9 J; n3 \- V3 D1 J: Q import matplotlib.pyplot as plt
! w& C8 P* a6 Q import scipy.signal as signal
0 I6 a& a" d: W6 ]$ P6 n
data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
. u+ Y1 d3 y& u' y time=data[date [AST]]
g, Q) D( P. L! m+ ~3 }6 Q
temp=data[temperature [C]]
" {$ L7 D* e+ U0 t. A8 l
datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
! o$ N; B: N) ~8 w
DATE,decday = [],[]
+ U, t4 Y* o4 z$ F for row in time:
: X. h. a! ?6 X5 y% P daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
" a8 r3 H5 G5 d$ C; y
DATE.append(daterow)
6 X, D H5 z8 c6 F& @6 D9 b: [
decday.append((daterow-datestart).total_seconds()/(3600*24))
/ U5 x/ a/ _5 P3 Y) p6 ^ # First, design the Buterworth filter
. b+ o& x; {; T# v% V0 r" { N = 2 # Filter order
0 S1 w6 S) t4 `' C2 K! w+ c. [ Wn = 0.01 # Cutoff frequency
- V' i) Q0 l Q% W B, A = signal.butter(N, Wn, output=ba)
$ t7 A7 w. m* a+ C3 U. ]3 H- Q
# Second, apply the filter
1 X3 y8 K" S% R6 Z { tempf = signal.filtfilt(B,A, temp)
3 m" ?% J3 c: ]5 D9 U4 z3 X
# Make plots
& A9 @, q$ C) R/ t3 u: w# c' ~
fig = plt.figure()
' b: \2 r+ _# y \, X3 I ax1 = fig.add_subplot(211)
& v2 v+ e5 _: [* k( @ plt.plot(decday,temp, b-)
a. R9 f" D# `( _# B
plt.plot(decday,tempf, r-,linewidth=2)
# }6 n9 x* c$ U+ P) o7 c8 B plt.ylabel("Temperature (oC)")
9 J2 H9 w+ |9 V+ z7 ? plt.legend([Original,Filtered])
3 e6 y( M9 d: b$ w$ R! L
plt.title("Temperature from LOBO (Halifax, Canada)")
& ^# q: \" P6 J/ M" q3 y ax1.axes.get_xaxis().set_visible(False)
, }/ q8 R/ _& ~3 y; M ~
ax1 = fig.add_subplot(212)
1 ?* b6 B+ M/ u' h* x- O1 j! P
plt.plot(decday,temp-tempf, b-)
) [! ]& L4 T! Q0 S plt.ylabel("Temperature (oC)")
- z U$ a' Q5 F" _0 u' @! J
plt.xlabel("Date")
: m& U" O* z7 y* |$ Y+ J+ W3 g; J
plt.legend([Residuals])
5 T6 B* ?9 b3 p: q! z4 L& U* A plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
$ R) E9 L: S8 |# G6 |
plt.show()
5 c1 j/ X8 v. y1 Z) U4 | 
% Q" [/ p' m, d0 W" g4 _. q
Example 4:三维地形(Python)
! y* D% A0 A6 I- E # This import registers the 3D projection
' i3 g9 F1 _3 \' `- v$ p6 V- b* w from mpl_toolkits.mplot3d import Axes3D
' j+ `( e7 P+ l+ n6 u5 W from matplotlib import cbook
t: G& {: g( y: B7 f
from matplotlib import cm
! Y/ f3 l z: v4 M3 u! b
from matplotlib.colors import LightSource
) G! N$ T! H: @ import matplotlib.pyplot as plt
8 b& w& t& B) ?4 s
import numpy as np
4 S# X$ B+ Q/ t$ y5 j0 v# s2 ]3 a filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
k; {) ?/ _+ S) a$ }) s- Q
with np.load(filename) as dem:
& ?- N D6 V, x8 O3 j9 M z = dem[elevation]
$ c( b: ?; J2 m% Z" ?1 P nrows, ncols = z.shape
. x: e" R( M# W) K1 u+ B! N F r x = np.linspace(dem[xmin], dem[xmax], ncols)
5 L+ D2 c2 H$ T y = np.linspace(dem[ymin], dem[ymax], nrows)
% C6 F, m% B3 k& O4 W x, y = np.meshgrid(x, y)
0 T) g G2 K5 R region = np.s_[5:50, 5:50]
) I- M) \4 Z" h5 y
x, y, z = x[region], y[region], z[region]
; L; k' b2 g9 k& w; H. d
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
( m9 i+ ?: k0 Q8 L- W8 S ls = LightSource(270, 45)
2 I2 d; b8 i i0 C5 l* w rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
, @/ p0 I( e8 d* S/ d$ U& I: h. T0 ^
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
5 D5 q6 z) E! \* s/ [. z n8 e/ z, O
linewidth=0, antialiased=False, shade=False)
- w( z) V+ A6 u8 A" w) e. X& ] plt.savefig(example4.png,dpi=600, bbox_inches=tight)
4 W( a/ B# z5 _: W* u plt.show()
# [! A7 c2 `, G$ C" n" c" O
2 I/ D0 }7 e! M( J+ L Example 5:三维地形,包含投影(Python)
, d/ H, V6 F9 r; Y
) q0 {# w! }, S1 j. j- v. r Example 6:切片,多维数据同时展现(Python)
! W+ z. H% _! _$ i ( Q, V. ?8 n/ e9 y/ h8 ~/ D
Example 7:SSH GIF 动图展现(Matlab)
8 Z* D, p0 S4 S- s! m
' u( o* |9 L* o
Example 8:Glider GIF 动图展现(Python)
; m7 A) F/ C6 D4 k
5 I7 ?" |) I; o+ P$ ]8 [4 `. ~
Example 9:涡度追踪 GIF 动图展现
7 |; o8 s/ }* ^1 _! e: f
3 v" q, X# O) c' z, a x* C6 k