Question

I have a set of X and Y coordinates and each point has a different pixel value - the Z quantity. I would like to plot these values using a raster or contour plot.

I am having difficulty doing this because there is no mathematical relationship between the pixel value and the X and Y coordinates.

I have created an array for the range of x and y values and I have tried constructing a dictionary where I can look up the value of z using a concatenated x and y string. At the moment I am having an index issue and I am wondering if there is a better way of achieving this?

My code so far is:

import matplotlib.pyplot as plt
import numpy as np

XY_Zpoints = {'11':8,
             '12':8,
             '13':8,
             '14':6,
             '21':6,
             '22':8,      
             '23':6,
             '24':6,
             '31':8,
             '32':3, 
             '33':8,
             '34':6,
             '41':8,
             '42':3, 
             '43':3,
             '44':8,
                   }

x, y = np.meshgrid(np.linspace(1,4,4), np.linspace(1,4,4))

z = XY_Zpoints[str(x)+str(y)]

# Plot the grid
plt.imshow(z)
plt.spectral()
plt.show()

Thanks in advance for any help you can offer!

Was it helpful?

Solution

Instead of a dictionary, you can use a numpy array where the position of each pixel value coresponds to the x and y coordinates. For your example this array would look like:

z = np.array([[8, 8, 8, 6], [6, 8, 6, 6], [8, 3, 8, 6], [8, 3, 3, 8]])

To access the pixel value at x = 2 and y = 3, for example you can do this:

x = 2
y = 3
pixel = z[x-1][y - 1]

z can be displayed with:

imshow(z)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top