Hello everyone,
I'm trying to export a simple graph to svg format, but I'm facing some
issues.
My graph should contain a series of points and three infinitelines.
The problem is that when I export the plot to svg the infinitelines are
exported as lines long just 1 unit and the axis labels overlap with the
axes itself, as you can see in the picture attached to this message.
The version of pyqtgraph I'm currently using is 0.12.3
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/62c099d0-1d08-4fe3-9e0c-42f6e8e8fc1cn%40googlegroups.com.
import os.path
import sys
import pyqtgraph as pg
from PySide2 import QtCore
from pyqtgraph.exporters import SVGExporter, ImageExporter
import numpy as np
from PySide2.QtCore import Qt, QRectF, QObject
from PySide2.QtWidgets import QMainWindow, QApplication
from app.widgets.StandardGraphicsView import StandardGraphicsView
from app.statistics.PlotData import PlotData
def exportGraph(imgPath: str) -> None:
view = StandardGraphicsView()
pen = pg.mkPen(color='w', width=2)
plot = pg.PlotItem()
view.setCentralItem(plot)
#Declaration of coordinates lists
Xs = [1, 2, 3, 4, 5]
Ys = [6, 7, 8, 9, 10]
#Axis labels setup
axis = plot.getAxis("bottom")
axis.setLabel("X")
axis.showLabel(True)
axis = plot.getAxis("left")
axis.setLabel("Y")
axis.showLabel(True)
plot.getAxis("left").setWidth(200)
plot.getAxis("bottom").setWidth(200)
plot.enableAutoRange(x=True, y=True)
plot.setLimits(xMin=min(Xs) - 1, xMax=len(Xs) + 1)
plot.setRange(xRange=(len(Xs) + 1, min(Xs) - 1))
#Points plotting
plot.plot(x=Xs, y=Ys, symbol='s', pen=pen, antialias=True)
#InfiniteLines plotting
pen = pg.mkPen(color="r", width=2)
plot.addLine(y=10, pen=pen)
pen = pg.mkPen(color="r", width=2)
plot.addLine(y=20, pen=pen)
pen = pg.mkPen(color='g', width=2)
plot.addLine(y=15, pen=pen)
exportImage(plot, imgPath)
#This function chooses automatically the proper exporter accordingly to the file extension
def exportImage(view, filePath: str):
splittedName= filePath.split(".")
if splittedName[len(splittedName) - 1] == "svg":
exporter = SVGExporter(view)
else:
exporter = ImageExporter(view)
exporter.export(filePath)
if __name__ == "__main__":
if (sys.flags.interactive == 1) and hasattr(QtCore, 'PYQT_VERSION'):
exit()
app = QApplication([])
exportGraph(os.path.abspath("test.svg"))<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny" viewBox ="0 0 640.000000 480.000000">
<title>pyqtgraph SVG export</title>
<desc>Generated with Qt and pyqtgraph</desc>
<style>
image {
image-rendering: crisp-edges;
image-rendering: -moz-crisp-edges;
image-rendering: pixelated;
}
</style>
<rect width="100%" height="100%" style="fill:rgba(0.000000, 0.000000, 0.000000, 1)" />
<defs>
</defs>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="PlotItem_1">
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="ViewBox_1">
<g fill="none" stroke="none" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M36.0,1.0 L639.0,1.0 L639.0,459.0 L36.0,459.0 L36.0,1.0 "/>
</g>
<clipPath id="ViewBox_1_clip">
<path vector-effect="none" fill-rule="evenodd" d="M36.0,1.0 L639.0,1.0 L639.0,459.0 L36.0,459.0 L36.0,1.0 " stroke-width="1.0"/>
</clipPath>
<g clip-path="url(#ViewBox_1_clip)">
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="ChildGroup_1">
<g id="PlotDataItem_1">
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="PlotCurveItem_1">
<g fill="none" stroke="#ffffff" stroke-opacity="1" stroke-width="2" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M136.5,438.1388 L237.0,408.3126 L337.5,378.4864 L438.0,348.66020000000003 L538.5,318.834 "/>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="ScatterPlotItem_1">
<g fill="#323296" fill-opacity="1" stroke="#c8c8c8" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M131.5,433.139 L141.5,433.139 L141.5,443.139 L131.5,443.139 L131.5,433.139 "/>
</g>
<g fill="#323296" fill-opacity="1" stroke="#c8c8c8" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M232.0,403.312 L242.0,403.312 L242.0,413.312 L232.0,413.312 L232.0,403.312 "/>
</g>
<g fill="#323296" fill-opacity="1" stroke="#c8c8c8" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M332.5,373.486 L342.5,373.486 L342.5,383.486 L332.5,383.486 L332.5,373.486 "/>
</g>
<g fill="#323296" fill-opacity="1" stroke="#c8c8c8" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M433.0,343.66 L443.0,343.66 L443.0,353.66 L433.0,353.66 L433.0,343.66 "/>
</g>
<g fill="#323296" fill-opacity="1" stroke="#c8c8c8" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M533.5,313.834 L543.5,313.834 L543.5,323.834 L533.5,323.834 L533.5,313.834 "/>
</g>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="InfiniteLine_1">
<g fill="none" stroke="#ff0000" stroke-opacity="1" stroke-width="2" stroke-linecap="square" stroke-linejoin="miter" stroke-miterlimit="2" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,318.834 136.5,318.834"/>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="InfiniteLine_2">
<g fill="none" stroke="#ff0000" stroke-opacity="1" stroke-width="2" stroke-linecap="square" stroke-linejoin="miter" stroke-miterlimit="2" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,20.5719 136.5,20.5719"/>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="InfiniteLine_3">
<g fill="none" stroke="#00ff00" stroke-opacity="1" stroke-width="2" stroke-linecap="square" stroke-linejoin="miter" stroke-miterlimit="2" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,169.703 136.5,169.703"/>
</g>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="QGraphicsRectItem_1">
<g fill="none" stroke="none" transform="matrix(1,0,0,1,36,1)" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<rect vector-effect="non-scaling-stroke" x="0" y="0" width="603" height="458"/>
</g>
</g>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="AxisItem_1">
<g fill="none" stroke="#969696" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,459.0 639.0,459.0"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,459.0 36.0,454.0"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="237.0,459.0 237.0,454.0"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="438.0,459.0 438.0,454.0"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="639.0,459.0 639.0,454.0"/>
</g>
<g fill="none" stroke="#969696" stroke-opacity="0.498039" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="136.5,459.0 136.5,455.66667"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="337.5,459.0 337.5,455.66667"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="538.5,459.0 538.5,455.66667"/>
</g>
<g fill="none" stroke="#969696" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,36,459)" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="-3" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">0</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="198" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">2</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="399" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">4</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="600" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">6</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="97.5" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">1</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="298.5" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">3</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="499.5" y="13" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">5</text>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="QGraphicsTextItem_1">
<g fill="none" stroke="#969696" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,333,463)" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="4" y="15" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">X </text>
</g>
</g>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="AxisItem_2">
<g fill="none" stroke="#969696" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,1.0 36.0,459.0"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,20.5719 41.0,20.5719"/>
</g>
<g fill="none" stroke="#969696" stroke-opacity="0.498039" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,318.834 39.3333,318.834"/>
</g>
<g fill="none" stroke="#969696" stroke-opacity="0.333333" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,438.139 38.5,438.139"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,378.486 38.5,378.486"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,259.181 38.5,259.181"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,199.529 38.5,199.529"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,139.877 38.5,139.877"/>
<polyline fill="none" vector-effect="non-scaling-stroke" points="36.0,80.2243 38.5,80.2243"/>
</g>
<g fill="none" stroke="#969696" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,1,1)" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="18" y="24.0625" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">20</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="18" y="322.328" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">10</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="24" y="441.625" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">6</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="24" y="381.984" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">8</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="18" y="262.672" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">12</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="18" y="203.016" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">14</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="18" y="143.375" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">16</text>
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="18" y="83.7188" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">18</text>
</g>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" id="QGraphicsTextItem_2">
<g fill="none" stroke="#969696" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(0,-1,1,0,-4,234)" font-family="MS Shell Dlg 2" font-size="11" font-weight="400" font-style="normal">
<text fill="#969696" fill-opacity="1" stroke="none" xml:space="preserve" x="4" y="15" font-family=""MS Shell Dlg 2"" font-size="11" font-weight="400" font-style="normal">Y </text>
</g>
</g>
</g>
</g>
</svg>