5 S. z# ]( g) ~% {2 K 在我们科研、工作中,将数据完美展现出来尤为重要。
- I/ @9 ?1 t, {
数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
2 R7 @, f9 r8 ]* W) k
下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
6 {6 m6 Y Q8 y" G$ M a& f+ z Example 1 :散点图、密度图(Python)
: e! ]+ x6 y9 p1 Q# w, D import numpy as np
* U/ i% k- t5 _$ ]6 ^$ t' y
import matplotlib.pyplot as plt
; w( {" J' q$ P2 l5 X # 创建随机数
! g. m" ^! y; j4 R& E
n = 100000
2 |& V5 }6 T" ` x = np.random.randn(n)
$ O6 ~( @* `! r/ D7 z! T- B% } y = (1.5 * x) + np.random.randn(n)
# A9 |/ K' F& U fig1 = plt.figure()
0 I; F9 [9 \, _' t! c6 k( O% T. d
plt.plot(x,y,.r)
3 k% P: s7 p( z5 Q2 l# H
plt.xlabel(x)
2 J9 s' ?/ I9 d# A: u, z5 D) P: |
plt.ylabel(y)
o% t- r* z5 ^5 N3 Z
plt.savefig(2D_1V1.png,dpi=600)
% ~+ b8 R5 v1 W: C I! B; [$ V8 O+ E nbins = 200
$ N, t7 M' z6 c. i
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
* \* g3 L. h0 C9 a/ Z$ I W( k # H needs to be rotated and flipped
1 q2 ^5 V4 O8 w8 }* X H = np.rot90(H)
* i& A3 u% n; x; s( _ H = np.flipud(H)
6 t$ h D) s4 ?9 ]( S3 J7 P
# 将zeros mask
5 Y4 r7 t$ O. p9 [ k
Hmasked = np.ma.masked_where(H==0,H)
: M- P1 C" y7 \# i" i ^ # Plot 2D histogram using pcolor
G1 z; M4 d! f, Q8 T* v fig2 = plt.figure()
3 |( f! p2 i# L& y2 Q* d
plt.pcolormesh(xedges,yedges,Hmasked)
. J( L8 ?; G6 a O+ U9 f# m" H
plt.xlabel(x)
: m0 s3 _; n: n- \) t plt.ylabel(y)
# \' N) j3 i# ?# J3 @
cbar = plt.colorbar()
; g. r' N0 Q/ n6 E
cbar.ax.set_ylabel(Counts)
& r/ D5 j$ p! u {2 k plt.savefig(2D_2V1.png,dpi=600)
! b7 i8 n7 m6 }
plt.show()
+ }9 [- Y! y" B3 f! S0 r$ y. {7 x
- ^6 m/ r6 i- Y- ^0 c: a4 i
* B( I2 [0 S, J( U- R2 f
打开凤凰新闻,查看更多高清图片
, z, J0 W. p8 A
; s3 U" i' g# l# g S
% p/ [& s; W8 B2 q% o; g8 z
+ V) l2 f u* f: b& r: b5 x Example 2 :双Y轴(Python)
2 X8 m0 G% U. `4 l; C import csv
1 _5 B- f! V( X L0 O! V import pandas as pd
' N x/ R7 Z7 Y import matplotlib.pyplot as plt
8 ^( E0 `- a+ S
from datetime import datetime
+ d& w6 }! [' `$ T
data=pd.read_csv(LOBO0010-2020112014010.tsv,sep=)
3 K) @" k% A8 C9 T3 T3 T; Z0 | time=data[date [AST]]
" I! p2 z: x) u sal=data[salinity]
( p( T% H) l+ o% ? tem=data[temperature [C]]
$ t. L; c \* Y
print(sal)
. V% F8 Z+ r% X% j; w- m: x' P
DAT = []
3 x' n' a9 Z9 T. z! x& W) e% ]
for row in time:
) K) c( Q! x4 C0 l* M# r+ |4 X9 t DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
; V) l/ j) O9 }) z; \* K #create figure
- A6 _5 k6 n6 c* u. C) o
fig, ax =plt.subplots(1)
% ~% I9 E! b, P; ]
# Plot y1 vs x in blue on the left vertical axis.
F4 U$ t& w8 l0 ^7 [5 o plt.xlabel("Date [AST]")
2 G- R# N2 U0 S8 F- p: y3 U
plt.ylabel("Temperature [C]", color="b")
% ]7 v. L9 D; G+ t. T6 ] B) y plt.tick_params(axis="y", labelcolor="b")
9 P9 ?+ Q. B4 G
plt.plot(DAT, tem, "b-", linewidth=1)
* k. D$ q. @0 }5 D- _ plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")
) s" G0 q. X# v, z0 A0 I4 d2 X fig.autofmt_xdate(rotation=50)
, }2 P6 q1 ^( t+ ]: y, ` # Plot y2 vs x in red on the right vertical axis.
: W" ^% V1 ]* i3 n; ]7 e7 N( \) n
plt.twinx()
- E. s! }! U) _3 l9 I plt.ylabel("Salinity", color="r")
# |& b" z" p3 _( \" c1 }: p plt.tick_params(axis="y", labelcolor="r")
5 t' l2 F$ L' O& ~ plt.plot(DAT, sal, "r-", linewidth=1)
- _ ~2 _- x+ e9 p* }5 u; O #To save your graph
1 `% r! H" I' Q7 T# J6 t- @
plt.savefig(saltandtemp_V1.png ,bbox_inches=tight)
+ F T: ^3 G6 U' {( p& ^
plt.show()
+ T% ~, k: x& u: b" N9 M
( U, i- Y% | V+ R
Example 3:拟合曲线(Python)
: e: e2 d8 t5 \+ ?" ? import csv
, P4 j. S" s. p
import numpy as np
5 B) k& h+ R' K! x
import pandas as pd
1 |1 O) v* ?- { from datetime import datetime
/ u3 F% T/ t. R& X/ {
import matplotlib.pyplot as plt
5 j4 Z3 ~: r$ S6 @3 j1 s& r" e! j; p E
import scipy.signal as signal
- o) ^+ D% q: h% m) k# S. i
data=pd.read_csv(LOBO0010-20201122130720.tsv,sep=)
/ Q$ ] T0 g' n) F% C
time=data[date [AST]]
8 ]: g9 R' A4 w3 V temp=data[temperature [C]]
5 U4 U- y4 I) R) E4 o7 _9 W# Y datestart = datetime.strptime(time[1],"%Y-%m-%d %H:%M:%S")
: J9 d: b) W7 z: H% K8 q+ O! M7 y
DATE,decday = [],[]
9 u. [8 u7 f; g3 J0 t+ z
for row in time:
- s, Z, ]9 A7 [4 K
daterow = datetime.strptime(row,"%Y-%m-%d %H:%M:%S")
( o: |9 p; D' V' W+ Q DATE.append(daterow)
. }+ ]9 q5 l9 Y decday.append((daterow-datestart).total_seconds()/(3600*24))
7 ]$ H4 C( I* c: T
# First, design the Buterworth filter
6 m/ Q6 z7 N8 n# d N = 2 # Filter order
7 |* A7 ^9 M1 h& F6 e Wn = 0.01 # Cutoff frequency
0 C- v7 G/ z: q7 S: X B, A = signal.butter(N, Wn, output=ba)
J6 D, W/ C# C3 l! C& F" Z9 {
# Second, apply the filter
* @/ ~6 _; |5 U: n: p: g9 a
tempf = signal.filtfilt(B,A, temp)
2 S* o4 g2 \0 A$ c6 r # Make plots
! T/ A% W8 b( r8 A1 N5 f fig = plt.figure()
! v# b2 K" v9 D% f/ j9 ~7 b, D
ax1 = fig.add_subplot(211)
4 h. M& v$ Q. | u0 V. f
plt.plot(decday,temp, b-)
$ j# L; H2 }" o/ e: k. u plt.plot(decday,tempf, r-,linewidth=2)
/ e+ Z) C6 m4 m* J" M plt.ylabel("Temperature (oC)")
0 o" q& b0 w6 Q& Z plt.legend([Original,Filtered])
5 }0 Q2 u K. n" A4 M; x
plt.title("Temperature from LOBO (Halifax, Canada)")
. c9 K5 N9 P8 `" Q ax1.axes.get_xaxis().set_visible(False)
- A. \6 [5 `+ _8 X( |
ax1 = fig.add_subplot(212)
/ x6 G& W2 ^' W$ G* g% C/ N9 x, j w6 G plt.plot(decday,temp-tempf, b-)
5 ]( N5 V H4 d( K& [3 | plt.ylabel("Temperature (oC)")
5 F: b: {" ?, B% M9 U9 b plt.xlabel("Date")
( i+ y1 O0 a1 J$ q B
plt.legend([Residuals])
3 |1 y6 J0 I$ S2 V. U4 V
plt.savefig(tem_signal_filtering_plot.png, bbox_inches=tight)
+ v3 r5 W+ r5 K- T) [* T0 _ plt.show()
, B3 I' B* p- u* ?7 t2 o2 @
- A4 ^$ T/ @! ^+ \" G+ e9 r Example 4:三维地形(Python)
, |: J; i: T; c3 t% Y o/ X
# This import registers the 3D projection
3 a# u# d: x9 Z2 U0 ~: T: l from mpl_toolkits.mplot3d import Axes3D
8 C; g9 j. }4 t: ?) o
from matplotlib import cbook
$ L# Z+ X7 \! T! T! F' Z2 K
from matplotlib import cm
$ f a4 @' A! Q6 a( ?- @ from matplotlib.colors import LightSource
! B+ n3 L' B) @6 u ~! M! b import matplotlib.pyplot as plt
]1 O5 m1 \6 M3 S3 ~- V
import numpy as np
3 K) r8 Q7 C, J+ x: j% z; h7 C
filename = cbook.get_sample_data(jacksboro_fault_dem.npz, asfileobj=False)
- k5 W2 R2 I9 ~$ U6 f2 H/ [3 _ with np.load(filename) as dem:
: S' q, Y* I% [3 s: p% N$ D' i
z = dem[elevation]
) m+ m% L4 e5 e- ]% Z nrows, ncols = z.shape
! z: i! p+ e* A4 n+ d x = np.linspace(dem[xmin], dem[xmax], ncols)
; U7 \4 H$ Z T _. l8 J y = np.linspace(dem[ymin], dem[ymax], nrows)
8 _. i) i6 a" g. \8 p' j x, y = np.meshgrid(x, y)
' L: R$ ~9 J) E- I; W region = np.s_[5:50, 5:50]
' R& \2 N1 i% w6 o* U2 S x, y, z = x[region], y[region], z[region]
" n4 e6 [. l9 I t. V
fig, ax = plt.subplots(subplot_kw=dict(projection=3d))
9 C6 O- j% Y3 D2 h# Q3 ` ls = LightSource(270, 45)
9 |2 M0 H: C/ g* H
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode=soft)
0 Z ~+ ]' x% t6 N8 e; j3 }
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
- C3 g% l2 s2 _& |* B linewidth=0, antialiased=False, shade=False)
6 X: c& w$ w6 N/ M* Y" {+ o6 M
plt.savefig(example4.png,dpi=600, bbox_inches=tight)
. s- o6 }" E, b
plt.show()
& x9 l. `" J: k& c5 A) B
/ u' K1 A* r: ~ Example 5:三维地形,包含投影(Python)
7 w. r2 ^2 O- Y9 I* Y3 e, S 1 ?3 [& x- x5 S0 I) }8 s
Example 6:切片,多维数据同时展现(Python)
7 Q$ M) {( c6 y
2 g" ?" J r0 o @+ h7 y
Example 7:SSH GIF 动图展现(Matlab)
$ D# Y8 R9 o5 l8 o
* @, ?# W3 l( l4 Q Example 8:Glider GIF 动图展现(Python)
9 N0 o2 s5 L2 F, A& x
6 [0 s* o9 d: v' w
Example 9:涡度追踪 GIF 动图展现
6 w7 E7 N( K" D* W8 O5 U3 v) k8 x2 p
% I4 j. _6 u3 O9 ]0 B9 r# P