( J& R. ^, V. K9 f
在我们科研、工作中,将数据完美展现出来尤为重要。
8 X8 ~' }+ t1 d- Z& b/ z, M
数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
0 @3 d( j! c# `! g 下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
4 T6 d9 \+ R, r. e+ m9 _
Example 1 :散点图、密度图(Python)
" Q. m4 k: ^7 j* a, s" o
import numpy as np
7 V' W+ i* G1 N import matplotlib.pyplot as plt
' n: S ^: d# t) k( Q2 S
# 创建随机数
' K8 Z1 S# m6 n" z. M8 Y n = 100000
& y& N0 y# n% Z; e) V x = np.random.randn(n)
; j$ s) h6 V9 e# }
y = (1.5 * x) + np.random.randn(n)
4 ?) Z" `+ v+ v fig1 = plt.figure()
% h+ d5 k; O/ g8 s6 h% t4 D" u1 c
plt.plot(x,y,.r)
! _" ^# u s3 V, ~6 _6 x2 |( x plt.xlabel(x)
% B# S& }. t& x( e; h6 Z; @
plt.ylabel(y)
! {0 c' g4 R" O' w% X plt.savefig(2D_1V1.png,dpi=600)
' C1 s1 O0 e: C1 m, L9 x nbins = 200
5 `- d' q4 T) Z2 V
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
. @. a- ]) I' E( m% Q1 \ # H needs to be rotated and flipped
) i0 ]* g3 r6 I- r6 c+ S- y. }
H = np.rot90(H)
% p+ k$ }; O% {* k+ U1 o% B
H = np.flipud(H)
% a' X9 Z$ v, J, |- s
# 将zeros mask
, n" ?( j+ B: j4 x
Hmasked = np.ma.masked_where(H==0,H)
- P/ _9 X# g' f& p7 L+ j # Plot 2D histogram using pcolor
b$ v5 y6 L5 M: R
fig2 = plt.figure()
8 a9 o( b( J+ H) V w' p* D% O6 x
plt.pcolormesh(xedges,yedges,Hmasked)
3 a, z. Q, K2 U) X5 {9 O
plt.xlabel(x)
4 F5 ?; h k3 h- O: p: m plt.ylabel(y)
/ K' n/ M" m9 B- l4 m s7 p9 X cbar = plt.colorbar()
0 v! u! b) z2 \. O cbar.ax.set_ylabel(Counts)
N+ q; Q0 h% B plt.savefig(2D_2V1.png,dpi=600)
. x7 s& a! x; n7 t
plt.show()
5 a6 t2 H4 K2 D' b4 l7 J
3 z6 J% l! I+ e% b$ Q8 x# o0 Q " C1 ]+ W% V9 @3 O7 i3 c" D9 i2 Q$ X
打开凤凰新闻,查看更多高清图片
8 ^, `0 S3 L4 L: Q( Y& o
* f6 `& L1 z4 `; T- f1 l/ G 3 B0 |# L2 R& B4 T0 [

" ]2 M: M' N6 b- g
Example 2 :双Y轴(Python)
5 E8 @' r; d$ H B: g import csv
$ y$ |' v) `8 }1 I1 g- E
import pandas as pd
0 l6 @) c/ k- z import matplotlib.pyplot as plt
0 A6 d1 O3 {4 [8 Q+ t" D
from datetime import datetime
7 w5 a" K1 I* L! Y5 A+ s data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
; t8 j; Q( W2 |& o
time=data[date [AST]]
& d. p: y( c5 b, G# G+ h sal=data[salinity]
P5 X4 G$ w+ y4 y; F: b tem=data[temperature [C]]
6 ^% h m8 { L! Q print(sal)
+ C6 G! t1 ^- `3 S; n
DAT = []
" ^1 y4 k# x0 ~8 D- b0 V" P ^ for row in time:
# h" H5 \( V5 j
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
' }& U6 r4 y* u+ s8 b. [* @) ^ ^, o
#create figure
2 `9 h' F5 j+ g1 y9 ^% F fig, ax =plt.subplots(1)
8 k+ k" b* T7 z t2 c3 L6 G/ z # Plot y1 vs x in blue on the left vertical axis.
9 a5 Y0 {) ~9 a" ]: x: j9 S plt.xlabel("Date [AST]")
, e% g& I, ~$ Z( x( I! E plt.ylabel("Temperature [C]", color="b")
. F( R( W: ^) x; J' m
plt.tick_params(axis="y", labelcolor="b")
) z0 d, d' B8 W% m3 j4 T
plt.plot(DAT, tem, "b-", linewidth=1)
; z! y+ W8 i3 K- u8 @* f plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
# [2 U2 t# A( [ fig.autofmt_xdate(rotation=50)
6 A: B: H& K- \' j# _5 @ # Plot y2 vs x in red on the right vertical axis.
4 a( @7 m) K4 Y, a/ N plt.twinx()
( m+ c- F6 Z0 ~$ P
plt.ylabel("Salinity", color="r")
) }$ t$ c3 }1 s plt.tick_params(axis="y", labelcolor="r")
4 s3 K, M5 i6 t6 T4 t: d9 O2 m% P
plt.plot(DAT, sal, "r-", linewidth=1)
" b9 D) X( w. P( F
#To save your graph
/ K8 ~5 Q! H5 B7 n$ i0 i6 C plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
3 T) a% \' A( Y$ M$ v) G plt.show()
# Q( l E' ]; D# x$ E) Q* t 
; |+ a: X0 l. N4 A: U/ E Example 3:拟合曲线(Python)
6 ^0 {* Y+ c6 t. [: u0 ?5 {
import csv
, E$ t: q& D( o$ R
import numpy as np
& \" u/ S( X' P+ {5 h9 n import pandas as pd
i& n% f+ I3 B* e* i( z* j
from datetime import datetime
+ B3 o/ E" }* x# r' T/ H7 e( `- W4 S import matplotlib.pyplot as plt
* U3 _, f9 Y% ?( E' H+ n0 S
import scipy.signal as signal
6 Q; g. M- d! f6 b3 ~ q data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
4 N7 ~6 @- n8 a# n; h+ K* `/ ?
time=data[date [AST]]
0 W q) |8 j/ d
temp=data[temperature [C]]
2 t0 ^6 l! I; c% Q {( |. _ W datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
' W9 o9 [0 t. L6 P$ b- Y2 @
DATE,decday = [],[]
5 ^) O7 n9 q+ a h; l for row in time:
4 \0 \% h3 Q% p daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
9 v3 M ~) ~5 Z+ x4 h \
DATE.append(daterow)
# k& C& B J% q2 e
decday.append((daterow-datestart).total_seconds()/(3600*24))
% f$ g! I6 Y# i3 x G( P7 [ # First, design the Buterworth filter
5 R# F3 A. u; x! V1 A9 q4 I } N = 2 # Filter order
! E3 T8 Y' S) R( l/ j
Wn = 0.01 # Cutoff frequency
( z! v3 d- J0 C8 @& J/ y/ n B, A = signal.butter(N, Wn, output=ba)
, A. a9 S! p2 \' y: _( V: U
# Second, apply the filter
: [# M* F8 U, n! V
tempf = signal.filtfilt(B,A, temp)
. N o, P7 I+ ]( E1 R4 R
# Make plots
3 `4 \; O+ H7 m | fig = plt.figure()
6 o( j+ c+ Y( x/ T ax1 = fig.add_subplot(211)
% F- k3 y6 _! H; `6 G' \9 k' R plt.plot(decday,temp, b-)
; I$ K$ G8 l) x* b plt.plot(decday,tempf, r-,linewidth=2)
: m; L# K" Y6 W y9 G
plt.ylabel("Temperature (oC)")
+ \; E8 t, B+ Q6 ^% u
plt.legend([Original,Filtered])
6 n! z$ e9 h m) w& L5 l plt.title("Temperature from LOBO (Halifax, Canada)")
9 Y: Z! N( y+ o
ax1.axes.get_xaxis().set_visible(False)
1 W/ m6 b- c% j. r0 a! x- E7 e" R- t ax1 = fig.add_subplot(212)
4 n3 I0 e9 |& n3 ? plt.plot(decday,temp-tempf, b-)
* ~! G7 g8 m* H1 Z4 n7 }
plt.ylabel("Temperature (oC)")
5 ]# F2 k+ ~9 [4 |2 ^
plt.xlabel("Date")
$ ~$ B$ q u& x plt.legend([Residuals])
0 O% b+ T. U2 w plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
6 a+ j3 M% n7 j# W( a d3 `
plt.show()
0 M4 S5 R2 T0 H9 _

% V& I' U: R, H+ t Example 4:三维地形(Python)
/ C1 t4 \8 P0 [( b5 L ]5 x
# This import registers the 3D projection
- x3 ~: `/ d( y
from mpl_toolkits.mplot3d import Axes3D
6 ^* E3 J7 u) Q% _, H' ?( m- } from matplotlib import cbook
' I. ~$ p9 L6 j9 R from matplotlib import cm
: y) {$ s! d$ P* @ from matplotlib.colors import LightSource
" P+ |% @8 Y$ K) Q
import matplotlib.pyplot as plt
* m) S# T9 Q$ n" u import numpy as np
, A( s( N9 [' U( W filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
4 c- k M, u2 x3 _9 `5 m/ c3 n
with np.load(filename) as dem:
$ z7 J6 [2 c+ C, {+ R5 n3 y z = dem[elevation]
7 o% N: [6 Z2 s7 Q nrows, ncols = z.shape
; \9 _" q h, j) J& H
x = np.linspace(dem[xmin], dem[xmax], ncols)
$ y7 {0 Q2 J$ o8 C& l
y = np.linspace(dem[ymin], dem[ymax], nrows)
+ N' @1 v8 E( U7 Y' N x, y = np.meshgrid(x, y)
; l' V* z; ~2 ^
region = np.s_[5:50, 5:50]
c% I6 P7 V* j* O8 c x, y, z = x[region], y[region], z[region]
" _* Y+ C; l7 ^! Y& B0 t8 C" P fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
* X8 i+ I6 r. j$ Z* s4 d1 W5 v$ k ls = LightSource(270, 45)
# Q3 j& B K8 z8 c2 D$ e- e! T- m
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
/ Y3 i* ~, N& o4 K1 _ surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
) {! J- m( `; O( J2 E, G, v4 t4 b. [ linewidth=0, antialiased=False, shade=False)
7 F& Y3 ^* i9 A, H2 E, } plt.savefig(example4.png,dpi=600, bbox_inches=tight)
2 v' g: S% ?* E plt.show()
) j# @( m& |6 i j4 s5 B
E2 h( ~2 N" T, `6 O T( {
Example 5:三维地形,包含投影(Python)
8 M* N3 ?# _' i& w- e
5 I# N: V" p. e7 c7 W' Z
Example 6:切片,多维数据同时展现(Python)
8 O! N3 s! N0 b& L& O
0 E j2 X, D+ Z" y Example 7:SSH GIF 动图展现(Matlab)
0 d z! N- r U* \! h1 A# u) l 9 v6 C4 V( G4 h$ V1 F9 b
Example 8:Glider GIF 动图展现(Python)
9 @* Y7 R& f) y" K' {; _9 B
% L* k: q$ { X0 Y& j! _
Example 9:涡度追踪 GIF 动图展现
2 p: z/ [- P2 a) p
" h9 @0 G3 m9 f% q% H; m