Matlab、Python为工具解析数据可视化之美
程序员文章站
2022-03-10 08:09:36
在我们科研、工作中,将数据完美展现出来尤为重要。数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。下面介绍一些数据可视化的作品...
在我们科研、工作中,将数据完美展现出来尤为重要。
数据可视化是以数据为视角,探索世界。我们真正想要的是 — 数据视觉,以数据为工具,以可视化为手段,目的是描述真实,探索世界。
下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科。
example 1 :散点图、密度图(python)
import numpy as np import matplotlib.pyplot as plt # 创建随机数 n = 100000 x = np.random.randn(n) y = (1.5 * x) + np.random.randn(n) fig1 = plt.figure() plt.plot(x,y,'.r') plt.xlabel('x') plt.ylabel('y') plt.savefig('2d_1v1.png',dpi=600) nbins = 200 h, xedges, yedges = np.histogram2d(x,y,bins=nbins) # h needs to be rotated and flipped h = np.rot90(h) h = np.flipud(h) # 将zeros mask hmasked = np.ma.masked_where(h==0,h) # plot 2d histogram using pcolor fig2 = plt.figure() plt.pcolormesh(xedges,yedges,hmasked) plt.xlabel('x') plt.ylabel('y') cbar = plt.colorbar() cbar.ax.set_ylabel('counts') plt.savefig('2d_2v1.png',dpi=600) plt.show()
example 2 :双y轴(python)
import csv import pandas as pd import matplotlib.pyplot as plt from datetime import datetime data=pd.read_csv('lobo0010-2020112014010.tsv',sep='\t') time=data['date [ast]'] sal=data['salinity'] tem=data['temperature [c]'] print(sal) dat = [] for row in time: dat.append(datetime.strptime(row,"%y-%m-%d %h:%m:%s")) #create figure fig, ax =plt.subplots(1) # plot y1 vs x in blue on the left vertical axis. plt.xlabel("date [ast]") plt.ylabel("temperature [c]", color="b") plt.tick_params(axis="y", labelcolor="b") plt.plot(dat, tem, "b-", linewidth=1) plt.title("temperature and salinity from lobo (halifax, canada)") fig.autofmt_xdate(rotation=50) # plot y2 vs x in red on the right vertical axis. plt.twinx() plt.ylabel("salinity", color="r") plt.tick_params(axis="y", labelcolor="r") plt.plot(dat, sal, "r-", linewidth=1) #to save your graph plt.savefig('saltandtemp_v1.png' ,bbox_inches='tight') plt.show()
example 3:拟合曲线(python)
import csv import numpy as np import pandas as pd from datetime import datetime import matplotlib.pyplot as plt import scipy.signal as signal data=pd.read_csv('lobo0010-20201122130720.tsv',sep='\t') time=data['date [ast]'] temp=data['temperature [c]'] datestart = datetime.strptime(time[1],"%y-%m-%d %h:%m:%s") date,decday = [],[] for row in time: daterow = datetime.strptime(row,"%y-%m-%d %h:%m:%s") date.append(daterow) decday.append((daterow-datestart).total_seconds()/(3600*24)) # first, design the buterworth filter n = 2 # filter order wn = 0.01 # cutoff frequency b, a = signal.butter(n, wn, output='ba') # second, apply the filter tempf = signal.filtfilt(b,a, temp) # make plots fig = plt.figure() ax1 = fig.add_subplot(211) plt.plot(decday,temp, 'b-') plt.plot(decday,tempf, 'r-',linewidth=2) plt.ylabel("temperature (oc)") plt.legend(['original','filtered']) plt.title("temperature from lobo (halifax, canada)") ax1.axes.get_xaxis().set_visible(false) ax1 = fig.add_subplot(212) plt.plot(decday,temp-tempf, 'b-') plt.ylabel("temperature (oc)") plt.xlabel("date") plt.legend(['residuals']) plt.savefig('tem_signal_filtering_plot.png', bbox_inches='tight') plt.show()
example 4:三维地形(python)
# this import registers the 3d projection from mpl_toolkits.mplot3d import axes3d from matplotlib import cbook from matplotlib import cm from matplotlib.colors import lightsource import matplotlib.pyplot as plt import numpy as np filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=false) with np.load(filename) as dem: z = dem['elevation'] nrows, ncols = z.shape x = np.linspace(dem['xmin'], dem['xmax'], ncols) y = np.linspace(dem['ymin'], dem['ymax'], nrows) x, y = np.meshgrid(x, y) region = np.s_[5:50, 5:50] x, y, z = x[region], y[region], z[region] fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) ls = lightsource(270, 45) rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft') surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb, linewidth=0, antialiased=false, shade=false) plt.savefig('example4.png',dpi=600, bbox_inches='tight') plt.show()
example 5:三维地形,包含投影(python)
example 6:切片,多维数据同时展现(python)
example 7:ssh gif 动图展现(matlab)
example 8:glider gif 动图展现(python)
example 9:涡度追踪 gif 动图展现
到此这篇关于数据可视化之美 -- 以matlab、python为工具的文章就介绍到这了,更多相关python数据可视化之美内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: day3 各种运算符
下一篇: numpy数据类型dtype转换实现