代码之家  ›  专栏  ›  技术社区  ›  Kevin

pyqtgraph导出SVG凌乱/移位

  •  0
  • Kevin  · 技术社区  · 8 年前

    代码:

    import pyqtgraph as pg
    from pyqtgraph.Qt import QtCore, QtGui
    import numpy as np
    import pandas as pd
    import pyqtgraph.exporters
    
    
    pg.setConfigOption('background', 'w')
    pg.setConfigOption('foreground', 'k')
    
    df = pd.DataFrame(np.random.randint(
        0, 10, size=(1000, 11)), columns=list('ABCDEFGHIJK'))
    
    win = pg.GraphicsWindow(title="Basic plotting examples")
    # win.resize(900, 600)
    win.setWindowTitle('pyqtgraph example: Plotting')
    
    # Enable antialiasing for prettier plots
    pg.setConfigOptions(antialias=True)
    
    ndays = 5
    ngenos = df.shape[1]
    nrows = 3
    ncols = 4
    p = ['p%s' % s for s in range(ngenos)]
    bg = ['bg%s' % s for s in range(ngenos * ndays)]
    font = QtGui.QFont()
    font.setPixelSize(5)
    
    for i in range(nrows):
        for j in range(ncols):
            if (i * ncols + j) < ngenos:
                p[i * ncols + j] = win.addPlot(title=df.dtypes.index[i * ncols + j])
                for k in range(ndays):
                    bg[(i * ncols + j) + k] = pg.BarGraphItem(x=range(200), y0=(10 * k), height=df.iloc[: 200*(k+1), (i * ncols + j)], width=0.3)
                    p[i * ncols + j].addItem(bg[(i * ncols + j) + k])
                # p[i * ncols + j].showGrid(x=None, y=True)
                p[i * ncols + j].setRange(xRange=(0, 200), yRange=(0, 60))
                p[i * ncols + j].getAxis('bottom').tickFont = font
                p[i * ncols + j].getAxis('left').tickFont = font
    
        win.nextRow()
    
    
    QtGui.QApplication.processEvents()
    ex = pg.exporters.SVGExporter(win.scene())
    ex.export('test2.svg')
    ex = pg.exporters.ImageExporter(win.scene())
    ex.export('test2.png')
    
    pg.cleanup
    
    if __name__ == '__main__':
        import sys
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()
    

    png文件看起来不错,但分辨率很低。在SVG文件中,似乎所有轴都发生了移动。

    enter image description here

    SVG文件: enter image description here

    如何正确导出SVG?谢谢

    1 回复  |  直到 5 年前
        1
  •  1
  •   titusjan    8 年前

    我检查了一下,得到了与您相同的输出。

    see issue #434

    SVG导出中似乎还有一些其他长期存在的未决问题(问题 105 176 ),所以我不会指望快速修复。

    SVGExporter.py