Home > Software design >  Python: Creating a Grid of X,Y coordinates and corresponding calculated Z values to result in a 3D a
Python: Creating a Grid of X,Y coordinates and corresponding calculated Z values to result in a 3D a

Time:10-27

I have a function that calculates a z value from a given x and y coordinate. I then want to combine these values together to get a 3D array of x,y,z. 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: 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. This is what the output should look like but "flipped" over the x over axis. Additionally the axes show node numbers and not the X,Y values. 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. 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

CodePudding user response:

There is an option for ax.imshow() that allows to specify the origin point.

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. The convention (the default) 'upper' is typically used for matrices and images.

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.

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)
  • Related