TransWikia.com

Matplotlib integration in QGIS

Geographic Information Systems Asked by Snaileater on May 20, 2021

I just made some experiments with Matplotlib and what i noticed is that all that is generated using matplotlib appear in a new window that doesn’t seem to be tightly linked to qgis. The only “link” i see is a save button enabling plot export as raster images.

My aim would be to stack(/merge) graphics/plots or (georeferenced) shapes generated with matplotlib with layers in a qgis composer.

Is there a way to do that ? (i mean a tighter/quicker way than adding image objects in the composer).

Would it be a way to share a common “coordinate system” between map objects and rasters generated by Matplotlib ?

If possible could someone point me in the right direction ?

One Answer

You can do this using the matplotlib.backends.backend_qt4agg backend. The snippet below comes from a QGIS plugin but the key principles are a FigureCanvas which is used to create a widget added to a graphicsView

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from .lineStringGraph_dialog import lineStringGraphDialog #This is a ui file created in QTDesigner with a graphicsView object

self.dlg = YOUR-UI-FILENAME()
...code here to construct your figure...

self.scene = QGraphicsScene(self.dlg) 
canvas = FigureCanvas(figure)
self.scene.addWidget(canvas)
self.dlg.YOURgraphicsViewNAME.setScene(self.scene)
self.dlg.show()    

Answered by CitizenFish on May 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP