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

  • You need to add more details. Your data is in what format? Numpy Array? Add a reproducable code snippet. – BERA Feb 14 at 9:22
  • Hi BERA, thank you for the answer. I have added the code and the result but i can't upload the csv file. Anyway I also have the long lat coordinates of every point. – FM79 Feb 14 at 9:43
  • @BERA maybe i can add xconv, yconv and zconv values if you think that may be usefull – FM79 Feb 14 at 11:24
  • Do you want to turn it into raster or vector data in QGIS? What is your next step – BERA Feb 14 at 11:43
  • @BERA Maybe a raster data is better – FM79 Feb 14 at 11:49

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)

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:
  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)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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