Is there a better way to plot a stem plot?

68 views
Skip to first unread message

Edward Kigwana

unread,
Jun 11, 2018, 12:07:28 AM6/11/18
to pyqtgraph
It seems expensive the way I am doing it from this post. It seem to me that if there was a stemPlot option, it'd be a matter of plotting a line from X pt to zero and only requires that boolean. Instead I have to double my data points to plot the stems via connect='pairs' and then plot the points last so that the symbol is on top of the stem. It is basically like plotting the original data 3 times. A dedicated option would certainly improve the efficiency.

from PyQt5 import Qt, QtCore, QtGui
import pyqtgraph as pg
import numpy as np

app = pg.mkQApp()

# Set Global pyqtgraph options
pg.setConfigOption('foreground', 'k')   # Default foreground color for text, lines, axes, etc.
pg.setConfigOption('background', None)  # Default background for GraphicsView.
pg.setConfigOptions(antialias=True)     # Draw lines with smooth edges at the cost of reduced performance.

def stem_plot(pw, x, y):
    curve = pw.plot(np.repeat(x, 2), np.dstack((np.zeros(y.shape[0], dtype=int), y)).flatten(), pen=pg.mkPen('b', width=1.5), connect='pairs', name='Stems')
    curve = pw.plot(x, y, pen=None, symbol='o', symbolPen='b', symbolBrush=Qt.QBrush(Qt.Qt.gray), symbolSize=8, name='Points')

pw = pg.PlotWidget(enableMenu=False, title="Stem Plot Example")
x = np.arange(1,100)
y = np.arange(1,100)

stem_plot(pw, x, y)

pw.show()

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


Reply all
Reply to author
Forward
0 new messages