/ z! Q& p& c9 \9 H9 J
在我们科研、工作中,将数据完美展现出来尤为重要。
& Z) _% e, L8 R- x4 k/ E 数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
8 L1 ~7 b6 w# @# V
下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
' o- M3 s# i' O Example 1 :散点图、密度图(Python)
% O8 Z# s8 v5 g. e7 n1 a* V0 m& c, g& }
import numpy as np
8 Y' d) T: j; K import matplotlib.pyplot as plt
! w0 F4 ]. b3 K, @" e( l( z # 创建随机数
" I) [, s. w, @8 |) o
n = 100000
# Z0 p7 h7 O& ^( j
x = np.random.randn(n)
9 ]- a) p% P% i2 N y = (1.5 * x) + np.random.randn(n)
, P0 P: c) X8 Q3 i3 j" n
fig1 = plt.figure()
8 y2 } M9 \* Z2 }- v plt.plot(x,y,.r)
, B8 U, `. e3 D* o2 _
plt.xlabel(x)
- w# g; @5 F+ b" k4 c" Q0 J
plt.ylabel(y)
" e5 b7 f. k8 y, [ plt.savefig(2D_1V1.png,dpi=600)
5 ~( [4 a, ]' a$ w
nbins = 200
4 X& ~ s* `/ _" ^
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
+ }4 o E* h* Y$ }. M5 U # H needs to be rotated and flipped
9 H( V: b2 g. B# U2 W
H = np.rot90(H)
- }/ }: V, e7 i- J
H = np.flipud(H)
0 V( T s0 `: g9 E/ H # 将zeros mask
! B) m2 V7 V; h' j! q9 _$ K9 K9 p1 ] Hmasked = np.ma.masked_where(H==0,H)
: ?* A2 g) S8 G$ a) a$ C
# Plot 2D histogram using pcolor
; A/ N: P% R( L. M e+ p
fig2 = plt.figure()
8 h, I5 |$ Q: M/ z, P plt.pcolormesh(xedges,yedges,Hmasked)
/ `+ R7 }' u+ @) Z% v
plt.xlabel(x)
% R( T( \, O& |* e; I% C2 E plt.ylabel(y)
5 Z) p% ]' P) T; w) F6 E
cbar = plt.colorbar()
- `6 ]& [, P/ c9 @$ D# [
cbar.ax.set_ylabel(Counts)
, [# K# E) [- @# Y) W3 C; a1 Z plt.savefig(2D_2V1.png,dpi=600)
+ @5 t7 |' C8 S% }2 N+ F' k$ R
plt.show()
3 B+ J- `, K' M9 u ( y! ^/ i: r* ]8 w4 [0 ^/ }
2 L# b; o" h6 t2 ], n4 k
打开凤凰新闻,查看更多高清图片
5 o1 x/ H9 {6 K6 u
. t- Q- \2 ~& d) U9 w! d g 4 C- O( Z2 h1 C) y+ W
& [9 {/ i* t) K$ Q/ G: t" j Example 2 :双Y轴(Python)
, v% P1 H2 U* Y o" z0 K5 R. k
import csv
1 j4 ~% ^9 R1 V) l import pandas as pd
4 V: g0 @2 y* z8 K import matplotlib.pyplot as plt
, w6 G* O$ ?: o% \
from datetime import datetime
4 Q" }, R! x4 i data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
8 T: K! I* r$ l1 l- O. m' |
time=data[date [AST]]
& D, I5 s A5 \8 H sal=data[salinity]
" w5 I, E% d3 q4 i9 a8 T+ q$ ] tem=data[temperature [C]]
! I# g2 s& y2 e( B( T8 R& L5 c print(sal)
2 i T: z$ X' P- a DAT = []
6 ^3 J, U& Z7 d4 e. }$ B for row in time:
! h' P. l! R, o
DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
5 L2 h; h9 K' M/ H+ Z, g #create figure
9 \& T* I7 C8 }4 D8 { fig, ax =plt.subplots(1)
# f) P& ?; r, g' G* d
# Plot y1 vs x in blue on the left vertical axis.
6 Q; @" S& H c6 v) r plt.xlabel("Date [AST]")
9 v" |: G1 i# K8 k$ } C plt.ylabel("Temperature [C]", color="b")
: q' l" z; a% g plt.tick_params(axis="y", labelcolor="b")
0 D: Y3 d* k/ k: v3 z( h0 ?5 @ plt.plot(DAT, tem, "b-", linewidth=1)
' `7 P9 |- V- c+ [$ y3 ~$ ~3 S
plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
j9 g8 K4 z, T5 g) |9 d5 {. f
fig.autofmt_xdate(rotation=50)
, z# i$ T2 d4 `" @: N
# Plot y2 vs x in red on the right vertical axis.
, s9 W# g! V, o5 l plt.twinx()
) r! a; A4 c& o3 Q6 E- s plt.ylabel("Salinity", color="r")
; E W# T9 g" U7 i; L plt.tick_params(axis="y", labelcolor="r")
; Y* w9 Z* L0 [2 X9 W
plt.plot(DAT, sal, "r-", linewidth=1)
0 l4 B+ i3 s: P% ]: `7 E
#To save your graph
/ D4 h$ d% M& I# L7 }" U
plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
+ ]2 w. F( H% T% R
plt.show()
" ?5 m# O; t( U: B
" T1 e1 f( S% a+ l& U
Example 3:拟合曲线(Python)
2 u7 R3 g9 ^( _. p2 @5 J; ?
import csv
! E- }1 }8 \# K$ k import numpy as np
5 V& J% I8 r# J* K3 K2 R import pandas as pd
. e" I+ g, s* |6 a3 R- |5 p from datetime import datetime
% S/ d5 z; J+ O) u& g import matplotlib.pyplot as plt
! N. P( M+ g9 O& W- {+ ~! H2 H import scipy.signal as signal
( P4 ?, ^5 D( J) B9 t1 D data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
. k" ?, h$ k$ z" @& W
time=data[date [AST]]
) \$ _* R+ s1 Z8 P* {6 I0 b
temp=data[temperature [C]]
$ _+ ]) }4 \# e' @/ J/ F2 e8 P datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
/ ~( S4 d: M8 P' t
DATE,decday = [],[]
7 A; f" H I2 Y0 K e+ Y8 ` for row in time:
/ R9 Z" _& `, ~: L7 f! T daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
* v- c& I* |5 R" g
DATE.append(daterow)
& `( H5 @# Q+ V0 e8 z decday.append((daterow-datestart).total_seconds()/(3600*24))
& X" a0 B! \* D$ O! { # First, design the Buterworth filter
" g! S, E7 _- z' a- y6 W
N = 2 # Filter order
. ~; }+ y/ n9 n* X/ o Wn = 0.01 # Cutoff frequency
" R' @' j$ S2 f8 P5 H B, A = signal.butter(N, Wn, output=ba)
' f9 @ m7 P W# P" i' ^% F # Second, apply the filter
' I' ?; y1 w8 ]1 R% k) Z tempf = signal.filtfilt(B,A, temp)
# h* P% U; U% O9 U, q. Y # Make plots
" A, O$ D* G- B
fig = plt.figure()
0 K; f; S+ i S" ]' Y- V' j9 \ K ax1 = fig.add_subplot(211)
3 q$ l) J0 e+ E- w5 D6 [- |
plt.plot(decday,temp, b-)
8 Z$ ]3 i& I* i
plt.plot(decday,tempf, r-,linewidth=2)
; u$ u% ~$ G# j: j- e plt.ylabel("Temperature (oC)")
+ B" u4 S- @. Z0 A4 M2 }( A
plt.legend([Original,Filtered])
# T8 t, K1 X7 n2 C/ u plt.title("Temperature from LOBO (Halifax, Canada)")
6 i( z" a: W. H& K& S4 y& ~
ax1.axes.get_xaxis().set_visible(False)
/ j& |* ^9 ^5 C- d& I7 G) |) j ax1 = fig.add_subplot(212)
, v+ ~; k. ^* E5 f( f$ [. X+ m plt.plot(decday,temp-tempf, b-)
0 Y9 V6 i+ i- w. O e- Q/ b: d" O
plt.ylabel("Temperature (oC)")
5 Q7 k# }) C* P1 L* V/ g
plt.xlabel("Date")
7 H4 T# H* B; T
plt.legend([Residuals])
& l3 p/ G; A6 N. r0 \ plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
1 m7 a( `- R' q
plt.show()
2 \2 N$ l4 v* z7 _2 M3 c/ M/ R( ~
# S$ C* v* x& U& O
Example 4:三维地形(Python)
" L1 k+ a8 @3 d
# This import registers the 3D projection
0 J3 A4 G; J9 t: ~6 J
from mpl_toolkits.mplot3d import Axes3D
1 d. W* O+ Z1 S% C8 T
from matplotlib import cbook
6 H+ }3 \$ C+ J. H( O
from matplotlib import cm
& x# n& D3 c8 p! b( ^/ F from matplotlib.colors import LightSource
7 |, p$ E% g4 G' @2 ~ import matplotlib.pyplot as plt
) V: w1 c& i' _7 ?" [# q- G
import numpy as np
3 Q7 s7 o+ u8 c6 c5 f n
filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
, m4 \# h x o( z
with np.load(filename) as dem:
' Y6 s# o" ]# o2 ]6 x" g$ N z = dem[elevation]
' ~ v$ t4 K% E+ e3 Y, v n
nrows, ncols = z.shape
; G; p% d! W( i4 A. A( a x = np.linspace(dem[xmin], dem[xmax], ncols)
8 k! \# K7 U! h. F% w& B y = np.linspace(dem[ymin], dem[ymax], nrows)
% T/ J4 j/ Z( U0 Z# i# {. {
x, y = np.meshgrid(x, y)
" k! e0 N( y! _/ c2 ] region = np.s_[5:50, 5:50]
1 ^# v4 J9 ^5 b* b) q x, y, z = x[region], y[region], z[region]
" ~0 r$ I+ v U# m
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
% t$ F# |8 S9 }6 `9 w# W ls = LightSource(270, 45)
2 \ B0 s, q; H. H) L+ Y
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
/ X1 f, ^" }# {+ s- ]9 q$ h! n3 Y' c
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
& W9 q4 b$ b9 M( `8 X1 Q! l$ A linewidth=0, antialiased=False, shade=False)
* K& A: L7 N5 |* F6 J
plt.savefig(example4.png,dpi=600, bbox_inches=tight)
' Z, n% [* C7 |% c' \9 B plt.show()
9 |- Q$ k9 u: v% u6 ]1 w$ t4 Y, M
; `/ ^: k n! [) D T Example 5:三维地形,包含投影(Python)
. S" J9 @4 W7 T9 p) v
5 k0 F0 @3 r9 s* `( _6 w Example 6:切片,多维数据同时展现(Python)
5 W( |$ ]/ l" B2 P" q6 P1 s) h 8 G6 O% o1 r+ C2 F
Example 7:SSH GIF 动图展现(Matlab)
! i4 D% _, i4 Y2 e. _3 E! g
" j+ s; [3 h* X5 W/ Q) q" o
Example 8:Glider GIF 动图展现(Python)
; ]' @6 h- ^' g% V- D# O/ V
, {8 ]6 ~. G( p/ [1 w
Example 9:涡度追踪 GIF 动图展现
) O5 I7 t* b @
# ~' p) C9 \# V9 U# E( Y