Skip to content

Commit

Permalink
fix: export subplots with axes labels (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jan 16, 2020
1 parent 5e2978f commit 37d58c5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2.0.0b12
- feat: add button to copy Quick View statistics to clipboard
- fix: export subplots with axes labels (#7)
- enh: be more verbose when user wants to add box filters (#34)
2.0.0b11
- fix: imported polygon filters did not immediately show up in
Expand Down
39 changes: 39 additions & 0 deletions shapeout2/gui/pipeline_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import numpy as np
from PyQt5 import uic, QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph import exporters
from pyqtgraph.graphicsItems.GradientEditorItem import Gradients


from .. import plot_cache
from .. import util

Expand Down Expand Up @@ -211,6 +213,7 @@ def update_content(self):
parent.setMaximumSize(size_hint)



class PipelinePlotItem(SimplePlotItem):
def __init__(self, *args, **kwargs):
super(PipelinePlotItem, self).__init__(*args, **kwargs)
Expand All @@ -221,6 +224,38 @@ def __init__(self, *args, **kwargs):
# Keep track of all elements (for redraw)
self._plot_elements = []

def perform_export(self, file):
"""Performs export in new layout with axes labels set

Overrides the basic functionality of SimplePlotItem.
See https://github.com/ZELLMECHANIK-DRESDEN/ShapeOut2/issues/7
"""
# Create a plot window
win = pg.GraphicsLayoutWidget(
size=(self.width() + 100, self.height() + 100),
show=True)
# fill layout
labelx, labely = get_axes_labels(self.plot_state, self.slot_states)
win.addLabel(labely, angle=-90)
explot = PipelinePlotItem()
explot.redraw(self.dslist, self.slot_states, self.plot_state)
win.addItem(explot)
win.nextRow()
win.addLabel(labelx, col=1)
# Update the UI (do it twice, otherwise the tick labels overlap)
QtWidgets.QApplication.processEvents()
QtWidgets.QApplication.processEvents()
win.hide()
# perform actual export
suffix = file[-3:]
if suffix == "png":
exp = exporters.ImageExporter(win.scene())
# translate from screen resolution (80dpi) to 300dpi
exp.params["width"] = int(exp.params["width"] / 72 * 300)
elif suffix == "svg":
exp = exporters.SVGExporter(win.scene())
exp.export(file)

def redraw(self, dslist, slot_states, plot_state):
# Remove everything
for el in self._plot_elements:
Expand All @@ -229,6 +264,10 @@ def redraw(self, dslist, slot_states, plot_state):
if not dslist:
return

self.dslist = dslist
self.slot_states = slot_states
self.plot_state = plot_state

# General
gen = plot_state["general"]
# TODO:
Expand Down
7 changes: 6 additions & 1 deletion shapeout2/gui/widgets/simple_plot_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,23 @@ def axes_to_front(self):
self.axes[kax]["item"].setZValue(200)

def on_export(self, suffix):
"""Export subplots as original figures (with axes labels, etc)"""
file, _ = QtWidgets.QFileDialog.getSaveFileName(
None,
'Save {} file'.format(suffix.upper()),
'',
'{} file (*.{})'.format(suffix.upper(), suffix))
if not file.endswith("." + suffix):
file += "." + suffix
self.perform_export(file)

def perform_export(self, file):
suffix = file[-3:]
if suffix == "png":
exp = exporters.ImageExporter(self)
# translate from screen resolution (80dpi) to 300dpi
exp.params["width"] = int(exp.params["width"] / 72 * 300)
if suffix == "svg":
elif suffix == "svg":
exp = exporters.SVGExporter(self)
exp.export(file)

Expand Down

0 comments on commit 37d58c5

Please sign in to comment.