7 q; Z- |. s% W* \( U& Z/ D 在我们科研、工作中,将数据完美展现出来尤为重要。
! @& U3 {7 O! ]# J 数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
2 ?! @1 {3 j4 R, N6 |0 B5 m8 f- Y
下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
7 B& Y4 w3 P7 b Example 1 :散点图、密度图(Python)
' y d6 @. r, z" t' r- w& y" d
import numpy as np
3 t0 v7 \9 i2 `) H5 D B. y' X import matplotlib.pyplot as plt
" p1 v1 h( U# i, C # 创建随机数
9 ~3 }0 G& G' t* j; j/ D( e1 c% B2 J" ~
n = 100000
& D9 l. V( b( E( C1 X! s5 \
x = np.random.randn(n)
4 f c! H& _7 @4 S2 a4 ~ y = (1.5 * x) + np.random.randn(n)
) E( D- H0 W7 G/ ~4 n
fig1 = plt.figure()
8 v2 [9 Y- F; }! e, c1 ], g3 L
plt.plot(x,y,.r)
3 u+ k. U0 J7 }; C plt.xlabel(x)
7 E) v) l, H. k1 `# M( w plt.ylabel(y)
1 [0 X5 q( k0 D$ f9 v& j) W
plt.savefig(2D_1V1.png,dpi=600)
0 c9 `" f! w, G% U
nbins = 200
; E4 ^! [/ ~3 p* `: d' C" t$ E$ s H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
0 q- s( i4 H: q # H needs to be rotated and flipped
) B l& [, S% O$ |, s
H = np.rot90(H)
' y& a! q' \) w1 ^, Q! W4 ^3 P H = np.flipud(H)
7 e8 {* v' C1 k' d0 H/ B # 将zeros mask
' S5 u% e* z$ T1 d Hmasked = np.ma.masked_where(H==0,H)
; V# p2 m3 q! d/ E
# Plot 2D histogram using pcolor
6 R. h" v3 v/ I/ ]& G- i& G% x( { fig2 = plt.figure()
2 x% l% [$ }/ y6 `6 {5 w3 A plt.pcolormesh(xedges,yedges,Hmasked)
4 |! e% w% O# T2 X+ ~ plt.xlabel(x)
2 t5 @7 T% T0 s plt.ylabel(y)
, I( L% \7 V$ S
cbar = plt.colorbar()
! ?6 ]' D' E# u. N/ g' _* P& @
cbar.ax.set_ylabel(Counts)
. o& d1 Z" [, t. n3 W plt.savefig(2D_2V1.png,dpi=600)
, G! W* G/ v. Z Z& b plt.show()
; _, c. D* C5 C F* O) N" j
4 U7 h) t6 f3 @% d
6 W- X6 W; i' Y3 c( T 打开凤凰新闻,查看更多高清图片
0 h/ Y6 I5 \: r4 H. \. |
6 T0 @. e6 @) [& N& ^
. |0 F3 B; M* }2 C 
, a$ Y) l& g8 `0 |4 V# O9 T" D
Example 2 :双Y轴(Python)
0 k; _- P' X/ W$ |+ Q- S
import csv
9 n' u E: o# M$ V) J; b* o import pandas as pd
8 y I/ M: [& `5 x x( c import matplotlib.pyplot as plt
6 }. r2 c' ~: ?; Q
from datetime import datetime
( B- l' J3 _9 Y) y& i0 R6 h data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
+ A, g' V" G7 B4 Z time=data[date [AST]]
: C w e0 y/ r: g7 l3 q5 C# |
sal=data[salinity]
) y* s5 I% s& g; N- c& ?1 F
tem=data[temperature [C]]
1 K m& x0 z! K& b# h. ]% e
print(sal)
3 I; v% o7 e7 Z# F8 R9 c e
DAT = []
1 W, A$ N9 ]. P
for row in time:
- S( \1 K- y4 E9 T' G
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
2 l' L1 d: F8 W. D( Z0 O7 G #create figure
0 m0 ?( t. F5 z* k# g! G" X1 U# R
fig, ax =plt.subplots(1)
1 E) f" ~% i5 ~9 u& \0 Y8 m
# Plot y1 vs x in blue on the left vertical axis.
6 ~- c4 t2 x9 z6 w6 j0 Y, T plt.xlabel("Date [AST]")
# P+ \. O4 ]! n b9 n8 R plt.ylabel("Temperature [C]", color="b")
4 R# H/ l0 j$ A# D* V) w plt.tick_params(axis="y", labelcolor="b")
5 m2 k5 W/ i. x6 M) G7 x plt.plot(DAT, tem, "b-", linewidth=1)
% c0 f3 {' D3 |( e plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
# @4 v- Y2 @1 ]' j B fig.autofmt_xdate(rotation=50)
6 r& @7 o I8 t$ \ # Plot y2 vs x in red on the right vertical axis.
# a, L" |' R$ C# w
plt.twinx()
5 b/ B( e+ `+ o) q
plt.ylabel("Salinity", color="r")
% Y- V$ G! f. o7 B+ j# r
plt.tick_params(axis="y", labelcolor="r")
; [( E& } B0 F. b5 B% E7 i plt.plot(DAT, sal, "r-", linewidth=1)
C& @2 ]# Q8 n5 Z$ B; n #To save your graph
7 q/ r! k' Z- n plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
! t; B9 i5 V7 f$ b0 C" h5 S U7 v
plt.show()
6 Z. i/ o0 o+ A9 t# J

2 Z9 D+ c+ G" @3 Z* m
Example 3:拟合曲线(Python)
8 o( n- R$ s3 V( [ import csv
& P |3 Y: {4 e0 S& a7 {1 ^9 w import numpy as np
1 ^! P' f' J* o E7 H import pandas as pd
( n# _5 F$ v' J" @& \) v, G
from datetime import datetime
& _: E9 I6 x# N9 y1 ]) M7 g import matplotlib.pyplot as plt
. B( y O# P) p1 S2 [ import scipy.signal as signal
5 h8 G/ V1 L9 c& c. w% W data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
0 X+ k) {7 e5 i
time=data[date [AST]]
0 b8 R. b: K! X7 U
temp=data[temperature [C]]
! T. J; @4 H# l, M
datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
8 d8 B/ V6 j& C, O1 Y( t: ]
DATE,decday = [],[]
6 i* W0 T* K+ k# ^ for row in time:
) ~8 s7 @! l8 W$ k6 d6 U: B
daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
5 X& x% K7 t, V* M3 U L
DATE.append(daterow)
& J' A- y5 C5 h2 y3 Q8 ^+ @
decday.append((daterow-datestart).total_seconds()/(3600*24))
; b1 h1 K* p, L$ i; K
# First, design the Buterworth filter
U! E& E2 l& |9 i$ P0 Y N = 2 # Filter order
& F9 ^; a- ^9 ~* W( [
Wn = 0.01 # Cutoff frequency
$ s& w# n4 M3 S/ t7 o6 `$ y
B, A = signal.butter(N, Wn, output=ba)
- z8 s& x+ p: U # Second, apply the filter
# _, V+ _5 p4 a! B# l% j0 i$ i7 [$ P tempf = signal.filtfilt(B,A, temp)
7 ?: c; p/ O! |1 z- q # Make plots
P8 B$ z4 c j1 O" M' K7 e7 w
fig = plt.figure()
) F; ^$ h( T" l" S# t3 K ax1 = fig.add_subplot(211)
7 d5 b/ P. |. d) J& C plt.plot(decday,temp, b-)
6 a, z. b" |: c4 ?' w9 [9 ~ plt.plot(decday,tempf, r-,linewidth=2)
2 E4 @4 `- t7 H plt.ylabel("Temperature (oC)")
! G5 b9 J% E; ~) I9 H0 A plt.legend([Original,Filtered])
2 u+ C& Q! B1 g$ {' q/ A: j plt.title("Temperature from LOBO (Halifax, Canada)")
* q) T, H2 g6 t; @! r
ax1.axes.get_xaxis().set_visible(False)
# h: W5 D6 h! l( b" _5 o ax1 = fig.add_subplot(212)
* Q ^" s3 D1 U6 L' u) ~* p" o- f plt.plot(decday,temp-tempf, b-)
. ]* Y: I/ {( Z1 p2 |# a
plt.ylabel("Temperature (oC)")
) o# n! N) H* B |; t4 T
plt.xlabel("Date")
" ?: k! w! r1 u) H- R5 V plt.legend([Residuals])
2 L) K8 S7 K. f0 S# A7 l' \3 i1 u plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
) y+ L5 p6 e: y9 H7 m) _ plt.show()
. _" Z- u# X7 x. c4 W. A( i 
! A! ]: }) I' m! r7 `4 n Example 4:三维地形(Python)
' [; g* n! g/ Z i: ~( q
# This import registers the 3D projection
2 e6 ^7 u. x- c! J
from mpl_toolkits.mplot3d import Axes3D
, M* a; g! |4 J; ?* @$ G/ g4 t from matplotlib import cbook
/ p% P) ]( V9 G, k, e from matplotlib import cm
$ B2 T: D: X( a; y/ E7 j1 v1 _
from matplotlib.colors import LightSource
# y# B6 m d @8 w- k) X- w
import matplotlib.pyplot as plt
6 u# ?+ U9 {1 _ _% l) W import numpy as np
: H! C5 ^8 w5 L$ o filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
8 i: e+ `/ L$ b! g |
with np.load(filename) as dem:
+ n# r/ m: u! S. e, K: n
z = dem[elevation]
* I) @2 \) {' F2 Q5 W8 e" M
nrows, ncols = z.shape
1 r0 f2 _! q0 \
x = np.linspace(dem[xmin], dem[xmax], ncols)
, N( b; t' Z) _ d0 h y = np.linspace(dem[ymin], dem[ymax], nrows)
% L9 r4 w9 O6 `( e/ i* m x, y = np.meshgrid(x, y)
& Q! H1 I5 u! R. H9 d7 l# a region = np.s_[5:50, 5:50]
# A6 o; T% b( x" C) ]/ O x, y, z = x[region], y[region], z[region]
' P8 S5 \1 l# ?' |% Y+ ]
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
; d. T1 P, [8 o# C# o ls = LightSource(270, 45)
" p$ O. Q9 g" s1 @/ [/ O rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
3 m$ @$ e7 H: B5 ?6 D" D/ u! [* f surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
6 o. G4 a6 O, M( z7 i0 Q
linewidth=0, antialiased=False, shade=False)
/ }7 ~# E0 P. j% W2 Q( ~! ?2 M plt.savefig(example4.png,dpi=600, bbox_inches=tight)
9 s% t% Q- ?5 e6 `3 C0 J plt.show()
. z! i1 m$ d+ P9 ]% z) \
, n [2 f, A9 a1 i9 l$ f
Example 5:三维地形,包含投影(Python)
8 m: J3 x& b6 T
r5 [+ S% ?& k6 t Example 6:切片,多维数据同时展现(Python)
! E* z6 n/ z' \8 v4 l& N2 Y" m( `
* u7 I7 x. b& o9 \ R# D Example 7:SSH GIF 动图展现(Matlab)
3 I# c d: T$ D) Y% B' X$ x a! e6 J& N. }
Example 8:Glider GIF 动图展现(Python)
/ F2 z6 g, h3 h4 ^- Z+ H8 n
8 N2 g% }( t$ Q! d. T8 P } Example 9:涡度追踪 GIF 动图展现
+ S2 B m; V6 ^9 m+ L: b! q
& `$ u+ B' r; N, l! b$ V