图形的对角线扫描 - Python

2024-06-01 00:44:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一些代码,对图1中的图形进行切片。 目前,我已经能够进行垂直(和水平)切割,如图2所示。 我需要调整代码,现在采取对角线削减,但不知道如何

我在下面包含了垂直切割的代码(注意,它包括用于绘制初始图形的代码):

import numpy
import matplotlib.pyplot as pyplot
import scipy.interpolate 

# Open Data #
'opens data and reads in the file '
data=numpy.genfromtxt('Scan1.txt')
theta2=data[:,0]
omega=data[:,1]
intens=data[:,2]

# Convert To Q and Map Coords #
qx= (4.0*numpy.pi/1.579)*numpy.sin(theta2*numpy.pi/180.0/2.0)*numpy.sin((omega-theta2/2.0)*numpy.pi/180.0)
qz= (4.0*numpy.pi/1.579)*numpy.sin(theta2*numpy.pi/180.0/2.0)*numpy.cos((omega-theta2/2.0)*numpy.pi/180.0)

coords=zip(qx,qz)
coords = list(coords)
f=scipy.interpolate.LinearNDInterpolator(coords,intens,fill_value=numpy.nan, rescale=False)

x_coords = numpy.linspace(min(qx),max(qx),500) # Start, stop, number of points
y_coords = numpy.linspace(min(qz),max(qz),500)
X,Y=numpy.meshgrid(x_coords,y_coords) # Create grid
X = X.reshape((numpy.prod(X.shape),)) # Reshape 
Y = Y.reshape((numpy.prod(Y.shape),))

newcoords = (X, Y) # Map coords

Z = f(newcoords)
pyplot.figure()
pyplot.title('Interpolated Data')
pyplot.xlabel('Qx')
pyplot.ylabel('Qz')
pyplot.scatter(X,Y,c=numpy.log(Z))
pyplot.colorbar()
pyplot.show()
Z=numpy.nan_to_num(Z)

# Vertical Scan #
vertcenpos = 0.0096 #Central position of cut
vertwidth = 0.005 #Width of slice

def find_nearest(array,value):
    'FUNCTION TO FIND NEAREST VALUE'
    idx = (numpy.abs(numpy.subtract(array, value))).argmin()
    return array[idx]

def position_finder(array,value):
    'FUNCTION TO FIND CORRESPONDING POSITION IN ANOTHER ARRAY'
    a = find_nearest(array, value)
    b=[i for i,x in enumerate(array) if x == a]
    position=b[0]
    return position

cen_index=position_finder(x_coords,vertcenpos)
win_index_low=position_finder(x_coords,vertcenpos-vertwidth)
win_index_high=position_finder(x_coords,vertcenpos+vertwidth)
xnew=x_coords[win_index_low:win_index_high]
ynew=y_coords#[win_index_low:win_index_high]
X,Y=numpy.meshgrid(xnew,ynew) # Create grid
X = X.reshape((numpy.prod(X.shape),)) # Reshape
Y = Y.reshape((numpy.prod(Y.shape),))
newcoords = (X, Y) # Map coords
Z = f(newcoords) # Create interpolated map
Z=numpy.nan_to_num(Z)

pyplot.figure()
pyplot.title('Slice in Qx')
pyplot.xlabel('Qx')
pyplot.ylabel('Qz')
pyplot.scatter(X,Y,c=numpy.log(Z))
pyplot.colorbar()
pyplot.xlim(min(x_coords),max(x_coords))

Figure 1 - Initial GraphFigure 2 - Vertical slice


Tags: 代码numpydataindexvaluepipositioncoords