0

I'm new in QGIS and I’m having some troubles. I have produced a surface plot in python and I would like to import it in QGIS but I don't know how do that, can anyone help me?

P.S. I also have the long lat coordinates of the plot for the geo referencing

FM79
  • 9
  • 2

1 Answers1

1

Using Writing numpy array to raster file as suggested by BERA is the easiest way

With one of my examples

Matplotlib 2D:

enter image description here

Matplotlib 3D (with def axisEqual3D(ax) in set matplotlib 3d plot aspect ratio):

ax.plot_surface(xconv, yconv, zconv, rstride=1, cstride=1, cmap='gist_earth',antialiased=True)
ax.view_init(60,-160)
axisEqual3D(ax)

enter image description here

Convert it into an ASCII Raster

xmin,ymin,xmax,ymax = [xconv,.min(),yconv.min(),xconv.max(),yconv.max()]
nrows,ncols = np.shape(zconv)
xres = (xmax-xmin)/float(ncols)
yres = (ymax-ymin)/float(nrows)
geotransform=(xmin,xres,0,ymax,0, -yres)  
# creation of the raster file 
header = "ncols     %s\n" % ncols
header += "nrows    %s\n" % nrows
header += "xllcorner %s\n" % geotransform[0]
header += "yllcorner %s\n" % geotransform[3]
header += "dx  %s\n" % geotransform[1]
header += "dy  %s\n" % geotransform[5]
header += "NODATA_value -9999\n"
with  open("grid.asc", "w") as f:
  f.write(header)
  np.savetxt(f,zconv, fmt="%1.2f")

grid.asc content

ncols 100
nrows 100
xllcorner 251215.52097430476
yllcorner 46722.44250503713
dx 11.230954009630368
dy -8.647123208649791
NODATA_value -9999
395.37 395.23 395.10 ...

Result in QGIS:

enter image description here

But you can also use osgeo.gdal (Writing Numpy array to raster file (tif) returns a trivial black square) or rasterio (Lesson 1. Export Numpy Arrays to Geotiff Format Using Rasterio and Python)

gene
  • 54,868
  • 3
  • 110
  • 187