首页 > 解决方案 > numpy switch axes and reshape

问题描述

I have some geophysical data in a 2d numpy array. My target is to filter just one trace data and plot it. I am able to do it, but I need to switch my axes.

This is what I have for now:

enter image description here

So the shape is: (436,1771).

And this is what I have done:

    x = arr[:,1]
    
    fig,ax = plt.subplots()
    ax.plot(x,'k-')

    ax.set_xlim(0,500)
    
    plt.show()

Now that I have my trace, I want to switch my axes so I can see the trace vertically, but I´m having problems doing that and I get errors.

x2 = np.swapaxes(x,0,1)

I get an error saying " numpy.AxisError: axis2: axis 1 is out of bounds for array of dimension 1". I understand that this is because the shape of the array.

So, how can I reshape my arrar into (1771,436) and transpose the data?

标签: pythonnumpy

解决方案


Maybe try something along these lines?

plt.plot(arr[:,1], range(len(arr[:,1])),'k-')

Instead of pushing all the Y coordinates, I put all your Y-data as X data and want with a linear scaled axis along the Y-axis done with the range() command. If you just put Y-data, it will assume range(len(Y-data)) as X-data, which is what happens in your example. feel free to correct me if there is a mistake


推荐阅读