簡體   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

我有一個函數可以根據給定的 x 和 y 坐標計算 az 值。 然后我想將這些值組合在一起以獲得 x、y、z 的 3D 數組。 我正在嘗試使用以下代碼執行此操作:

#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))

輸出: 輸出

我確實得到了一個 XYZ 數組,當我打印它時,這些值似乎正確映射,但是當我繪制數據時,它基本上顯示了“顛倒”的 y 值。 這是輸出應該是什么樣子,但在 x 軸上“翻轉”了。 此外,軸顯示節點編號而不是 X、Y 值。 我希望 0,0 點像笛卡爾坐標一樣是左下角,並且每個 x,y 都有一個對應的 z,它是根據給定的 x,y 計算出來的。 我知道必須有更簡單的方法來解決這個問題。 有人知道更好的方法嗎? 或者我在這里做錯了什么?

謝謝

ax.imshow() 有一個選項可以指定原點。

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

origin{'upper', 'lower'}, default: rcParams["image.origin"] (default: 'upper') 將數組的 [0, 0] 索引放在 Axes 的左上角或左下角. 約定(默認)'upper' 通常用於矩陣和圖像。

請注意,垂直軸向上指向“下”,而“上”指向下。

有關示例和更詳細的描述,請參閱 imshow 教程中的來源和范圍。

嘗試修改為:

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

對於軸標簽,可以使用以下命令更改它們

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM