Диагональное сканирование графика - Python - PullRequest
0 голосов
/ 08 ноября 2019

Я пытаюсь написать какой-то код, который принимает разрез фрагмента графика на рисунке 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))

Рисунок 1 - Начальный график Рисунок 2 - Вертикальный срез

...