O. W4 c7 c$ D0 G" T 在我们科研、工作中,将数据完美展现出来尤为重要。
/ B: R, r# @, Y) O0 S. I( E3 |
数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
6 I3 I2 ^ j" J% J
下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
0 K! t5 T6 g7 Z" g" m9 t
Example 1 :散点图、密度图(Python)
/ S- n9 o- Q/ W1 U/ x# R8 @
import numpy as np
# }7 {. G% x& b( P" p' f
import matplotlib.pyplot as plt
8 j8 F# ~+ Q% Y) `& ?* W # 创建随机数
% O$ P( H8 j8 v& W } n = 100000
- Z% ~1 O5 {- ?" o3 D( |
x = np.random.randn(n)
4 H1 S+ x4 D3 z/ } y = (1.5 * x) + np.random.randn(n)
' Q! X0 y+ J) t* ^
fig1 = plt.figure()
% d# `2 }" M; l2 b" D9 Q* C8 ^
plt.plot(x,y,.r)
2 P/ ~" b. @5 T4 }1 t% {, w plt.xlabel(x)
9 ~/ f: \& A; @% p6 ]4 Y plt.ylabel(y)
4 i1 A/ S+ o. I0 M! I T' X) Z; u plt.savefig(2D_1V1.png,dpi=600)
. O2 z. F( `9 J4 \) a" [ nbins = 200
. _6 g! B0 Y9 O& c8 p l+ f
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
( P5 p- g1 F8 ^ # H needs to be rotated and flipped
- V" \6 K6 K0 ?8 j/ G H = np.rot90(H)
3 _" v3 p1 b5 x$ D' q) @: }* }
H = np.flipud(H)
) i. Q, D, L+ P' c+ a+ ~5 B0 l5 U& Z # 将zeros mask
+ Q% U9 q& S* Z$ |+ A8 w
Hmasked = np.ma.masked_where(H==0,H)
$ O) I: k1 R1 W: @* G
# Plot 2D histogram using pcolor
$ d. h" ?* r+ ^1 v; C. f+ M fig2 = plt.figure()
3 ]- s9 r/ P& b! @% A! M } plt.pcolormesh(xedges,yedges,Hmasked)
4 J' y4 k% K" G0 n4 Z9 Q& h
plt.xlabel(x)
3 Q9 d* c. n& J0 d) |* I0 C. c6 O
plt.ylabel(y)
' b) b' U4 E9 a, E" [/ e# X( j cbar = plt.colorbar()
3 w! a; I- A# n$ b" k cbar.ax.set_ylabel(Counts)
+ r0 V1 ]6 B! p0 i, g plt.savefig(2D_2V1.png,dpi=600)
* B. i$ ^( Q7 A6 Y% q4 h plt.show()
0 l% ~* \* \5 H% k
# X$ f0 ~. p3 \3 s4 q# L1 I2 _4 @ 1 ?; w3 F; s6 Y7 ]* T% T5 M, l
打开凤凰新闻,查看更多高清图片
0 {4 k4 i1 U6 k4 t9 u, `2 P, @
) U4 p! ?$ D# \7 P. O3 ~1 e2 U+ \
1 M* }- y3 {0 l, r, P3 n2 E 
2 O: u% B: [/ {* _7 _
Example 2 :双Y轴(Python)
" Z- h- H: g C% Q" T5 Q) v. {4 o, V import csv
) y: i0 j7 \% P& p; T; j
import pandas as pd
( p" z) [* R9 B+ W5 l; H9 i6 _
import matplotlib.pyplot as plt
5 A; \- _- J, } from datetime import datetime
- t7 O9 F' z( p8 ^ data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
# p6 {+ S- Q. \; \2 E( ]1 s, D" o
time=data[date [AST]]
H5 B* d, e& k0 F$ _" j0 e: [' A
sal=data[salinity]
$ l6 t- G8 p2 e tem=data[temperature [C]]
+ c, I% p+ ?* u1 Q: q4 K1 C print(sal)
! R& n; J/ e& E5 L/ H" W" b# L* A DAT = []
& F; M) \6 J$ @% W6 @! u! r for row in time:
0 ^" K. {( {& ^% E; h DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
+ h: _7 `0 b+ ]; n( l- U0 @ #create figure
' L" `) V5 p# N- h! b. @. Q" F# y' _
fig, ax =plt.subplots(1)
/ l) Y6 _4 Z. }
# Plot y1 vs x in blue on the left vertical axis.
+ c, P; L6 F; F3 ^ plt.xlabel("Date [AST]")
. x" U7 b* f% g
plt.ylabel("Temperature [C]", color="b")
- N m. I8 ^ S plt.tick_params(axis="y", labelcolor="b")
1 f/ V ~1 i" g! t2 O plt.plot(DAT, tem, "b-", linewidth=1)
3 Y4 q& v0 m2 b4 h z5 \0 c# x
plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
4 M3 s0 a4 i. a7 y6 d. S& W
fig.autofmt_xdate(rotation=50)
# }% h v& ^+ p. K/ I4 J3 a # Plot y2 vs x in red on the right vertical axis.
" W$ ^( F0 o8 b* ~( e
plt.twinx()
$ x, m+ T3 h m4 [4 g1 L+ K4 i
plt.ylabel("Salinity", color="r")
. K, _% Q" ]% l
plt.tick_params(axis="y", labelcolor="r")
& K: W2 J1 \/ X plt.plot(DAT, sal, "r-", linewidth=1)
' i+ r5 n2 _! w+ v. x. M4 @0 ]
#To save your graph
. \# q4 g$ r0 J# @6 B6 U
plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
, c! x- r1 Y& i* i* f
plt.show()
2 B0 ^4 c% ~# }2 G C

. s/ Z- r' k$ ~& x+ ~: Y
Example 3:拟合曲线(Python)
' w, L' C9 L# O. {4 v import csv
: o. ^# ]( C |- @+ C! k
import numpy as np
M, C b1 s0 H4 Y2 N2 ?
import pandas as pd
9 Y9 {( v" G# K, ?0 I
from datetime import datetime
# @' e; R" t- c8 e8 K import matplotlib.pyplot as plt
8 W7 ]4 ^1 \/ N( V! n7 h( z import scipy.signal as signal
, F9 U+ Y% y5 K4 g9 c4 l data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
4 @/ T/ m/ r' {- t4 ?+ ]. \. X time=data[date [AST]]
: u1 y5 `$ L8 w, |4 h5 ^
temp=data[temperature [C]]
% f; Z( D% W: o: z& Y! H' J
datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
: O1 A6 ?0 A: C' D. `4 t$ J4 |( x; y
DATE,decday = [],[]
3 P+ a" ~0 F5 Q, e U for row in time:
& G% w' \* |- p* e
daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
2 s5 I( T, L9 P; Y, K* q DATE.append(daterow)
/ G" k9 M3 F- u8 `
decday.append((daterow-datestart).total_seconds()/(3600*24))
! X' Q, _9 w" A/ { # First, design the Buterworth filter
/ z3 h, N9 A0 N/ l N = 2 # Filter order
- j- }0 o; h, }9 K: [& x, d
Wn = 0.01 # Cutoff frequency
# p- G v- c' d" v" p B, A = signal.butter(N, Wn, output=ba)
0 ^, Q# t! u$ H5 T4 B" m8 h # Second, apply the filter
3 O' n7 y1 Z1 f7 h4 M1 A/ ?: M8 b
tempf = signal.filtfilt(B,A, temp)
' ^% ]# B/ O/ ^, c: `
# Make plots
* }. @7 _' y9 d7 _* W
fig = plt.figure()
* b0 D6 B, \ @; w; h( N2 l. \
ax1 = fig.add_subplot(211)
! a( Z/ e0 y( y0 g
plt.plot(decday,temp, b-)
; u1 _2 I" [- l, ^0 ^ plt.plot(decday,tempf, r-,linewidth=2)
) Q4 n3 ^2 N9 o" q/ U
plt.ylabel("Temperature (oC)")
/ T3 T: N' q. \
plt.legend([Original,Filtered])
7 X! H4 w; _, {* O. V q6 C7 k
plt.title("Temperature from LOBO (Halifax, Canada)")
8 V- ~2 y8 ~ e4 s, U2 x
ax1.axes.get_xaxis().set_visible(False)
( k8 k2 ]6 d" h5 U ax1 = fig.add_subplot(212)
* r: h$ k' R* ` @3 `6 C plt.plot(decday,temp-tempf, b-)
9 N; m: K& j. Q, ?& s
plt.ylabel("Temperature (oC)")
6 G( H1 d; A6 g
plt.xlabel("Date")
! d( b( K; ^# \) o% V& J# H
plt.legend([Residuals])
4 p% O5 i# v( V( a' J+ E, U% e plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
5 {+ p+ t# ?9 \ x, V8 f
plt.show()
( }6 ^. r* N+ R" g7 E9 m @

# m! g0 E, E0 k3 ~
Example 4:三维地形(Python)
5 a, m8 T3 M4 [+ }7 n% @
# This import registers the 3D projection
( C2 m: S# Z5 e from mpl_toolkits.mplot3d import Axes3D
2 i9 t; a# C' X$ o' K from matplotlib import cbook
6 I# Y( r- s# n3 z" R from matplotlib import cm
' O' K# L# W( _ from matplotlib.colors import LightSource
: Q" E5 S8 ^' X; t import matplotlib.pyplot as plt
2 U" s n# Y1 `; m. g) c) l import numpy as np
; D: T- A1 K- z+ y2 b. @9 l/ O filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
6 {. Z0 y7 n2 z& P6 C
with np.load(filename) as dem:
" Z+ h/ ?% L C& J6 A" o& l z = dem[elevation]
, V! {* C: ]6 ]' P6 Y1 { G, m0 u3 ]
nrows, ncols = z.shape
0 u7 a3 u6 {7 F7 o7 b
x = np.linspace(dem[xmin], dem[xmax], ncols)
+ t- q+ E: S8 ?) l, u
y = np.linspace(dem[ymin], dem[ymax], nrows)
' F" b7 c' I' V* P x, y = np.meshgrid(x, y)
0 J: v2 X0 _' P% r$ J* h region = np.s_[5:50, 5:50]
8 L2 S! z+ r; G) N" y' d, \. z
x, y, z = x[region], y[region], z[region]
. x; x8 {" h6 D
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
# {8 Z& v# j% Y9 p3 A, D ls = LightSource(270, 45)
+ E: `' O7 x) k" N$ h6 O- q rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
$ x8 _: b& d4 e3 ~' P+ B+ e( G5 b surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
7 x6 C6 `& ~, b" c linewidth=0, antialiased=False, shade=False)
8 V; L. p% J: S# X8 `( o( \
plt.savefig(example4.png,dpi=600, bbox_inches=tight)
: M, E9 _2 T1 y- A
plt.show()
; y( }/ a9 }' ]
8 N& u7 c4 C# I7 W1 } Example 5:三维地形,包含投影(Python)
( o& ^4 n3 K6 }/ A, C' }9 ?
9 u7 e8 j- N8 I" ] Example 6:切片,多维数据同时展现(Python)
# L- F' D+ ~5 L4 z% l5 L
1 J7 u9 c7 A, e Example 7:SSH GIF 动图展现(Matlab)
: q c x$ f4 w. l0 ^7 @3 N e
: f. @# z* N+ L, f; W; i
Example 8:Glider GIF 动图展现(Python)
( p1 H# h8 y! U
5 U- E t$ Z$ |+ L( t N Example 9:涡度追踪 GIF 动图展现
& I$ z+ o7 O6 x, c" {* x , G, h. |) F* N: o