53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
##
|
||
|
## This example demonstrates the creation of a plot with a customized
|
||
|
## AxisItem and ViewBox.
|
||
|
##
|
||
|
|
||
|
|
||
|
import initExample ## Add path to library (just for examples; you do not need this)
|
||
|
|
||
|
import pyqtgraph as pg
|
||
|
from pyqtgraph.Qt import QtCore, QtGui
|
||
|
import numpy as np
|
||
|
import time
|
||
|
|
||
|
class DateAxis(pg.AxisItem):
|
||
|
def tickStrings(self, values, scale, spacing):
|
||
|
return [time.strftime('%b %Y', time.localtime(x)) for x in values]
|
||
|
|
||
|
class CustomViewBox(pg.ViewBox):
|
||
|
def __init__(self, *args, **kwds):
|
||
|
pg.ViewBox.__init__(self, *args, **kwds)
|
||
|
self.setMouseMode(self.RectMode)
|
||
|
|
||
|
## reimplement right-click to zoom out
|
||
|
def mouseClickEvent(self, ev):
|
||
|
if ev.button() == QtCore.Qt.RightButton:
|
||
|
self.autoRange()
|
||
|
|
||
|
def mouseDragEvent(self, ev):
|
||
|
if ev.button() == QtCore.Qt.RightButton:
|
||
|
ev.ignore()
|
||
|
else:
|
||
|
pg.ViewBox.mouseDragEvent(self, ev)
|
||
|
|
||
|
|
||
|
app = pg.mkQApp()
|
||
|
|
||
|
axis = DateAxis(orientation='bottom')
|
||
|
vb = CustomViewBox()
|
||
|
|
||
|
pw = pg.PlotWidget(viewBox=vb, axisItems={'bottom': axis}, enableMenu=False, title="PlotItem with custom axis and ViewBox<br>Menu disabled, mouse behavior changed: left-drag to zoom, right-click to reset zoom")
|
||
|
dates = np.arange(8) * (3600*24*356)
|
||
|
pw.plot(x=dates, y=[1,6,2,4,3,5,6,8], symbol='o')
|
||
|
pw.show()
|
||
|
|
||
|
r = pg.PolyLineROI([(0,0), (10, 10)])
|
||
|
pw.addItem(r)
|
||
|
|
||
|
## Start Qt event loop unless running in interactive mode or using pyside.
|
||
|
import sys
|
||
|
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
||
|
QtGui.QApplication.instance().exec_()
|