! x# l. b- u4 M( M
在我们科研、工作中,将数据完美展现出来尤为重要。
' k) U( h5 s, f: M; M5 r; x+ f 数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
# k Y7 Z8 h( d) w* k 下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
6 j3 F& h9 |- n
Example 1 :散点图、密度图(Python)
: L8 y; t' q0 d6 P& s import numpy as np
7 {$ e2 j6 e; \ import matplotlib.pyplot as plt
* S" |+ D0 L+ P) O/ f; I) H
# 创建随机数
3 S. P8 ^, f. C" Q) D* } i
n = 100000
0 q! b4 E. I; F/ j$ T
x = np.random.randn(n)
* T8 k" R4 b+ `8 o) i y = (1.5 * x) + np.random.randn(n)
) e5 F! W C+ D' A7 H5 x j fig1 = plt.figure()
4 N3 r* m$ D" b( \% r f" R0 { plt.plot(x,y,.r)
; M( Q3 F2 @$ `' O
plt.xlabel(x)
2 q) d3 z/ b/ _% l4 T! } plt.ylabel(y)
" F8 z9 L3 f7 h1 G2 w
plt.savefig(2D_1V1.png,dpi=600)
+ [$ V, x+ J( \2 L" h nbins = 200
2 w0 t7 T4 h5 J5 [4 B* T; z H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
& W2 k, [% L/ H' ?" u. Q8 R
# H needs to be rotated and flipped
' Y4 k2 H. _ V7 C4 V# A" f$ P H = np.rot90(H)
: t, j- w7 a5 _# H H = np.flipud(H)
' U! b8 o7 P: {' O+ g! ^/ O
# 将zeros mask
# [- X2 B2 S; a& G* W# J. |) |1 u- a Hmasked = np.ma.masked_where(H==0,H)
' s% c) A0 G, ` # Plot 2D histogram using pcolor
0 T. f$ X; \$ g* z/ N3 r fig2 = plt.figure()
! H3 N5 ~4 u0 J( t6 L; w plt.pcolormesh(xedges,yedges,Hmasked)
w7 s y7 k; n/ k5 Q2 \- Q" j plt.xlabel(x)
' P0 ?* ]6 y* t- m! C/ S) ^" H plt.ylabel(y)
/ y9 P9 K/ X: m' y. H
cbar = plt.colorbar()
( B& @+ q) z4 A% s cbar.ax.set_ylabel(Counts)
' ]: G9 }/ Z5 ^- D( b* ? plt.savefig(2D_2V1.png,dpi=600)
" U' l5 k3 f) w4 f plt.show()
% l" G: ?4 c0 M4 ?+ U, V& T6 w ?2 n& M
) s, p( t2 \) X1 P* Q0 l
L4 {( {8 }' h$ P( j 打开凤凰新闻,查看更多高清图片
* p& a" P- N; H
7 n; f9 `% C: m+ s) W
- q6 B7 F; e0 X* `8 g 
+ F; r0 K1 _; Z# Y Example 2 :双Y轴(Python)
" I3 B p L z6 C5 ^/ P) T import csv
) R( K- b! E( y% G6 Q4 r$ e9 y, ~
import pandas as pd
% Z; E7 `% u' ]5 | N/ N" A; W import matplotlib.pyplot as plt
, N% C/ U% M' y# y
from datetime import datetime
3 U/ Y" b& o+ U" l1 B5 }$ F. V' N
data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
$ B3 e: L" s7 E, x8 W# B
time=data[date [AST]]
6 K0 w6 R6 ^# g& H5 G$ C- P0 `
sal=data[salinity]
. q. n6 [% U+ E% ~9 }' K! s9 r tem=data[temperature [C]]
3 j. @% z. ]1 D! y' b0 V( F print(sal)
$ B: N0 w4 V: x1 j- X# s6 W
DAT = []
1 d! o9 I) J: z for row in time:
7 L' m' w0 ]4 x& {+ A) P
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
4 m0 o1 Z- p1 B( ^2 p
#create figure
: S. p$ y, B( _; X# r fig, ax =plt.subplots(1)
) A7 \" h) r6 v* r # Plot y1 vs x in blue on the left vertical axis.
E' l* X+ }( u1 {2 D plt.xlabel("Date [AST]")
4 L9 Q( a& o5 T1 w5 I, m plt.ylabel("Temperature [C]", color="b")
3 L, @& r0 e$ ~4 k& X1 I2 o
plt.tick_params(axis="y", labelcolor="b")
N, x7 C2 |5 [4 i' F9 a
plt.plot(DAT, tem, "b-", linewidth=1)
6 I$ a8 R4 y+ {" \% w! r% ] plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
( W$ }& v8 J2 m3 H fig.autofmt_xdate(rotation=50)
( L; Y2 V3 ]! J! H # Plot y2 vs x in red on the right vertical axis.
: p2 u8 P6 J* T3 Q! G0 K7 E! r plt.twinx()
3 B# q, w! v3 D
plt.ylabel("Salinity", color="r")
: w8 S4 I) ]" j( j
plt.tick_params(axis="y", labelcolor="r")
) O) x$ H# @1 ]+ |1 C2 D' Z
plt.plot(DAT, sal, "r-", linewidth=1)
/ z# Y% P" A3 X #To save your graph
0 P0 a3 \/ O7 |* H$ E9 j
plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
3 B/ f( o- N G/ F" m2 ~% h plt.show()
- }, Y$ z( m9 N) D$ l0 d2 P

$ @7 b- H; O( V1 N1 K d
Example 3:拟合曲线(Python)
- T0 Z; b( h0 e
import csv
6 P- E6 q& e+ d import numpy as np
$ b4 e6 n6 e* d1 _. r9 y S' H5 U
import pandas as pd
0 z% Z6 e$ |1 \+ X from datetime import datetime
# x& e4 V* J0 @
import matplotlib.pyplot as plt
& n4 t# U Y3 l' P/ h
import scipy.signal as signal
. E8 C+ G& e4 M! B/ b1 W7 h
data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
7 r. i* P& _9 p7 @ time=data[date [AST]]
( l9 m/ _, i* n. t [
temp=data[temperature [C]]
8 P2 D. P4 r6 L' N datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
! {! N2 n7 q `# R& }) I DATE,decday = [],[]
9 _( [' p9 x6 r2 Z& L" B
for row in time:
$ d8 C4 l& i1 `/ P daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
5 U; _ E3 i D b3 ^! F
DATE.append(daterow)
' n0 q% }! X1 c4 n7 }7 ^ decday.append((daterow-datestart).total_seconds()/(3600*24))
! M- w0 @- s8 ]# M% D; u# l3 ] # First, design the Buterworth filter
: `! u `/ b+ v* u
N = 2 # Filter order
A: X# q6 N+ n Wn = 0.01 # Cutoff frequency
, [& Q a' S$ I- p" S B, A = signal.butter(N, Wn, output=ba)
, ^7 `7 a; \/ K& K5 i: p; `
# Second, apply the filter
2 F; i9 t9 p3 W6 P9 H2 Y" |+ P
tempf = signal.filtfilt(B,A, temp)
8 u: b9 q- {! F3 C' N, _8 E4 T # Make plots
% J6 `5 U$ j* E3 o
fig = plt.figure()
. B' y( m* `. l7 m ax1 = fig.add_subplot(211)
% e$ s8 J2 j2 B G' C0 P
plt.plot(decday,temp, b-)
0 N+ e1 z) ` q, o9 _# t
plt.plot(decday,tempf, r-,linewidth=2)
! o K/ d5 A3 q( R4 s5 p plt.ylabel("Temperature (oC)")
: G! `: {, a1 ?
plt.legend([Original,Filtered])
5 n, w5 n9 i' ]/ n7 @$ z4 p plt.title("Temperature from LOBO (Halifax, Canada)")
. B0 i# F; N* \* [) D ax1.axes.get_xaxis().set_visible(False)
* `. V7 Y) t" h' R9 p ax1 = fig.add_subplot(212)
- P+ M' E/ P/ N% e! {# o
plt.plot(decday,temp-tempf, b-)
6 p/ z6 N3 K$ } plt.ylabel("Temperature (oC)")
8 n2 k/ {1 N" |- b& ~- X2 H plt.xlabel("Date")
; n( Y) _) J% [7 l! J2 m" Q) `/ w plt.legend([Residuals])
# x) ~$ C( |* H. @ plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
i+ j! y9 T% b2 p. s! l4 U) e: N$ R
plt.show()
* y3 M( {5 f i) l6 n5 D6 [2 _3 |

% x# O( F( |) t0 g: |, K
Example 4:三维地形(Python)
; R9 u' ^ f% q9 `; }
# This import registers the 3D projection
$ k" ?) x2 A7 B4 `7 {4 g3 T from mpl_toolkits.mplot3d import Axes3D
: v8 t5 J$ T4 k from matplotlib import cbook
7 c$ E3 V0 W' p$ F$ S J from matplotlib import cm
3 [+ @6 y" U0 g/ s2 U( u+ Y1 w from matplotlib.colors import LightSource
( V0 H3 X; f2 E' g( i0 A0 y
import matplotlib.pyplot as plt
% L$ a3 f" U# L: w import numpy as np
; X/ S6 h% _: E9 f! L filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
' L6 b7 p( u% C }: h7 u; S# B
with np.load(filename) as dem:
; }- T2 P/ m1 n6 x z = dem[elevation]
3 |& ^5 r9 \3 i" L& p nrows, ncols = z.shape
/ U* D+ [! y2 B0 C
x = np.linspace(dem[xmin], dem[xmax], ncols)
" U4 [% |# |7 g8 X
y = np.linspace(dem[ymin], dem[ymax], nrows)
$ ]( v& k! M* ] x, y = np.meshgrid(x, y)
2 F7 Y9 i0 ]# }$ O: i' |$ M8 U, q. s( W region = np.s_[5:50, 5:50]
H" u* h* c3 D: w" M% ~: b' n( m
x, y, z = x[region], y[region], z[region]
" X; t5 u/ @% v fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
4 n: w" f* e4 V ls = LightSource(270, 45)
5 b5 G) T \/ u$ C rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
* R7 q, g, h1 d
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
8 i$ H4 l0 _* |3 |0 X- i: M2 n
linewidth=0, antialiased=False, shade=False)
, S/ j# C7 {# B! o plt.savefig(example4.png,dpi=600, bbox_inches=tight)
$ ], h4 Y1 ?9 T f# ]7 V
plt.show()
0 K% @4 J2 q+ K& {
0 R2 U& I% n* I: N9 G Example 5:三维地形,包含投影(Python)
7 Y9 {8 [! c* ~+ K
1 }! A. V/ m# W! J& c/ q( E
Example 6:切片,多维数据同时展现(Python)
4 e- v/ k' @" N+ {3 G
8 F6 a- d2 D/ X- y& C5 L
Example 7:SSH GIF 动图展现(Matlab)
5 S$ f( q1 O5 g( U9 a; [: D
* ?% i) `. z6 |" R Example 8:Glider GIF 动图展现(Python)
2 }/ B* V' R7 e8 Z% L2 l $ j1 w8 ^: J7 K: V
Example 9:涡度追踪 GIF 动图展现
# \ X3 ?0 c; Z; x: |
9 Y, x) \7 R0 n% z