: z* c1 ?2 _0 O, c, o: I f8 p 在我们科研、工作中,将数据完美展现出来尤为重要。
/ Z( b, O7 [7 o+ S% v
数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
5 w& W, e* V; B$ ^ 下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
1 a3 P+ V8 z% B& I5 X
Example 1 :散点图、密度图(Python)
% o4 H. L9 N9 ~* m6 E) W$ B import numpy as np
' N9 X1 @2 |( j# |; `
import matplotlib.pyplot as plt
1 P% j( N6 K k% p; t+ { # 创建随机数
1 G5 r9 m; G5 s! ?
n = 100000
9 S" t3 L7 S* |9 E ~" L, U x = np.random.randn(n)
6 g$ m9 s! Z5 l8 P) m* { y = (1.5 * x) + np.random.randn(n)
% \+ _0 v0 z) N$ y( @ fig1 = plt.figure()
9 V8 ?0 s8 ?- E/ C! N$ C. ` plt.plot(x,y,.r)
% ~/ p$ b& D, c% d8 l plt.xlabel(x)
; T: M# a! @* C4 o- I5 V! L' I
plt.ylabel(y)
/ I" _7 m8 S: h4 Q: a
plt.savefig(2D_1V1.png,dpi=600)
* e8 Y$ c) e g8 q* ]- k1 B6 N
nbins = 200
( k2 h; ^& n& R) a2 H
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
3 T0 ^- R- i& \- I
# H needs to be rotated and flipped
|" S6 I) ]3 T
H = np.rot90(H)
$ G8 n% J) `: X- Z+ J8 u: z
H = np.flipud(H)
% c* Z0 n& A8 S2 p/ Y4 E # 将zeros mask
) A% o3 s. @7 ?( A5 Z
Hmasked = np.ma.masked_where(H==0,H)
' Y" n" \( x( O4 t! h # Plot 2D histogram using pcolor
. }+ x- M7 {5 D8 ]5 H+ E1 @, j% R8 P fig2 = plt.figure()
- \3 V) E, i* M" O plt.pcolormesh(xedges,yedges,Hmasked)
# J! k# I9 a3 ~5 Q; @- i
plt.xlabel(x)
. J5 l( _1 ?, x- { plt.ylabel(y)
5 Z5 ]) B: K( o8 T( v# E cbar = plt.colorbar()
4 h; q6 K. e! \; _) ~ cbar.ax.set_ylabel(Counts)
( ?# s1 T1 B, t% i: q+ M- B0 o
plt.savefig(2D_2V1.png,dpi=600)
& g9 X3 d* [4 m
plt.show()
: p$ r0 l$ M D: P3 R- P: f
9 Z' k1 U: [ X5 Z! P# ]- R ! y" ?, K2 T5 c$ C2 T1 ]! e( V
打开凤凰新闻,查看更多高清图片
$ T4 r# I$ [7 C. ~
$ }6 c) g x* B; t% t. o 9 O' p! x2 d ~& X

- T( `+ A3 U& ?' w Example 2 :双Y轴(Python)
5 |+ P( z, e8 C z; X5 z. W import csv
- }" p n2 v6 v. i5 G
import pandas as pd
- J6 z- I9 z/ G import matplotlib.pyplot as plt
) s- a/ H, L' e, w. a7 I" ~+ D4 M
from datetime import datetime
7 \: H) {& _: j% e
data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
, c- o" [! G+ B4 O$ u1 s) u time=data[date [AST]]
" j0 ?# G" c) Q sal=data[salinity]
( {3 {: ~$ ~4 |$ y. u# U6 s0 G
tem=data[temperature [C]]
( l. ^! H6 I5 [3 p; ^1 _ y print(sal)
/ x9 J% O( @ U3 c: k DAT = []
* _& Q% B) }# U# S2 T, O& }
for row in time:
! r+ @! T0 X( r$ l. e
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
. X2 A; u3 L. L8 Z; n
#create figure
9 H$ O) c. g; B( c( s fig, ax =plt.subplots(1)
, A, e4 i z8 i1 L" _! E- c
# Plot y1 vs x in blue on the left vertical axis.
1 y" f& V+ |' ^6 h0 L, f" V. @7 g
plt.xlabel("Date [AST]")
' ]3 Z$ n$ T, ^, n* b1 A C$ L plt.ylabel("Temperature [C]", color="b")
/ m+ ^: \9 H, [8 L# l( v+ R8 x plt.tick_params(axis="y", labelcolor="b")
9 r" m& W5 j& H2 i
plt.plot(DAT, tem, "b-", linewidth=1)
3 M( j, B' z* |. Q+ f4 V0 |
plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
0 C" O( i$ d4 O' P6 P2 p
fig.autofmt_xdate(rotation=50)
W1 K Y9 @2 i
# Plot y2 vs x in red on the right vertical axis.
, j' k. p+ `* e
plt.twinx()
$ i( Z9 C( u+ A" p% k plt.ylabel("Salinity", color="r")
4 G$ c- R- l- r4 N plt.tick_params(axis="y", labelcolor="r")
) b# `7 h6 Y% [
plt.plot(DAT, sal, "r-", linewidth=1)
+ W0 h4 @, X# W- J
#To save your graph
1 C" d, M6 v8 W
plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
" d$ L0 t* V/ F" I/ c; v, p& e
plt.show()
3 M3 h6 w+ L6 U. m3 _' L 
/ o" \: w( G2 d% z8 M' M. e
Example 3:拟合曲线(Python)
# z3 I# ^% [9 J9 U0 \
import csv
# z3 v2 U0 s$ ?6 ~: m4 Q
import numpy as np
; C$ g0 A% X& w9 ^
import pandas as pd
! w( s; r; M5 ]0 T# z# K from datetime import datetime
1 J; R7 g' w) s. i. j import matplotlib.pyplot as plt
4 {6 N% y* d( x: d! z4 ?4 W
import scipy.signal as signal
+ p3 ~. L9 X! [, W m% ?* L data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
r! G3 f6 M$ d: E" F: Y, R
time=data[date [AST]]
- M' H1 w" ~2 a1 `
temp=data[temperature [C]]
9 b4 g) k5 w& }4 Q$ w/ l1 ~ datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
8 \( Z- K T. J6 o# L$ e" z
DATE,decday = [],[]
$ W+ ]1 g8 r0 A2 a& N for row in time:
0 I0 L" h9 s* X2 |$ j* Y) E B
daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
4 @* C4 R4 P+ X& A DATE.append(daterow)
+ W8 @* e1 z g
decday.append((daterow-datestart).total_seconds()/(3600*24))
$ G) c9 R# e$ O # First, design the Buterworth filter
) s- O6 ^/ U0 d N = 2 # Filter order
: l3 ?6 X. g! y) K4 s) @
Wn = 0.01 # Cutoff frequency
5 D" H. n3 E/ R7 ]6 g6 L! k( i B, A = signal.butter(N, Wn, output=ba)
5 S, g, f( T7 g! x6 w # Second, apply the filter
+ p6 _( r0 x- d& z' v) s2 L
tempf = signal.filtfilt(B,A, temp)
* l" f, N! N" x: ]8 J( S # Make plots
' S# V) M9 u) ~- w& I" M% G! _ fig = plt.figure()
7 t1 g3 |7 T" i V ax1 = fig.add_subplot(211)
+ M, m; z0 G. \- {9 x9 R
plt.plot(decday,temp, b-)
. }% A, n; n$ \ plt.plot(decday,tempf, r-,linewidth=2)
4 V, Z" K* i$ a
plt.ylabel("Temperature (oC)")
7 z4 a0 m3 v# `8 g: W% E# Q plt.legend([Original,Filtered])
- l0 [3 B. q8 c- \
plt.title("Temperature from LOBO (Halifax, Canada)")
3 O* U6 {( S3 k" n$ }+ b- G$ }
ax1.axes.get_xaxis().set_visible(False)
0 H% ]* s5 B2 ]* ^% l k C
ax1 = fig.add_subplot(212)
l7 q* \3 H& M& W4 _: a' b9 o
plt.plot(decday,temp-tempf, b-)
" a. R( b5 C: |: Z2 j. p1 q" Y& i
plt.ylabel("Temperature (oC)")
( P, o! l# h" D' ^
plt.xlabel("Date")
! H( l7 Z, Q$ Z; R
plt.legend([Residuals])
+ U" l2 @/ i6 u
plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
$ b z' y4 D# L, i3 C3 j6 v plt.show()
( {2 V/ e, y' I8 H

& | k5 `+ |. Y8 {( | Example 4:三维地形(Python)
" G1 w1 @2 k2 W$ C& R # This import registers the 3D projection
; u6 ]2 `8 y a
from mpl_toolkits.mplot3d import Axes3D
3 T9 K8 P: A- f( L
from matplotlib import cbook
8 E& o! O) f% P
from matplotlib import cm
0 @. o1 v m9 X" R4 R4 p
from matplotlib.colors import LightSource
; g$ x! p" ^( i- v, T6 z
import matplotlib.pyplot as plt
: }- w1 ]& q6 E
import numpy as np
# v0 x2 B1 p2 M filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
& C9 r' O8 L4 V0 _ with np.load(filename) as dem:
7 b/ q- j3 k* H
z = dem[elevation]
+ T$ y- |3 J3 ^. ]% _9 L5 V nrows, ncols = z.shape
# R0 K7 J5 }" N& g7 j x = np.linspace(dem[xmin], dem[xmax], ncols)
9 U7 l6 m& S* n8 T5 B y = np.linspace(dem[ymin], dem[ymax], nrows)
8 g% y n' N; E6 M9 P x, y = np.meshgrid(x, y)
: K# _" w2 A ?1 @ region = np.s_[5:50, 5:50]
' R, A7 J" b) q8 \+ p' R
x, y, z = x[region], y[region], z[region]
U* a/ C i6 T7 E2 @. N+ v
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
9 k/ ]' D( J1 l
ls = LightSource(270, 45)
+ S) n! A9 V' M: o7 L5 O
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
2 r; [# y2 W) S3 r2 B) Y
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
4 e& G) T6 J m U- ~
linewidth=0, antialiased=False, shade=False)
) B' s4 O* A7 v; `& i plt.savefig(example4.png,dpi=600, bbox_inches=tight)
# G2 e5 V3 z5 f$ n1 H
plt.show()
. k [" M6 t+ @6 G' ]. E
( x3 v; k: k# Y" n6 r4 B8 }. v6 R6 D4 g Example 5:三维地形,包含投影(Python)
& h5 m9 Z/ x4 A$ V0 {
1 Q7 W+ Z; e/ `/ s( ~ Example 6:切片,多维数据同时展现(Python)
) a7 V. ]" L; R) @; j3 r9 ~ 5 P; a" y3 i8 B' m
Example 7:SSH GIF 动图展现(Matlab)
' }% x4 X$ T, y L' ^
$ [3 K* H9 I, z7 K4 v; V9 y L9 L Example 8:Glider GIF 动图展现(Python)
) I: `9 E+ s7 p1 R! g: ]
1 ]6 j7 l7 j5 q0 b0 t9 w4 K
Example 9:涡度追踪 GIF 动图展现
# E1 ]$ X% C7 c! g5 ^* I3 q E& g
9 O2 `, T9 |, T) T" I$ L