简体   繁体   English

Python:创建 X、Y 坐标和相应的计算 Z 值的网格以生成 XYZ 的 3D 数组

[英]Python: Creating a Grid of X,Y coordinates and corresponding calculated Z values to result in a 3D array of XYZ

I have a function that calculates az value from a given x and y coordinate.我有一个函数可以根据给定的 x 和 y 坐标计算 az 值。 I then want to combine these values together to get a 3D array of x,y,z.然后我想将这些值组合在一起以获得 x、y、z 的 3D 数组。 I'm attempting to do this with the code below:我正在尝试使用以下代码执行此操作:

#import packages

import pandas as pd
import math
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.tri as tri
import matplotlib.pyplot as plt
from matplotlib import rcParams
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.mplot3d import Axes3D

#Define function to calculate z over a grid
def func(X, Y, x, y, Q):
    return (Q / (2 * np.pi)) * np.arctan((y-Y)/(x-X))

#For initial testing just defining the IW explicitly, last step will be to read the input file and pull this data
X1=2417743.658
Y1=806346.704
Q1=5
X2=2417690.718
Y2=806343.693
Q2=5
X3=2417715.221
Y3=806309.685
Q3=5

#initiate the XY grid
xi = np.linspace(2417675,2417800,625)
yi = np.linspace(806300,806375,375)

#mesh the grid in to x,y space
x,y = np.meshgrid(xi,yi)

#calculate the values over the grid at every x,y using the defined function above
zi = (func(X1,Y1,x,y,Q1)+func(X2,Y2,x,y,Q2)+func(X3,Y3,x,y,Q3))

#reshape the xy space into 3d space - when i plot this grid it looks correct
xy = np.array([[(x, y) for x in xi] for y in yi])

#reshape z into 3d space -  this appears to be where the issue begins
z = np.array(zi).reshape(xy.shape[0],xy.shape[1], -1)

#combined xyz into a single grid
xyz = np.concatenate((xy, z), axis = -1)

# Create figure and add axis
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)

img = ax.imshow((xyz*255).astype(np.uint8))

output:输出: 输出

I do get an XYZ array and when i print it the values appear to be mapping correctly, however when I plot the data, it shows the y values "upside down" essentially.我确实得到了一个 XYZ 数组,当我打印它时,这些值似乎正确映射,但是当我绘制数据时,它基本上显示了“颠倒”的 y 值。 This is what the output should look like but "flipped" over the x over axis.这是输出应该是什么样子,但在 x 轴上“翻转”了。 Additionally the axes show node numbers and not the X,Y values.此外,轴显示节点编号而不是 X、Y 值。 I want the 0,0 point to be the lower left hand corner like cartesian coordinates, and each x,y have a corresponding z which is calculated from that given x,y.我希望 0,0 点像笛卡尔坐标一样是左下角,并且每个 x,y 都有一个对应的 z,它是根据给定的 x,y 计算出来的。 I know there must be an easier way to go about this.我知道必须有更简单的方法来解决这个问题。 Does anyone know a better way?有人知道更好的方法吗? or maybe what i'm doing wrong here?或者我在这里做错了什么?

Thanks谢谢

There is an option for ax.imshow() that allows to specify the origin point. ax.imshow() 有一个选项可以指定原点。

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html

origin{'upper', 'lower'}, default: rcParams["image.origin"] (default: 'upper') Place the [0, 0] index of the array in the upper left or lower left corner of the Axes. origin{'upper', 'lower'}, default: rcParams["image.origin"] (default: 'upper') 将数组的 [0, 0] 索引放在 Axes 的左上角或左下角. The convention (the default) 'upper' is typically used for matrices and images.约定(默认)'upper' 通常用于矩阵和图像。

Note that the vertical axis points upward for 'lower' but downward for 'upper'.请注意,垂直轴向上指向“下”,而“上”指向下。

See the origin and extent in imshow tutorial for examples and a more detailed description.有关示例和更详细的描述,请参阅 imshow 教程中的来源和范围。

Try to modify to this:尝试修改为:

img = ax.imshow((xyz*255).astype(np.uint8), origin='lower')

For the axis labels they can be changed with the following commands对于轴标签,可以使用以下命令更改它们

ax.set_xticks(LIST_OF_INDICIES)
ax.set_xticklabels(LIST_OF_VALUES)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 3d将x,y,z坐标转换为3d numpy数组 - 3d coordinates x,y,z to 3d numpy array 在python中卷积具有三个内核(x,y,z)的3D数组 - Convolve a 3D array with three kernels (x, y, z) in python Python-在2D网格上对x,y,z值进行装箱 - Python - Binning x,y,z values on a 2D grid 在3D numpy对象中查找与给定z值对应的x和y值 - Finding x and y values corresponding to given z value in 3D numpy object 获取 3D 中平面给定 x 和 z 的 y 坐标 - Get y coordinates given x and z for a plane in 3D 如何在Python中获取每次单击的3D模型的(x,y,z)坐标? - How to get the (x,y,z) coordinates of a 3D model of every click in Python? 在给定 x,y,z 坐标时使用 DBSCAN 算法使用 python 对 3D 点进行聚类 - Clustering the 3D points when given the x,y,z coordinates using DBSCAN algorithm using python Python,Axes3D,绘图:无法附加到我的3D散点图中的X,Y,Z值列表 - Python, Axes3D, plotting: Cannot append to List of X,Y,Z values in my 3D scatterplot 获得x,y,z,通过3D数据阵列表示 - getting x,y,z, mean through a 3D data array 如何制作 3D plot(X,Y,Z),将 Z 值分配给 X,Y 有序对? - How to make a 3D plot (X, Y, Z), assigning Z values to X,Y ordered pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM