1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the Qwt License
# Copyright (c) 2002 Uwe Rathmann, for the original C++ code
# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
# (see LICENSE file for more details)
"""
Plotting series item
--------------------
QwtPlotSeriesItem
~~~~~~~~~~~~~~~~~
.. autoclass:: QwtPlotSeriesItem
:members:
QwtSeriesData
~~~~~~~~~~~~~
.. autoclass:: QwtSeriesData
:members:
QwtPointArrayData
~~~~~~~~~~~~~~~~~
.. autoclass:: QwtPointArrayData
:members:
QwtSeriesStore
~~~~~~~~~~~~~~
.. autoclass:: QwtSeriesStore
:members:
"""
import numpy as np
from qtpy.QtCore import QPointF, QRectF, Qt
from qwt.plot import QwtPlotItem, QwtPlotItem_PrivateData
from qwt.text import QwtText
class QwtPlotSeriesItem_PrivateData(QwtPlotItem_PrivateData):
def __init__(self):
QwtPlotItem_PrivateData.__init__(self)
self.orientation = Qt.Horizontal
class QwtPlotSeriesItem(QwtPlotItem):
"""
Base class for plot items representing a series of samples
"""
def __init__(self, title):
if not isinstance(title, QwtText):
title = QwtText(title)
QwtPlotItem.__init__(self, title)
self.__data = QwtPlotSeriesItem_PrivateData()
self.setItemInterest(QwtPlotItem.ScaleInterest, True)
def setOrientation(self, orientation):
"""
Set the orientation of the item. Default is `Qt.Horizontal`.
The `orientation()` might be used in specific way by a plot item.
F.e. a QwtPlotCurve uses it to identify how to display the curve
int `QwtPlotCurve.Steps` or `QwtPlotCurve.Sticks` style.
.. seealso::
:py:meth`orientation()`
"""
if self.__data.orientation != orientation:
self.__data.orientation = orientation
self.legendChanged()
self.itemChanged()
def orientation(self):
"""
:return: Orientation of the plot item
.. seealso::
:py:meth`setOrientation()`
"""
return self.__data.orientation
def draw(self, painter, xMap, yMap, canvasRect):
"""
Draw the complete series
:param QPainter painter: Painter
:param qwt.scale_map.QwtScaleMap xMap: Maps x-values into pixel coordinates.
:param qwt.scale_map.QwtScaleMap yMap: Maps y-values into pixel coordinates.
:param QRectF canvasRect: Contents rectangle of the canvas
"""
self.drawSeries(painter, xMap, yMap, canvasRect, 0, -1)
def drawSeries(self, painter, xMap, yMap, canvasRect, from_, to):
"""
Draw a subset of the samples
:param QPainter painter: Painter
:param qwt.scale_map.QwtScaleMap xMap: Maps x-values into pixel coordinates.
:param qwt.scale_map.QwtScaleMap yMap: Maps y-values into pixel coordinates.
:param QRectF canvasRect: Contents rectangle of the canvas
:param int from_: Index of the first point to be painted
:param int to: Index of the last point to be painted. If to < 0 the curve will be painted to its last point.
.. seealso::
This method is implemented in `qwt.plot_curve.QwtPlotCurve`
"""
raise NotImplementedError
def boundingRect(self):
return self.dataRect() # dataRect method is implemented in QwtSeriesStore
def updateScaleDiv(self, xScaleDiv, yScaleDiv):
rect = QRectF(
xScaleDiv.lowerBound(),
yScaleDiv.lowerBound(),
xScaleDiv.range(),
yScaleDiv.range(),
)
self.setRectOfInterest(
rect
) # setRectOfInterest method is implemented in QwtSeriesData
def dataChanged(self):
self.itemChanged()
class QwtSeriesData(object):
"""
Abstract interface for iterating over samples
`PythonQwt` offers several implementations of the QwtSeriesData API,
but in situations, where data of an application specific format
needs to be displayed, without having to copy it, it is recommended
to implement an individual data access.
A subclass of `QwtSeriesData` must implement:
- size():
Should return number of data points.
- sample()
Should return values x and y values of the sample at specific position
as QPointF object.
- boundingRect()
Should return the bounding rectangle of the data series.
It is used for autoscaling and might help certain algorithms for
displaying the data.
The member `_boundingRect` is intended for caching the calculated
rectangle.
"""
def __init__(self):
self._boundingRect = QRectF(0.0, 0.0, -1.0, -1.0)
def setRectOfInterest(self, rect):
"""
Set a the "rect of interest"
QwtPlotSeriesItem defines the current area of the plot canvas
as "rectangle of interest" ( QwtPlotSeriesItem::updateScaleDiv() ).
It can be used to implement different levels of details.
The default implementation does nothing.
:param QRectF rect: Rectangle of interest
"""
pass
def size(self):
"""
:return: Number of samples
"""
pass
def sample(self, i):
"""
Return a sample
:param int i: Index
:return: Sample at position i
"""
pass
def boundingRect(self):
"""
Calculate the bounding rect of all samples
The bounding rect is necessary for autoscaling and can be used
for a couple of painting optimizations.
:return: Bounding rectangle
"""
pass
class QwtPointArrayData(QwtSeriesData):
"""
Interface for iterating over two array objects
.. py:class:: QwtCQwtPointArrayDataolorMap(x, y, [size=None])
:param x: Array of x values
:type x: list or tuple or numpy.array
:param y: Array of y values
:type y: list or tuple or numpy.array
:param int size: Size of the x and y arrays
:param bool finite: if True, keep only finite array elements (remove all infinity and not a number values), otherwise do not filter array elements
"""
def __init__(self, x=None, y=None, size=None, finite=None):
QwtSeriesData.__init__(self)
if x is None and y is not None:
x = np.arange(len(y))
elif y is None and x is not None:
y = x
x = np.arange(len(y))
elif x is None and y is None:
x = np.array([])
y = np.array([])
if isinstance(x, (tuple, list)):
x = np.array(x)
if isinstance(y, (tuple, list)):
y = np.array(y)
if size is not None:
x = np.resize(x, (size,))
y = np.resize(y, (size,))
if len(x) != len(y):
minlen = min(len(x), len(y))
x = np.resize(x, (minlen,))
y = np.resize(y, (minlen,))
if finite if finite is not None else True:
indexes = np.logical_and(np.isfinite(x), np.isfinite(y))
self.__x = x[indexes]
self.__y = y[indexes]
else:
self.__x = x
self.__y = y
def boundingRect(self):
"""
Calculate the bounding rectangle
The bounding rectangle is calculated once by iterating over all
points and is stored for all following requests.
:return: Bounding rectangle
"""
xmin = self.__x.min()
xmax = self.__x.max()
ymin = self.__y.min()
ymax = self.__y.max()
return QRectF(xmin, ymin, xmax - xmin, ymax - ymin)
def size(self):
"""
:return: Size of the data set
"""
return min([self.__x.size, self.__y.size])
def sample(self, index):
"""
:param int index: Index
:return: Sample at position `index`
"""
return QPointF(self.__x[index], self.__y[index])
def xData(self):
"""
:return: Array of the x-values
"""
return self.__x
def yData(self):
"""
:return: Array of the y-values
"""
return self.__y
class QwtSeriesStore(object):
"""
Class storing a `QwtSeriesData` object
`QwtSeriesStore` and `QwtPlotSeriesItem` are intended as base classes for
all plot items iterating over a series of samples.
"""
def __init__(self):
self.__series = None
def setData(self, series):
"""
Assign a series of samples
:param qwt.plot_series.QwtSeriesData series: Data
.. warning::
The item takes ownership of the data object, deleting it
when its not used anymore.
"""
if self.__series != series:
self.__series = series
self.dataChanged()
def dataChanged(self):
raise NotImplementedError
def data(self):
"""
:return: the series data
"""
return self.__series
def sample(self, index):
"""
:param int index: Index
:return: Sample at position index
"""
if self.__series:
return self.__series.sample(index)
else:
return
def dataSize(self):
"""
:return: Number of samples of the series
.. seealso::
:py:meth:`setData()`,
:py:meth:`qwt.plot_series.QwtSeriesData.size()`
"""
if self.__series is None:
return 0
return self.__series.size()
def dataRect(self):
"""
:return: Bounding rectangle of the series or an invalid rectangle, when no series is stored
.. seealso::
:py:meth:`qwt.plot_series.QwtSeriesData.boundingRect()`
"""
if self.__series is None or self.dataSize() == 0:
return QRectF(1.0, 1.0, -2.0, -2.0)
return self.__series.boundingRect()
def setRectOfInterest(self, rect):
"""
Set a the "rect of interest" for the series
:param QRectF rect: Rectangle of interest
.. seealso::
:py:meth:`qwt.plot_series.QwtSeriesData.setRectOfInterest()`
"""
if self.__series:
self.__series.setRectOfInterest(rect)
def swapData(self, series):
"""
Replace a series without deleting the previous one
:param qwt.plot_series.QwtSeriesData series: New series
:return: Previously assigned series
"""
swappedSeries = self.__series
self.__series = series
return swappedSeries
|