pyqtgraph/examples/customPlot.py

51 lines
1.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2013-02-25 04:09:03 +00:00
"""
Date axis item (#1154) * Add DateAxisItem * Change style to camelCase * Fix missing first tick for negative timestamps * Add ms precision, auto skipping Auto skipping allows a zoom level to skip ticks automatically if the maximum number of ticks/pt is exceeded * fixes suggested by @goetzc * workaround for negative argument to utcfromtimestamp on windows * attachToPlotItem method * default date axis orientation * Use new DateAxisItem in Plot Customization example * attachToPlotItem bugfix * examples of DateAxisItem * modified description of customPlot example * added descriptions to the new examples, reformatted their code, included the first one into utils.py * typo * Refactored code for setting axis items into new function Replaces "DateAxisItem.attachToPlotItem" * Fix string comparison with == * Doc: Slightly more text for DateAxisItem, small improvement for PlotItem * Make PlotWidget.setAxisItems official * Fix typo in docstring * renamed an example * merge bug fix * Revert "merge bug fix" This reverts commit 876b5a7cdb50cd824b4a5218427081b3ce5c2fe4. * Real bug fix * support for dates upto -1e13..1e13 * Automatically limit DateAxisItem to a range from -1e12 to 1e12 years Very large years (|y|>1e13) cause infinite loop, and since nobody needs time 100 times larger than the age of the universe anyways, this constrains it to 1e12. Following suggestion by @axil: https://github.com/pyqtgraph/pyqtgraph/pull/1154#issuecomment-612662168 * Also catch ValueErrors occuring on Linux before OverfloeErrors While zooming out, before hitting OverflowErrors, utctimestamp produces ValueErrors (at least on my Linux machine), so they are also catched. * Fix: Timestamp 0 corresponds to year 1970 For large years, x axis labels jump by 1970 years if it is not accounted for timestamp 0 to be equal to year 1970. * Fix: When zooming into extreme dates, OSError occurs This commit catches the OSError like the other observed errors * Disable stepping below years for dates outside *_REGULAR_TIMESTAMP 2 reasons: First: At least on my Linux machine, zooming into those dates creates infinite loops. Second: Nobody needs sub-year-precision for those extreme years anyways. * Adapt zoom level sizes based on current font size and screen resolution This is somewhat experimental. With this commit, no longer 60 px are assumed as width for all zoom levels, but the current font and display resolution are considered to calculate the width of ticks in each zoom level. See the new function `updateZoomLevels` for details. Before calling this function, overridden functions `paint` and `generateDrawSpecs` provide information over the current display and font via `self.fontScaleFactor` and `self.fontMetrics`. * Meaningful error meassage when adding axis to multiple PlotItems As @axil noted in the DateAxisItem PR, currently users get a segmentation fault when one tries to add an axis to multiple PlotItems. This commit adds a meaningful RuntimeError message for that case. * setZoomLevelForDensity: Refactoring and calculating optimal spacing on the fly * DateTimeAxis Fix: 1970 shows when zooming far out * Refactoring: Make zoomLevels a customizable dict again * updated the dateaxisitem example * Fix: Get screen resolution in a way that also works for Qt 4 This is both a simplification in code and an improvement in backwards compatibility with Qt 4. * DateAxisItem Fix: Also resolve time below 0.5 seconds * unix line endings in examples * DateTimeAxis Fix: For years < 1 and > 9999, stepping broke Stepping was off by 1970 years for years < 1 and > 9999, resulting in a gap in ticks visible when zooming out. Fixed by subtracting the usual 1970 years. * DateTimeAxis Fix: Zooming out too far causes infinite loop Fixed by setting default limits to +/- 1e10 years. Should still be enough. * improved second dateaxisitem example * 1..9999 years limit * DateTimeAxis: Use OrderedDict to stay compatible with Python < 3-6 * DateAxisItem: Use font height to determine spacing for vertical axes * window title * added dateaxisitem.rst * updated index.rst Co-authored-by: Lukas Heiniger <lukas.heiniger@sed.ethz.ch> Co-authored-by: Lev Maximov <lev.maximov@gmail.com> Co-authored-by: 2xB <2xB@users.noreply.github.com>
2020-04-27 18:43:22 +00:00
This example demonstrates the creation of a plot with
DateAxisItem and a customized ViewBox.
2013-02-25 04:09:03 +00:00
"""
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 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()
Date axis item (#1154) * Add DateAxisItem * Change style to camelCase * Fix missing first tick for negative timestamps * Add ms precision, auto skipping Auto skipping allows a zoom level to skip ticks automatically if the maximum number of ticks/pt is exceeded * fixes suggested by @goetzc * workaround for negative argument to utcfromtimestamp on windows * attachToPlotItem method * default date axis orientation * Use new DateAxisItem in Plot Customization example * attachToPlotItem bugfix * examples of DateAxisItem * modified description of customPlot example * added descriptions to the new examples, reformatted their code, included the first one into utils.py * typo * Refactored code for setting axis items into new function Replaces "DateAxisItem.attachToPlotItem" * Fix string comparison with == * Doc: Slightly more text for DateAxisItem, small improvement for PlotItem * Make PlotWidget.setAxisItems official * Fix typo in docstring * renamed an example * merge bug fix * Revert "merge bug fix" This reverts commit 876b5a7cdb50cd824b4a5218427081b3ce5c2fe4. * Real bug fix * support for dates upto -1e13..1e13 * Automatically limit DateAxisItem to a range from -1e12 to 1e12 years Very large years (|y|>1e13) cause infinite loop, and since nobody needs time 100 times larger than the age of the universe anyways, this constrains it to 1e12. Following suggestion by @axil: https://github.com/pyqtgraph/pyqtgraph/pull/1154#issuecomment-612662168 * Also catch ValueErrors occuring on Linux before OverfloeErrors While zooming out, before hitting OverflowErrors, utctimestamp produces ValueErrors (at least on my Linux machine), so they are also catched. * Fix: Timestamp 0 corresponds to year 1970 For large years, x axis labels jump by 1970 years if it is not accounted for timestamp 0 to be equal to year 1970. * Fix: When zooming into extreme dates, OSError occurs This commit catches the OSError like the other observed errors * Disable stepping below years for dates outside *_REGULAR_TIMESTAMP 2 reasons: First: At least on my Linux machine, zooming into those dates creates infinite loops. Second: Nobody needs sub-year-precision for those extreme years anyways. * Adapt zoom level sizes based on current font size and screen resolution This is somewhat experimental. With this commit, no longer 60 px are assumed as width for all zoom levels, but the current font and display resolution are considered to calculate the width of ticks in each zoom level. See the new function `updateZoomLevels` for details. Before calling this function, overridden functions `paint` and `generateDrawSpecs` provide information over the current display and font via `self.fontScaleFactor` and `self.fontMetrics`. * Meaningful error meassage when adding axis to multiple PlotItems As @axil noted in the DateAxisItem PR, currently users get a segmentation fault when one tries to add an axis to multiple PlotItems. This commit adds a meaningful RuntimeError message for that case. * setZoomLevelForDensity: Refactoring and calculating optimal spacing on the fly * DateTimeAxis Fix: 1970 shows when zooming far out * Refactoring: Make zoomLevels a customizable dict again * updated the dateaxisitem example * Fix: Get screen resolution in a way that also works for Qt 4 This is both a simplification in code and an improvement in backwards compatibility with Qt 4. * DateAxisItem Fix: Also resolve time below 0.5 seconds * unix line endings in examples * DateTimeAxis Fix: For years < 1 and > 9999, stepping broke Stepping was off by 1970 years for years < 1 and > 9999, resulting in a gap in ticks visible when zooming out. Fixed by subtracting the usual 1970 years. * DateTimeAxis Fix: Zooming out too far causes infinite loop Fixed by setting default limits to +/- 1e10 years. Should still be enough. * improved second dateaxisitem example * 1..9999 years limit * DateTimeAxis: Use OrderedDict to stay compatible with Python < 3-6 * DateAxisItem: Use font height to determine spacing for vertical axes * window title * added dateaxisitem.rst * updated index.rst Co-authored-by: Lukas Heiniger <lukas.heiniger@sed.ethz.ch> Co-authored-by: Lev Maximov <lev.maximov@gmail.com> Co-authored-by: 2xB <2xB@users.noreply.github.com>
2020-04-27 18:43:22 +00:00
axis = pg.DateAxisItem(orientation='bottom')
vb = CustomViewBox()
Date axis item (#1154) * Add DateAxisItem * Change style to camelCase * Fix missing first tick for negative timestamps * Add ms precision, auto skipping Auto skipping allows a zoom level to skip ticks automatically if the maximum number of ticks/pt is exceeded * fixes suggested by @goetzc * workaround for negative argument to utcfromtimestamp on windows * attachToPlotItem method * default date axis orientation * Use new DateAxisItem in Plot Customization example * attachToPlotItem bugfix * examples of DateAxisItem * modified description of customPlot example * added descriptions to the new examples, reformatted their code, included the first one into utils.py * typo * Refactored code for setting axis items into new function Replaces "DateAxisItem.attachToPlotItem" * Fix string comparison with == * Doc: Slightly more text for DateAxisItem, small improvement for PlotItem * Make PlotWidget.setAxisItems official * Fix typo in docstring * renamed an example * merge bug fix * Revert "merge bug fix" This reverts commit 876b5a7cdb50cd824b4a5218427081b3ce5c2fe4. * Real bug fix * support for dates upto -1e13..1e13 * Automatically limit DateAxisItem to a range from -1e12 to 1e12 years Very large years (|y|>1e13) cause infinite loop, and since nobody needs time 100 times larger than the age of the universe anyways, this constrains it to 1e12. Following suggestion by @axil: https://github.com/pyqtgraph/pyqtgraph/pull/1154#issuecomment-612662168 * Also catch ValueErrors occuring on Linux before OverfloeErrors While zooming out, before hitting OverflowErrors, utctimestamp produces ValueErrors (at least on my Linux machine), so they are also catched. * Fix: Timestamp 0 corresponds to year 1970 For large years, x axis labels jump by 1970 years if it is not accounted for timestamp 0 to be equal to year 1970. * Fix: When zooming into extreme dates, OSError occurs This commit catches the OSError like the other observed errors * Disable stepping below years for dates outside *_REGULAR_TIMESTAMP 2 reasons: First: At least on my Linux machine, zooming into those dates creates infinite loops. Second: Nobody needs sub-year-precision for those extreme years anyways. * Adapt zoom level sizes based on current font size and screen resolution This is somewhat experimental. With this commit, no longer 60 px are assumed as width for all zoom levels, but the current font and display resolution are considered to calculate the width of ticks in each zoom level. See the new function `updateZoomLevels` for details. Before calling this function, overridden functions `paint` and `generateDrawSpecs` provide information over the current display and font via `self.fontScaleFactor` and `self.fontMetrics`. * Meaningful error meassage when adding axis to multiple PlotItems As @axil noted in the DateAxisItem PR, currently users get a segmentation fault when one tries to add an axis to multiple PlotItems. This commit adds a meaningful RuntimeError message for that case. * setZoomLevelForDensity: Refactoring and calculating optimal spacing on the fly * DateTimeAxis Fix: 1970 shows when zooming far out * Refactoring: Make zoomLevels a customizable dict again * updated the dateaxisitem example * Fix: Get screen resolution in a way that also works for Qt 4 This is both a simplification in code and an improvement in backwards compatibility with Qt 4. * DateAxisItem Fix: Also resolve time below 0.5 seconds * unix line endings in examples * DateTimeAxis Fix: For years < 1 and > 9999, stepping broke Stepping was off by 1970 years for years < 1 and > 9999, resulting in a gap in ticks visible when zooming out. Fixed by subtracting the usual 1970 years. * DateTimeAxis Fix: Zooming out too far causes infinite loop Fixed by setting default limits to +/- 1e10 years. Should still be enough. * improved second dateaxisitem example * 1..9999 years limit * DateTimeAxis: Use OrderedDict to stay compatible with Python < 3-6 * DateAxisItem: Use font height to determine spacing for vertical axes * window title * added dateaxisitem.rst * updated index.rst Co-authored-by: Lukas Heiniger <lukas.heiniger@sed.ethz.ch> Co-authored-by: Lev Maximov <lev.maximov@gmail.com> Co-authored-by: 2xB <2xB@users.noreply.github.com>
2020-04-27 18:43:22 +00:00
pw = pg.PlotWidget(viewBox=vb, axisItems={'bottom': axis}, enableMenu=False, title="PlotItem with DateAxisItem and custom 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()
2013-02-25 04:09:03 +00:00
pw.setWindowTitle('pyqtgraph example: customPlot')
r = pg.PolyLineROI([(0,0), (10, 10)])
pw.addItem(r)
## 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_()