Discussion:
[pyqtgraph] How Does a Pyqtgraph Export for Three Subplots Look Like?
GR
2018-09-10 16:23:07 UTC
Permalink
Hi,

I posted a question on stackoverflow:

https://stackoverflow.com/questions/52250075/how-does-a-pyqtgraph-export-for-three-subplots-look-like

I am unsure about how much attention it would rise there. Therefore I would
like to draw your attention to that question.

I hope you do not mind.

If you have an idea how my question can be solved: thanks in advance!

Gerhard
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/55295a81-6000-4889-83c1-eafa086d78e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Patrick
2018-09-11 02:29:38 UTC
Permalink
Hi,
I'm pretty sure you can do this (update plot and export, without plot being
updated on screen), but I think the plot axes still needs to be displayed
somewhere initially so that the mapping from plot to device coordinates
gets set up. Maybe there's a way to do that without showing the plot
window, but I don't know.

In your example code you never call app.exec_(), so the Qt event loop never
starts, so none of the required initialisation happens. If you run a normal
app with the window displayed, then trigger the export from a
button/timer/event etc, then it should work.

As an aside, I use pyqtgraph to plot inside my applications due to fast
screen updates and interactivity, but produce output plot files with
matplotlib (as pdf or png). So when the user hits the "save" button, I pass
the data off to a separate matplotlib plotting method as it has prettier
and more flexible plotting options. For performance, you can keep the
matplotlib figure around and just modify the plot data with set_data
methods rather than recreating the whole plot again. If that still isn't
fast enough, then maybe store raw data and analyse/plot afterwards.
Post by GR
Hi,
https://stackoverflow.com/questions/52250075/how-does-a-pyqtgraph-export-for-three-subplots-look-like
I am unsure about how much attention it would rise there. Therefore I
would like to draw your attention to that question.
I hope you do not mind.
If you have an idea how my question can be solved: thanks in advance!
Gerhard
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/dd796d68-6817-481c-b9f3-228aa4c07742%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
GR
2018-09-11 16:33:24 UTC
Permalink
Hi Patrick,

thanks, that was already helpful.

You are right that the call to app.exec_() was missing for a proper
display. And with that, indeed, I can execute the export from the gui.

My boilerplate for exporting
was http://www.pyqtgraph.org/documentation/exporting.html and so I thought
I could work without the app.exec_().

So perhaps you know what I have to pass as a proper plotItem instance?

Thanks in advance!
Post by Patrick
Hi,
I'm pretty sure you can do this (update plot and export, without plot
being updated on screen), but I think the plot axes still needs to be
displayed somewhere initially so that the mapping from plot to device
coordinates gets set up. Maybe there's a way to do that without showing the
plot window, but I don't know.
In your example code you never call app.exec_(), so the Qt event loop
never starts, so none of the required initialisation happens. If you run a
normal app with the window displayed, then trigger the export from a
button/timer/event etc, then it should work.
As an aside, I use pyqtgraph to plot inside my applications due to fast
screen updates and interactivity, but produce output plot files with
matplotlib (as pdf or png). So when the user hits the "save" button, I pass
the data off to a separate matplotlib plotting method as it has prettier
and more flexible plotting options. For performance, you can keep the
matplotlib figure around and just modify the plot data with set_data
methods rather than recreating the whole plot again. If that still isn't
fast enough, then maybe store raw data and analyse/plot afterwards.
Post by GR
Hi,
https://stackoverflow.com/questions/52250075/how-does-a-pyqtgraph-export-for-three-subplots-look-like
I am unsure about how much attention it would rise there. Therefore I
would like to draw your attention to that question.
I hope you do not mind.
If you have an idea how my question can be solved: thanks in advance!
Gerhard
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/a6b60d55-326b-49e3-9b24-63461a2b0848%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Patrick
2018-09-12 01:06:42 UTC
Permalink
The "entire scene" export item is the GraphicsLayout object that then
contains the multiple PlotItem objects. Demo code below (Note on Linux you
can overwrite an open file, so can see exported image being updated in
realtime, probably can't do that on Windows).

import numpy as np
from PyQt5 import QtWidgets
import pyqtgraph as pg
import pyqtgraph.exporters
from pyqtgraph.Qt import QtCore, QtGui
from pyqtgraph import GraphicsLayoutWidget

class PyQtGraphExportTest(GraphicsLayoutWidget):

def __init__(self):

super().__init__()

self.setWindowTitle('Test pyqtgraph export')
self.resize(640, 400)

# Set up a couple of stacked plots
self.plot1 = self.addPlot(row=0, col=0)
self.trace1 = self.plot1.plot(np.random.random(10))
self.plot1.enableAutoRange(pg.ViewBox.XYAxes)
self.plot2 = self.addPlot(row=1, col=0)
self.trace2 = self.plot2.plot(np.random.random(10))
self.plot2.enableAutoRange(pg.ViewBox.XYAxes)

# Store reference to exporter so it doesn't have to be initialised
every
# time it's called. Note though the window needs to be displayed so
# mapping from plot to device coordinates is set up, that hasn't
# happened yet as this GraphicsLayoutWidget is also the Qt app
window,
# so we'll create it later on.
self.exporter = None

# Configure a timer to act as trigger event for export. This could
be a
# data acquisition event, button press etc.
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update_data)
self.timer.start(2000)

def update_data(self):
# Create the exporter if needed, now window is displayed on screen
if not self.exporter:
# Here we are passing the exporter the GraphicsLayout object
that is
# the central item (ci) inside this GraphicsLayoutWidget. That
in
# turn contains the two PlotItem objects.
self.exporter = pg.exporters.ImageExporter(self.ci)
self.exporter.parameters()['width'] = 640
# Get some new data, update plot and export
self.trace1.setData(np.random.random(10))
self.trace2.setData(np.random.random(10))
self.exporter.export('exported_image.png')

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = PyQtGraphExportTest()
window.show()
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
sys.exit(app.exec_())
Post by GR
Hi Patrick,
thanks, that was already helpful.
You are right that the call to app.exec_() was missing for a proper
display. And with that, indeed, I can execute the export from the gui.
My boilerplate for exporting was
http://www.pyqtgraph.org/documentation/exporting.html and so I thought I
could work without the app.exec_().
So perhaps you know what I have to pass as a proper plotItem instance?
Thanks in advance!
Post by Patrick
Hi,
I'm pretty sure you can do this (update plot and export, without plot
being updated on screen), but I think the plot axes still needs to be
displayed somewhere initially so that the mapping from plot to device
coordinates gets set up. Maybe there's a way to do that without showing the
plot window, but I don't know.
In your example code you never call app.exec_(), so the Qt event loop
never starts, so none of the required initialisation happens. If you run a
normal app with the window displayed, then trigger the export from a
button/timer/event etc, then it should work.
As an aside, I use pyqtgraph to plot inside my applications due to fast
screen updates and interactivity, but produce output plot files with
matplotlib (as pdf or png). So when the user hits the "save" button, I pass
the data off to a separate matplotlib plotting method as it has prettier
and more flexible plotting options. For performance, you can keep the
matplotlib figure around and just modify the plot data with set_data
methods rather than recreating the whole plot again. If that still isn't
fast enough, then maybe store raw data and analyse/plot afterwards.
Post by GR
Hi,
https://stackoverflow.com/questions/52250075/how-does-a-pyqtgraph-export-for-three-subplots-look-like
I am unsure about how much attention it would rise there. Therefore I
would like to draw your attention to that question.
I hope you do not mind.
If you have an idea how my question can be solved: thanks in advance!
Gerhard
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/c4471025-d5ee-4736-aca8-2507978f59fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
GR
2018-09-12 01:51:20 UTC
Permalink
Hi Patrick,

you made my day!

This is the framework I already can work with from scratch. Perfect!

0.04 seconds on average per export. Super!

I will post your code as an (unaccepted) answer in stackoverflow, as still
others might see it there and could benefit from it.

Thanks again!
Post by Patrick
The "entire scene" export item is the GraphicsLayout object that then
contains the multiple PlotItem objects. Demo code below (Note on Linux you
can overwrite an open file, so can see exported image being updated in
realtime, probably can't do that on Windows).
import numpy as np
from PyQt5 import QtWidgets
import pyqtgraph as pg
import pyqtgraph.exporters
from pyqtgraph.Qt import QtCore, QtGui
from pyqtgraph import GraphicsLayoutWidget
super().__init__()
self.setWindowTitle('Test pyqtgraph export')
self.resize(640, 400)
# Set up a couple of stacked plots
self.plot1 = self.addPlot(row=0, col=0)
self.trace1 = self.plot1.plot(np.random.random(10))
self.plot1.enableAutoRange(pg.ViewBox.XYAxes)
self.plot2 = self.addPlot(row=1, col=0)
self.trace2 = self.plot2.plot(np.random.random(10))
self.plot2.enableAutoRange(pg.ViewBox.XYAxes)
# Store reference to exporter so it doesn't have to be initialised
every
# time it's called. Note though the window needs to be displayed so
# mapping from plot to device coordinates is set up, that hasn't
# happened yet as this GraphicsLayoutWidget is also the Qt app
window,
# so we'll create it later on.
self.exporter = None
# Configure a timer to act as trigger event for export. This could
be a
# data acquisition event, button press etc.
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update_data)
self.timer.start(2000)
# Create the exporter if needed, now window is displayed on screen
# Here we are passing the exporter the GraphicsLayout object
that is
# the central item (ci) inside this GraphicsLayoutWidget. That
in
# turn contains the two PlotItem objects.
self.exporter = pg.exporters.ImageExporter(self.ci)
self.exporter.parameters()['width'] = 640
# Get some new data, update plot and export
self.trace1.setData(np.random.random(10))
self.trace2.setData(np.random.random(10))
self.exporter.export('exported_image.png')
import sys
app = QtWidgets.QApplication(sys.argv)
window = PyQtGraphExportTest()
window.show()
sys.exit(app.exec_())
Post by GR
Hi Patrick,
thanks, that was already helpful.
You are right that the call to app.exec_() was missing for a proper
display. And with that, indeed, I can execute the export from the gui.
My boilerplate for exporting was
http://www.pyqtgraph.org/documentation/exporting.html and so I thought I
could work without the app.exec_().
So perhaps you know what I have to pass as a proper plotItem instance?
Thanks in advance!
Post by Patrick
Hi,
I'm pretty sure you can do this (update plot and export, without plot
being updated on screen), but I think the plot axes still needs to be
displayed somewhere initially so that the mapping from plot to device
coordinates gets set up. Maybe there's a way to do that without showing the
plot window, but I don't know.
In your example code you never call app.exec_(), so the Qt event loop
never starts, so none of the required initialisation happens. If you run a
normal app with the window displayed, then trigger the export from a
button/timer/event etc, then it should work.
As an aside, I use pyqtgraph to plot inside my applications due to fast
screen updates and interactivity, but produce output plot files with
matplotlib (as pdf or png). So when the user hits the "save" button, I pass
the data off to a separate matplotlib plotting method as it has prettier
and more flexible plotting options. For performance, you can keep the
matplotlib figure around and just modify the plot data with set_data
methods rather than recreating the whole plot again. If that still isn't
fast enough, then maybe store raw data and analyse/plot afterwards.
Post by GR
Hi,
https://stackoverflow.com/questions/52250075/how-does-a-pyqtgraph-export-for-three-subplots-look-like
I am unsure about how much attention it would rise there. Therefore I
would like to draw your attention to that question.
I hope you do not mind.
If you have an idea how my question can be solved: thanks in advance!
Gerhard
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/d189e8c2-b66f-4152-a9eb-4b16c1211d00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...