'2D map from .xyz file
I'm a Python beginner and would like to create a 2D map from data in a .xyz file as shown below :
...
13 0 -0.11298451
14 0 -0.10656391
15 0 Nodata
16 0 Nodata
17 0 Nodata
....
The first column is x, the second y, and the third z.
Each time there is data in the third column I would like to set a black pixel, and each time there is Nodata a white pixel. At the end, I would like to get a map as shown in the figure below with x and y axis.
Could someone advise how to start?
I have started to write a small code but I have no idea how to continue
Many thanks for your help!
fig = plt.figure()
ax = fig.add_subplot(111)
x = pd.read_csv('path/data.txt',sep=' ',usecols=[0], header=None)
y = pd.read_csv('path/data.txt',sep=' ',usecols=[1], header=None)
z = pd.read_csv('path/data.txt',sep=' ',usecols=[2], header=None)
X,Y=np.meshgrid(x,y)
Solution 1:[1]
We can load this file into a numpy array using np.genfromtxt and create a 0-1 mask from it:
import numpy as np
import matplotlib.pyplot as plt
#load data into numpy array
my_data = np.genfromtxt("Essais1.prn")
#extract z-data and reshape into form x * y
z=my_data[:, 2].reshape(np.unique(my_data[:, 1]).size, np.unique(my_data[:, 0]).size)
#set NaN values to 0, everything else to 1
mask = np.where(np.isnan(z), 0, 1)
#display the mask with grayscale colormap
plt.imshow(mask, cmap="Greys", origin="lower")
#plt.axis("off")
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


