【问题标题】:plotting seismic wiggle traces using matplotlib使用 matplotlib 绘制地震摆动轨迹
【发布时间】:2016-02-12 14:02:27
【问题描述】:

我正在尝试使用 matplotlib 重新创建上述绘图风格。

原始数据存储在 2D numpy 数组中,其中快轴是时间。

绘制线条很容易。我正在尝试有效地获得阴影区域。

我目前的尝试看起来像:

import numpy as np
from matplotlib import collections
import matplotlib.pyplot as pylab

#make some oscillating data
panel = np.meshgrid(np.arange(1501), np.arange(284))[0]
panel = np.sin(panel)

#generate coordinate vectors.
panel[:,-1] = np.nan #lazy prevents polygon wrapping 
x = panel.ravel()
y = np.meshgrid(np.arange(1501), np.arange(284))[0].ravel() 

#find indexes of each zero crossing
zero_crossings = np.where(np.diff(np.signbit(x)))[0]+1 

#calculate scalars used to shift "traces" to plotting corrdinates
trace_centers = np.linspace(1,284, panel.shape[-2]).reshape(-1,1) 
gain = 0.5 #scale traces

#shift traces to plotting coordinates
x = ((panel*gain)+trace_centers).ravel()

#split coordinate vectors at each zero crossing
xpoly = np.split(x, zero_crossings)
ypoly = np.split(y, zero_crossings)

#we only want the polygons which outline positive values
if x[0] > 0:
    steps = range(0, len(xpoly),2)
else:
    steps = range(1, len(xpoly),2)

#turn vectors of polygon coordinates into lists of coordinate pairs
polygons = [zip(xpoly[i], ypoly[i]) for i in steps if len(xpoly[i]) > 2]

#this is so we can plot the lines as well
xlines = np.split(x, 284)
ylines = np.split(y, 284)
lines = [zip(xlines[a],ylines[a]) for a in range(len(xlines))]  

#and plot
fig = pylab.figure()
ax = fig.add_subplot(111)
col = collections.PolyCollection(polygons)
col.set_color('k')
ax.add_collection(col, autolim=True)
col1 = collections.LineCollection(lines)
col1.set_color('k')
ax.add_collection(col1, autolim=True)
ax.autoscale_view()
pylab.xlim([0,284])
pylab.ylim([0,1500])
ax.set_ylim(ax.get_ylim()[::-1])
pylab.tight_layout()
pylab.show()

结果是

有两个问题:

  1. 它没有完美填充,因为我在最接近零交叉的数组索引上进行拆分,而不是精确的零交叉。我假设计算每个过零将是一个巨大的计算冲击。

  2. 性能。考虑到问题的规模,这还不错——在我的笔记本电脑上渲染大约需要一秒钟,但我想把它降低到 100 毫秒 - 200 毫秒。

由于使用案例,我仅限于使用 numpy/scipy/matplotlib 的 python。有什么建议吗?

跟进:

结果表明,只需很少的计算负载就可以对零交叉进行线性插值。通过将插值插入数据中,将负值设置为 nans,并使用对 pyplot.fill 的一次调用,可以在大约 300 毫秒内绘制 500,000 个奇数样本。

作为参考,Tom 在以下相同数据上的方法耗时约 8 秒。

以下代码假设输入一个 numpy 重新数组,其 dtype 模拟地震 unix 标头/跟踪定义。

def wiggle(frame, scale=1.0):
        fig = pylab.figure()
        ax = fig.add_subplot(111)        
        ns = frame['ns'][0]
        nt = frame.size
        scalar = scale*frame.size/(frame.size*0.2) #scales the trace amplitudes relative to the number of traces
        frame['trace'][:,-1] = np.nan #set the very last value to nan. this is a lazy way to prevent wrapping
        vals = frame['trace'].ravel() #flat view of the 2d array.
        vect = np.arange(vals.size).astype(np.float) #flat index array, for correctly locating zero crossings in the flat view
        crossing = np.where(np.diff(np.signbit(vals)))[0] #index before zero crossing
        #use linear interpolation to find the zero crossing, i.e. y = mx + c. 
        x1=  vals[crossing]
        x2 =  vals[crossing+1]
        y1 = vect[crossing]
        y2 = vect[crossing+1]
        m = (y2 - y1)/(x2-x1)
        c = y1 - m*x1       
        #tack these values onto the end of the existing data
        x = np.hstack([vals, np.zeros_like(c)])
        y = np.hstack([vect, c])
        #resort the data
        order = np.argsort(y) 
        #shift from amplitudes to plotting coordinates
        x_shift, y = y[order].__divmod__(ns)
        ax.plot(x[order] *scalar + x_shift + 1, y, 'k')
        x[x<0] = np.nan
        x = x[order] *scalar + x_shift + 1
        ax.fill(x,y, 'k', aa=True) 
        ax.set_xlim([0,nt])
        ax.set_ylim([ns,0])
        pylab.tight_layout()
        pylab.show()

完整代码发布于https://github.com/stuliveshere/PySeis

【问题讨论】:

    标签: python numpy matplotlib plot


    【解决方案1】:

    您可以使用fill_betweenx 轻松完成此操作。来自文档:

    在两条水平曲线之间制作填充多边形。

    调用签名:

    fill_betweenx(y, x1, x2=0, where=None, **kwargs) 创建一个 PolyCollection 填充 x1 和 x2 之间 where==True 的区域

    这里的重要部分是where 参数。

    所以,你想拥有x2 = offset,然后拥有where = x&gt;offset

    例如:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig,ax = plt.subplots()
    
    # Some example data
    y = np.linspace(700.,900.,401)
    offset = 94.
    x = offset+10*(np.sin(y/2.)*
            1/(10. * np.sqrt(2 * np.pi)) *
            np.exp( - (y - 800)**2 / (2 * 10.**2))
            ) # This function just gives a wave that looks something like a seismic arrival
    
    ax.plot(x,y,'k-')
    ax.fill_betweenx(y,offset,x,where=(x>offset),color='k')
    
    ax.set_xlim(93,95)
    
    plt.show()
    

    您需要为每个偏移量执行fill_betweenx。例如:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig,ax = plt.subplots()
    
    # Some example data
    y = np.linspace(700.,900.,401)
    offsets = [94., 95., 96., 97.]
    times = [800., 790., 780., 770.]
    
    for offset, time in zip(offsets,times):
        x = offset+10*(np.sin(y/2.)*
            1/(10. * np.sqrt(2 * np.pi)) *
            np.exp( - (y - time)**2 / (2 * 10.**2))
            )
    
        ax.plot(x,y,'k-')
        ax.fill_betweenx(y,offset,x,where=(x>offset),color='k')
    
    ax.set_xlim(93,98)
    
    plt.show()
    

    【讨论】:

    • 绘制精美,但分析表明它比我的方法慢 5 倍左右,我假设这是因为您必须遍历每个跟踪,因此您要绘制数百个较小的集合而不是一个大的集合一。今晚我会更深入地研究分析。
    【解决方案2】:

    如果您的地震轨迹为 SEGY 格式和/或 txt 格式(您最终需要将它们设为 .txt 格式),这很容易做到。花了很长时间寻找最好的方法。对python和编程也很陌生,所以请温柔一点。

    为了将 SEGY 文件转换为 .txt 文件,我使用了 SeiSee(http://dmng.ru/en/freeware.html;不要介意俄罗斯网站,它是一个合法程序)。要加载和显示,您需要 numpy 和 matplotlib。

    以下代码将加载地震轨迹、转置它们并绘制它们。显然,您需要加载自己的文件,更改垂直和水平范围,并使用 vmin 和 vmax 进行一些调整。它还使用灰色颜色图。代码会生成这样的图像:http://goo.gl/0meLyz

    import numpy as np
    import matplotlib.pyplot as plt
    
    traces = np.loadtxt('yourtracestxtfile.txt')
    traces = np.transpose(traces)
    
    seismicplot = plt.imshow(traces[3500:4500,500:900], cmap = 'Greys',vmin = 0,vmax = 1,aspect = 'auto') #Tip: traces[vertical range,horizontal range]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多