0

I am trying to use the PyKrige module in Python to interpolate via Ordinary Kriging my XYZ dataset.

I have the below script, code taken from https://colab.research.google.com/drive/1mZDVJ-RioBc4QHHpe2HBNJrx8sJ4-1Lw?pli=1#scrollTo=6OfzYklg_Db1

import numpy as np
import matplotlib.pyplot as plt
import pykrige.kriging_tools as kt
from pykrige.ok import OrdinaryKriging
from pylab import *
import pandas as pd


x1 = np.loadtxt("c:/test_file.xyz", usecols=0, dtype='float')
y1 = np.loadtxt("c:/test_file.xyz", usecols=0, dtype='float')
z1 = np.loadtxt("c:/test_file.xyz", usecols=0, dtype='float')

x = list(x1)
y = list(y1)
z = list(z1)

cax = plt.scatter(x, y, c=z)
cbar = plt.colorbar(cax, fraction=0.03)
plt.title('DensA Points')


OK = OrdinaryKriging(
    x, 
    y, 
    z, 
    variogram_model='gaussian',
    verbose=True,
    enable_plotting=False,
    nlags=5,
)

OK.variogram_model_parameters

gridx = np.arange(626000, 629100, 5, dtype='float32')
gridy = np.arange(231500, 234500, 5, dtype='float32')
zstar, ss = OK.execute("grid", gridx, gridy)

print(zstar.shape)
print(ss.shape)

cax = plt.imshow(zstar, extent=(626000, 629100, 231500, 234500), origin='lower')
savefig( 'C:/users/public/test.tiff' )
print('image saved')

The code works, but my issue is the X and Y axis.

I only want the interpolated tiff image so that I can bring it into QGIS etc. However, the output is giving me an X and Y axis, and I'm not sure how to remove them without breaking the code.

2
  • 1
    Your script, save result as a matplotlib image, while PyKrige allows you to save the result as an ASCII grid file (*.asc). which can be used in QGIS
    – gene
    Sep 15, 2022 at 11:43
  • you can try : plt.axis('off') in your code, this should tell pyplot to not show the axis. however another format could be a better idea as @gene say Sep 15, 2022 at 12:09

1 Answer 1

0

You need kt.write_asc_grid() which will create a .asc file you can import in QGIS

import numpy as np
import matplotlib.pyplot as plt
import pykrige.kriging_tools as kt
from pykrige.ok import OrdinaryKriging
from pylab import *
import pandas as pd


x1 = np.loadtxt("c:/test_file.xyz", usecols=0, dtype='float')
y1 = np.loadtxt("c:/test_file.xyz", usecols=0, dtype='float')
z1 = np.loadtxt("c:/test_file.xyz", usecols=0, dtype='float')

x = list(x1)
y = list(y1)
z = list(z1)

cax = plt.scatter(x, y, c=z)
cbar = plt.colorbar(cax, fraction=0.03)
plt.title('DensA Points')


OK = OrdinaryKriging(
    x, 
    y, 
    z, 
    variogram_model='gaussian',
    verbose=True,
    enable_plotting=False,
    nlags=5,
)

OK.variogram_model_parameters

gridx = np.arange(626000, 629100, 5, dtype='float32')
gridy = np.arange(231500, 234500, 5, dtype='float32')
zstar, ss = OK.execute("grid", gridx, gridy)

print(zstar.shape)
print(ss.shape)

kt.write_asc_grid(gridx, gridy, zstar, filename="output.asc")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.