1
# -*- coding: utf-8 -*-
3
# Copyright © 2009-2010 CEA
4
# Pierre Raybaut <pierre.raybaut@cea.fr>
5
# Licensed under the terms of the CECILL License
6
# (see guiqwt/__init__.py for details)
12
The `builder` module provides a builder singleton class
13
used to simplify the creation of plot items.
18
Before creating any widget, a `QApplication` must be instantiated
19
(that is a `Qt` internal requirement):
22
>>> app = guidata.qapplication()
24
that is mostly equivalent to the following (the only difference is that
25
the `guidata` helper function also installs the `Qt` translation
26
corresponding to the system locale):
28
>>> from PyQt4.QtGui import QApplication
29
>>> app = QApplication([])
31
now that a `QApplication` object exists, we may create the plotting widget:
33
>>> from guiqwt.plot import ImageWidget
34
>>> widget = ImageWidget()
36
create curves, images, histograms, etc. and attach them to the plot:
38
>>> from guiqwt.builder import make
39
>>> curve = make.mcure(x, y, 'r+')
40
>>> image = make.image(data)
41
>>> hist = make.histogram(data, 100)
42
>>> for item in (curve, image, hist):
43
... widget.plot.add_item()
45
and then show the widget to screen:
53
.. autoclass:: PlotItemBuilder
57
from numpy import arange, array, zeros, meshgrid, ndarray
59
from PyQt4.Qwt5 import QwtPlot
62
from guiqwt.config import _, CONF, make_title
63
from guiqwt.curve import CurveItem, ErrorBarCurveItem, GridItem
64
from guiqwt.histogram import HistogramItem
65
from guiqwt.image import (ImageItem, QuadGridItem, TrImageItem, XYImageItem,
67
from guiqwt.shapes import (XRangeSelection, RectangleShape, EllipseShape,
69
from guiqwt.annotations import (AnnotatedRectangle, AnnotatedEllipse,
71
from guiqwt.styles import (update_style_attr, CurveParam, ErrorBarParam,
72
style_generator, LabelParam, LegendParam, ImageParam,
73
TrImageParam, HistogramParam, Histogram2DParam,
74
ImageFilterParam, MARKERS, COLORS, GridParam,
75
LineStyleParam, AnnotationParam,
76
LabelParamWithContents)
77
from guiqwt.label import (LabelItem, LegendBoxItem, RangeComputation,
78
RangeComputation2d, DataInfoLabel,
79
SelectedLegendBoxItem)
80
from guiqwt.io import imagefile_to_array
83
# default offset positions for anchors
101
class PlotItemBuilder(object):
103
This is just a bare class used to regroup
104
a set of factory functions in a single object
107
'bottom': QwtPlot.xBottom,
108
'left' : QwtPlot.yLeft,
109
'top' : QwtPlot.xTop,
110
'right' : QwtPlot.yRight,
114
self.style = style_generator()
116
def gridparam(self, background=None,
117
major_enabled=None, minor_enabled=None,
118
major_style=None, minor_style=None):
120
Make `guiqwt.styles.GridParam` instance
121
* background = canvas background color
122
* major_enabled = tuple (major_xenabled, major_yenabled)
123
* minor_enabled = tuple (minor_xenabled, minor_yenabled)
124
* major_style = tuple (major_xstyle, major_ystyle)
125
* minor_style = tuple (minor_xstyle, minor_ystyle)
127
Style: tuple (style, color, width)
129
gridparam = GridParam(title=_("Grid"), icon="lin_lin.png")
130
gridparam.read_config(CONF, "plot", "grid")
131
if background is not None:
132
gridparam.background = background
133
if major_enabled is not None:
134
gridparam.maj_xenabled, gridparam.maj_yenabled = major_enabled
135
if minor_enabled is not None:
136
gridparam.min_xenabled, gridparam.min_yenabled = minor_enabled
137
if major_style is not None:
138
style = LineStyleParam()
139
linestyle, color, style.width = major_style
140
style.set_style_from_matlab(linestyle)
141
style.color = COLORS.get(color, color) # MATLAB-style
142
if minor_style is not None:
143
style = LineStyleParam()
144
linestyle, color, style.width = minor_style
145
style.set_style_from_matlab(linestyle)
146
style.color = COLORS.get(color, color) # MATLAB-style
149
def grid(self, background=None, major_enabled=None, minor_enabled=None,
150
major_style=None, minor_style=None):
152
Make a grid `plot item` (`guiqwt.curve.GridItem` object)
153
* background = canvas background color
154
* major_enabled = tuple (major_xenabled, major_yenabled)
155
* minor_enabled = tuple (minor_xenabled, minor_yenabled)
156
* major_style = tuple (major_xstyle, major_ystyle)
157
* minor_style = tuple (minor_xstyle, minor_ystyle)
159
Style: tuple (style, color, width)
161
gridparam = self.gridparam(background, major_enabled, minor_enabled,
162
major_style, minor_style)
163
return GridItem(gridparam)
165
def __set_axes(self, curve, xaxis, yaxis):
167
for axis in (xaxis, yaxis):
168
if axis not in self.AXES:
169
raise RuntimeError("Unknown axis %s" % axis)
170
curve.setXAxis(self.AXES[xaxis])
171
curve.setYAxis(self.AXES[yaxis])
173
def __set_param(self, param, title, color, linestyle, linewidth,
174
marker, markersize, markerfacecolor, markeredgecolor,
175
shade, fitted, curvestyle, curvetype, baseline):
176
"""Apply parameters to a `guiqwt.styles.CurveParam` instance"""
179
if color is not None:
180
color = COLORS.get(color, color) # MATLAB-style
181
param.line.color = color
182
if linestyle is not None:
183
param.line.set_style_from_matlab(linestyle)
184
if linewidth is not None:
185
param.line.width = linewidth
186
if marker is not None:
187
if marker in MARKERS:
188
param.symbol.update_param(MARKERS[marker]) # MATLAB-style
190
param.symbol.marker = marker
191
if markersize is not None:
192
param.symbol.size = markersize
193
if markerfacecolor is not None:
194
markerfacecolor = COLORS.get(markerfacecolor,
195
markerfacecolor) # MATLAB-style
196
param.symbol.facecolor = markerfacecolor
197
if markeredgecolor is not None:
198
markeredgecolor = COLORS.get(markeredgecolor,
199
markeredgecolor) # MATLAB-style
200
param.symbol.edgecolor = markeredgecolor
201
if shade is not None:
203
if fitted is not None:
204
param.fitted = fitted
205
if curvestyle is not None:
206
param.curvestyle = curvestyle
207
if curvetype is not None:
208
param.curvetype = curvetype
209
if baseline is not None:
210
param.baseline = baseline
212
def __get_arg_triple_plot(self, args):
213
"""Convert MATLAB-like arguments into x, y, style"""
214
def get_x_y_from_data(data):
215
if len(data.shape) == 1 or data.shape[0] == 1 or data.shape[1] == 1:
216
x = arange(data.size)
219
x = arange(len(data[:, 0]))
220
y = [data[:, i] for i in range(len(data[0, :]))]
224
if isinstance(args[0], basestring):
229
x, y = get_x_y_from_data(args[0])
230
if isinstance(y, ndarray):
231
style = self.style.next()
233
style = [self.style.next() for yi in y]
236
if isinstance(a2, basestring):
237
x, y = get_x_y_from_data(a1)
242
style = self.style.next()
246
raise TypeError("Wrong number of arguments")
249
def __get_arg_triple_errorbar(self, args):
250
"""Convert MATLAB-like arguments into x, y, style"""
255
style = self.style.next()
258
if isinstance(a3, basestring):
266
style = self.style.next()
268
a1, a2, a3, a4 = args
269
if isinstance(a4, basestring):
270
x, y, dy = a1, a2, a3
275
style = self.style.next()
277
x, y, dx, dy, style = args
279
raise TypeError("Wrong number of arguments")
280
return x, y, dx, dy, style
282
def mcurve(self, *args, **kwargs):
284
Make a curve `plot item` based on MATLAB-like syntax
285
(may returns a list of curves if data contains more than one signal)
286
(:py:class:`guiqwt.curve.CurveItem` object)
288
Example: mcurve(x, y, 'r+')
290
x, y, style = self.__get_arg_triple_plot(args)
291
if isinstance(y, ndarray):
293
if not isinstance(style, list):
295
if len(y) > len(style):
296
style = [style[0]]*len(y)
297
basename = _("Curve")
299
for yi, stylei in zip(y, style):
300
param = CurveParam(title=basename, icon='curve.png')
301
if "label" in kwargs:
302
param.label = kwargs.pop("label")
306
param.label = make_title(basename, CURVE_COUNT)
307
update_style_attr(stylei, param)
308
curves.append(self.pcurve(x, yi, param, **kwargs))
314
def pcurve(self, x, y, param, xaxis="bottom", yaxis="left"):
316
Make a curve `plot item`
317
based on a `guiqwt.styles.CurveParam` instance
318
(:py:class:`guiqwt.curve.CurveItem` object)
320
Usage: pcurve(x, y, param)
322
curve = CurveItem(param)
324
curve.update_params()
325
self.__set_axes(curve, xaxis, yaxis)
328
def curve(self, x, y, title=u"",
329
color=None, linestyle=None, linewidth=None,
330
marker=None, markersize=None, markerfacecolor=None,
331
markeredgecolor=None, shade=None, fitted=None,
332
curvestyle=None, curvetype=None, baseline=None,
333
xaxis="bottom", yaxis="left"):
335
Make a curve `plot item` from x, y, data
336
(:py:class:`guiqwt.curve.CurveItem` object)
339
* color: curve color name
340
* linestyle: curve line style (MATLAB-like string or attribute name
341
from the :py:class:`PyQt4.QtCore.Qt.PenStyle` enum
342
(i.e. "SolidLine" "DashLine", "DotLine", "DashDotLine",
343
"DashDotDotLine" or "NoPen")
344
* linewidth: line width (pixels)
345
* marker: marker shape (MATLAB-like string or attribute name from
346
the :py:class:`PyQt4.Qwt5.QwtSymbol.Style` enum (i.e. "Cross",
347
"Ellipse", "Star1", "XCross", "Rect", "Diamond", "UTriangle",
348
"DTriangle", "RTriangle", "LTriangle", "Star2" or "NoSymbol")
349
* markersize: marker size (pixels)
350
* markerfacecolor: marker face color name
351
* markeredgecolor: marker edge color name
352
* shade: 0 <= float <= 1 (curve shade)
353
* fitted: boolean (fit curve to data)
354
* curvestyle: attribute name from the
355
:py:class:`PyQt4.Qwt5.QwtPlotCurve.CurveStyle` enum
356
(i.e. "Lines", "Sticks", "Steps", "Dots" or "NoCurve")
357
* curvetype: attribute name from the
358
:py:class:`PyQt4.Qwt5.QwtPlotCurve.CurveType` enum
359
(i.e. "Yfx" or "Xfy")
360
* baseline (float: default=0.0): the baseline is needed for filling
361
the curve with a brush or the Sticks drawing style.
362
The interpretation of the baseline depends on the curve type
363
(horizontal line for "Yfx", vertical line for "Xfy")
364
* xaxis, yaxis: X/Y axes bound to curve
367
curve(x, y, marker='Ellipse', markerfacecolor='#ffffff')
368
which is equivalent to (MATLAB-style support):
369
curve(x, y, marker='o', markerfacecolor='w')
371
basename = _("Curve")
372
param = CurveParam(title=basename, icon='curve.png')
376
title = make_title(basename, CURVE_COUNT)
377
self.__set_param(param, title, color, linestyle, linewidth, marker,
378
markersize, markerfacecolor, markeredgecolor,
379
shade, fitted, curvestyle, curvetype, baseline)
380
return self.pcurve(x, y, param, xaxis, yaxis)
382
def merror(self, *args, **kwargs):
384
Make an errorbar curve `plot item` based on MATLAB-like syntax
385
(:py:class:`guiqwt.curve.ErrorBarCurveItem` object)
387
Example: mcurve(x, y, 'r+')
389
x, y, dx, dy, style = self.__get_arg_triple_errorbar(args)
390
basename = _("Curve")
391
curveparam = CurveParam(title=basename, icon='curve.png')
392
errorbarparam = ErrorBarParam(title=_("Error bars"),
394
if "label" in kwargs:
395
curveparam.label = kwargs["label"]
399
curveparam.label = make_title(basename, CURVE_COUNT)
400
update_style_attr(style, curveparam)
401
errorbarparam.color = curveparam.line.color
402
return self.perror(x, y, dx, dy, curveparam, errorbarparam)
404
def perror(self, x, y, dx, dy, curveparam, errorbarparam,
405
xaxis="bottom", yaxis="left"):
407
Make an errorbar curve `plot item`
408
based on a `guiqwt.styles.ErrorBarParam` instance
409
(:py:class:`guiqwt.curve.ErrorBarCurveItem` object)
412
* dx: None, or scalar, or 1D NumPy array
413
* dy: None, or scalar, or 1D NumPy array
414
* curveparam: `guiqwt.styles.CurveParam` object
415
* errorbarparam: `guiqwt.styles.ErrorBarParam` object
416
* xaxis, yaxis: X/Y axes bound to curve
418
Usage: perror(x, y, dx, dy, curveparam, errorbarparam)
420
curve = ErrorBarCurveItem(curveparam, errorbarparam)
421
curve.set_data(x, y, dx, dy)
422
curve.update_params()
423
self.__set_axes(curve, xaxis, yaxis)
426
def error(self, x, y, dx, dy, title=u"",
427
color=None, linestyle=None, linewidth=None, marker=None,
428
markersize=None, markerfacecolor=None, markeredgecolor=None,
429
shade=None, fitted=None, curvestyle=None, curvetype=None,
430
baseline=None, xaxis="bottom", yaxis="left"):
432
Make an errorbar curve `plot item`
433
(:py:class:`guiqwt.curve.ErrorBarCurveItem` object)
436
* dx: None, or scalar, or 1D NumPy array
437
* dy: None, or scalar, or 1D NumPy array
438
* color: curve color name
439
* linestyle: curve line style (MATLAB-like string or attribute name
440
from the :py:class:`PyQt4.QtCore.Qt.PenStyle` enum
441
(i.e. "SolidLine" "DashLine", "DotLine", "DashDotLine",
442
"DashDotDotLine" or "NoPen")
443
* linewidth: line width (pixels)
444
* marker: marker shape (MATLAB-like string or attribute name from
445
the :py:class:`PyQt4.Qwt5.QwtSymbol.Style` enum (i.e. "Cross",
446
"Ellipse", "Star1", "XCross", "Rect", "Diamond", "UTriangle",
447
"DTriangle", "RTriangle", "LTriangle", "Star2" or "NoSymbol")
448
* markersize: marker size (pixels)
449
* markerfacecolor: marker face color name
450
* markeredgecolor: marker edge color name
451
* shade: 0 <= float <= 1 (curve shade)
452
* fitted: boolean (fit curve to data)
453
* curvestyle: attribute name from the
454
:py:class:`PyQt4.Qwt5.QwtPlotCurve.CurveStyle` enum
455
(i.e. "Lines", "Sticks", "Steps", "Dots" or "NoCurve")
456
* curvetype: attribute name from the
457
:py:class:`PyQt4.Qwt5.QwtPlotCurve.CurveType` enum
458
(i.e. "Yfx" or "Xfy")
459
* baseline (float: default=0.0): the baseline is needed for filling
460
the curve with a brush or the Sticks drawing style.
461
The interpretation of the baseline depends on the curve type
462
(horizontal line for "Yfx", vertical line for "Xfy")
463
* xaxis, yaxis: X/Y axes bound to curve
466
error(x, y, None, dy, marker='Ellipse', markerfacecolor='#ffffff')
467
which is equivalent to (MATLAB-style support):
468
error(x, y, None, dy, marker='o', markerfacecolor='w')
470
basename = _("Curve")
471
curveparam = CurveParam(title=basename, icon='curve.png')
472
errorbarparam = ErrorBarParam(title=_("Error bars"),
477
curveparam.label = make_title(basename, CURVE_COUNT)
478
self.__set_param(curveparam, title, color, linestyle, linewidth, marker,
479
markersize, markerfacecolor, markeredgecolor,
480
shade, fitted, curvestyle, curvetype, baseline)
481
errorbarparam.color = curveparam.line.color
482
return self.perror(x, y, dx, dy, curveparam, errorbarparam,
485
def histogram(self, data, bins=None, logscale=None, remove_first_bin=None,
486
title=u"", color=None, xaxis="bottom", yaxis="left"):
488
Make 1D Histogram `plot item`
489
(:py:class:`guiqwt.histogram.HistogramItem` object)
490
* data (1D NumPy array)
491
* bins: number of bins (int)
492
* logscale: Y-axis scale (bool)
494
basename = _("Histogram")
495
histparam = HistogramParam(title=basename, icon='histogram.png')
496
curveparam = CurveParam(_("Curve"), icon='curve.png')
497
curveparam.read_config(CONF, "histogram", "curve")
499
global HISTOGRAM_COUNT
501
title = make_title(basename, HISTOGRAM_COUNT)
502
curveparam.label = title
503
if color is not None:
504
curveparam.line.color = color
506
histparam.n_bins = bins
507
if logscale is not None:
508
histparam.logscale = logscale
509
if remove_first_bin is not None:
510
histparam.remove_first_bin = remove_first_bin
511
return self.phistogram(data, curveparam, histparam, xaxis, yaxis)
513
def phistogram(self, data, curveparam, histparam,
514
xaxis="bottom", yaxis="left"):
516
Make 1D histogram `plot item`
517
(:py:class:`guiqwt.histogram.HistogramItem` object)
518
based on a `guiqwt.styles.CurveParam` and
519
`guiqwt.styles.HistogramParam` instances
521
Usage: phistogram(data, curveparam, histparam)
523
hist = HistogramItem(curveparam, histparam)
525
hist.set_hist_data(data)
526
self.__set_axes(hist, xaxis, yaxis)
529
def __set_image_param(self, param, title, background_color,
530
alpha_mask, alpha, colormap, **kwargs):
536
param.label = make_title(_("Image"), IMAGE_COUNT)
537
if background_color is not None:
538
param.background = background_color
539
if alpha_mask is not None:
540
param.alpha_mask = alpha_mask
541
if alpha is not None:
543
if colormap is not None:
544
param.colormap = colormap
545
for key, val in kwargs.items():
546
setattr(param, key, val)
548
def _get_image_data(self, data, filename, title, cmap):
550
assert filename is not None
551
data = imagefile_to_array(filename)
552
if title is None and filename is not None:
553
title = osp.basename(filename)
554
return data, filename, title, cmap
556
def image(self, data=None, filename=None, title=None, background_color=None,
557
alpha_mask=None, alpha=None, colormap=None,
558
xaxis="bottom", yaxis="left", zaxis="right"):
560
Make an image `plot item` from data
561
(:py:class:`guiqwt.image.ImageItem` object)
563
param = ImageParam(title=_("Image"), icon='image.png')
564
params = self._get_image_data(data, filename, title, colormap)
565
data, filename, title, colormap = params
566
self.__set_image_param(param, title, background_color,
567
alpha_mask, alpha, colormap)
568
image = ImageItem(data, param)
569
image.set_filename(filename)
572
def quadgrid(self, X, Y, Z, filename=None, title=None,
573
background_color=None, alpha_mask=None, alpha=None,
574
colormap=None, xaxis="bottom", yaxis="left", zaxis="right"):
576
Make a pseudocolor `plot item` of a 2D array
577
(:py:class:`guiqwt.image.QuadGridItem` object)
579
param = ImageParam(title=_("Image"), icon='image.png')
580
self.__set_image_param(param, title, background_color,
581
alpha_mask, alpha, colormap)
582
image = QuadGridItem(X, Y, Z, param)
585
def pcolor(self, *args, **kwargs):
587
Make a pseudocolor `plot item` of a 2D array
588
based on MATLAB-like syntax
589
(:py:class:`guiqwt.image.QuadGridItem` object)
598
X, Y = meshgrid(arange(N, dtype=Z.dtype), arange(M, dtype=Z.dtype))
602
raise RuntimeError("1 or 3 non-keyword arguments expected")
603
return self.quadgrid(X, Y, Z, **kwargs)
605
def trimage(self, data=None, filename=None, title=None,
606
background_color=None, alpha_mask=None, alpha=None,
607
colormap=None, xaxis="bottom", yaxis="left", zaxis="right",
608
x0=0.0, y0=0.0, angle=0.0, dx=1.0, dy=1.0,
609
interpolation='linear'):
611
Make a transformable image `plot item` (image with an arbitrary
613
(:py:class:`guiqwt.image.TrImageItem` object)
614
* data: 2D NumPy array (image pixel data)
615
* filename: image filename (if data is not specified)
616
* title: image title (optional)
618
* angle: angle (radians)
619
* dx, dy: pixel size along X and Y axes
620
* interpolation: 'nearest', 'linear' (default), 'antialiasing' (5x5)
622
param = TrImageParam(title=_("Image"), icon='image.png')
623
params = self._get_image_data(data, filename, title, colormap)
624
data, filename, title, colormap = params
625
self.__set_image_param(param, title, background_color,
626
alpha_mask, alpha, colormap,
627
x0=x0, y0=y0, angle=angle, dx=dx, dy=dy)
628
interp_methods = {'nearest': 0, 'linear': 1, 'antialiasing': 5}
629
param.interpolation = interp_methods[interpolation]
630
image = TrImageItem(data, param)
631
image.set_filename(filename)
634
def xyimage(self, x, y, data, title=None, background_color=None,
635
alpha_mask=None, alpha=None, colormap=None,
636
xaxis="bottom", yaxis="left", zaxis="right"):
638
Make an xyimage `plot item` (image with non-linear X/Y axes) from data
639
(:py:class:`guiqwt.image.XYImageItem` object)
642
* data: 2D NumPy array (image pixel data)
643
* title: image title (optional)
645
param = ImageParam(title=_("Image"), icon='image.png')
646
self.__set_image_param(param, title, background_color,
647
alpha_mask, alpha, colormap)
648
return XYImageItem(x, y, data, param)
650
def imagefilter(self, xmin, xmax, ymin, ymax,
651
imageitem, filter, title=None):
653
Make a rectangular area image filter `plot item`
654
(:py:class:`guiqwt.image.ImageFilterItem` object)
655
* xmin, xmax, ymin, ymax: filter area bounds
656
* imageitem: An imageitem instance
657
* filter: function (x, y, data) --> data
659
param = ImageFilterParam(_("Filter"), icon="funct.png")
660
param.xmin, param.xmax, param.ymin, param.ymax = xmin, xmax, ymin, ymax
661
if title is not None:
663
filt = imageitem.get_filter(filter, param)
664
_m, _M = imageitem.get_lut_range()
665
filt.set_lut_range([_m, _M])
668
def histogram2D(self, X, Y, NX=None, NY=None, logscale=None,
669
title=None, transparent=None):
671
Make a 2D Histogram `plot item`
672
(:py:class:`guiqwt.image.Histogram2DItem` object)
675
* NX: Number of bins along x-axis (int)
676
* NY: Number of bins along y-axis (int)
677
* logscale: Z-axis scale (bool)
678
* title: item title (string)
679
* transparent: enable transparency (bool)
681
basename = _("2D Histogram")
682
param = Histogram2DParam(title=basename, icon='histogram2d.png')
687
if logscale is not None:
688
param.logscale = int(logscale)
689
if title is not None:
692
global HISTOGRAM2D_COUNT
693
HISTOGRAM2D_COUNT += 1
694
param.label = make_title(basename, HISTOGRAM2D_COUNT)
695
if transparent is not None:
696
param.transparent = transparent
697
return Histogram2DItem(X, Y, param)
699
def label(self, text, g, c, anchor, title=""):
701
Make a label `plot item`
702
(:py:class:`guiqwt.label.LabelItem` object)
703
* text: label text (string)
704
* g: position in plot coordinates (tuple)
705
or relative position (string)
706
* c: position in canvas coordinates (tuple)
707
* anchor: anchor position in relative position (string)
708
* title: label name (optional)
711
make.label("Relative position", (x[0], y[0]), (10, 10), "BR")
712
make.label("Absolute position", "R", (0,0), "R")
714
basename = _("Label")
715
param = LabelParamWithContents(basename, icon='label.png')
716
param.read_config(CONF, "plot", "label")
722
param.label = make_title(basename, LABEL_COUNT)
723
if isinstance(g, tuple):
725
param.xg, param.yg = g
730
c = ANCHOR_OFFSETS[anchor]
731
param.xc, param.yc = c
732
param.anchor = anchor
733
return LabelItem(text, param)
735
def legend(self, anchor='TR', c=None, restrict_items=None):
737
Make a legend `plot item`
738
(:py:class:`guiqwt.label.LegendBoxItem` or
739
:py:class:`guiqwt.label.SelectedLegendBoxItem` object)
740
* anchor: legend position in relative position (string)
741
* c (optional): position in canvas coordinates (tuple)
742
* restrict_items (optional):
743
- None: all items are shown in legend box
745
- [item1, item2]: item1, item2 are shown in legend box
747
param = LegendParam(_("Legend"), icon='legend.png')
748
param.read_config(CONF, "plot", "legend")
751
param.anchor = anchor
753
c = ANCHOR_OFFSETS[anchor]
754
param.xc, param.yc = c
755
if restrict_items is None:
756
return LegendBoxItem(param)
758
return SelectedLegendBoxItem(param, restrict_items)
760
def range(self, xmin, xmax):
761
return XRangeSelection(xmin, xmax)
763
def __shape(self, shapeclass, x0, y0, x1, y1, title=None):
764
shape = shapeclass(x0, y0, x1, y1)
765
shape.set_style("plot", "shape/drag")
766
if title is not None:
767
shape.setTitle(title)
770
def rectangle(self, x0, y0, x1, y1, title=None):
772
Make a rectangle shape `plot item`
773
(:py:class:`guiqwt.shapes.RectangleShape` object)
774
* x0, y0, x1, y1: rectangle coordinates
775
* title: label name (optional)
777
return self.__shape(RectangleShape, x0, y0, x1, y1, title)
779
def ellipse(self, x0, y0, x1, y1, ratio, title=None):
781
Make an ellipse shape `plot item`
782
(:py:class:`guiqwt.shapes.EllipseShape` object)
783
* x0, y0, x1, y1: ellipse x-axis coordinates
784
* ratio: ratio between y-axis and x-axis lengths
785
* title: label name (optional)
787
shape = EllipseShape(x0, y0, x1, y1, ratio)
788
shape.set_style("plot", "shape/drag")
789
if title is not None:
790
shape.setTitle(title)
793
def circle(self, x0, y0, x1, y1, title=None):
795
Make a circle shape `plot item`
796
(:py:class:`guiqwt.shapes.EllipseShape` object)
797
* x0, y0, x1, y1: circle diameter coordinates
798
* title: label name (optional)
800
return self.ellipse(x0, y0, x1, y1, 1., title=title)
802
def segment(self, x0, y0, x1, y1, title=None):
804
Make a segment shape `plot item`
805
(:py:class:`guiqwt.shapes.SegmentShape` object)
806
* x0, y0, x1, y1: segment coordinates
807
* title: label name (optional)
809
return self.__shape(SegmentShape, x0, y0, x1, y1, title)
811
def __get_annotationparam(self, title, subtitle):
812
param = AnnotationParam(_("Annotation"), icon="annotation.png")
813
if title is not None:
815
if subtitle is not None:
816
param.subtitle = subtitle
819
def __annotated_shape(self, shapeclass, x0, y0, x1, y1, title, subtitle):
820
param = self.__get_annotationparam(title, subtitle)
821
shape = shapeclass(x0, y0, x1, y1, param)
822
shape.set_style("plot", "shape/drag")
825
def annotated_rectangle(self, x0, y0, x1, y1, title=None, subtitle=None):
827
Make an annotated rectangle `plot item`
828
(:py:class:`guiqwt.annotations.AnnotatedRectangle` object)
829
* x0, y0, x1, y1: rectangle coordinates
830
* title, subtitle: strings
832
return self.__annotated_shape(AnnotatedRectangle,
833
x0, y0, x1, y1, title, subtitle)
835
def annotated_ellipse(self, x0, y0, x1, y1, ratio,
836
title=None, subtitle=None):
838
Make an annotated ellipse `plot item`
839
(:py:class:`guiqwt.annotations.AnnotatedEllipse` object)
840
* x0, y0, x1, y1: ellipse rectangle coordinates
841
* ratio: ratio between y-axis and x-axis lengths
842
* title, subtitle: strings
844
param = self.__get_annotationparam(title, subtitle)
845
shape = AnnotatedEllipse(x0, y0, x1, y1, ratio, param)
846
shape.set_style("plot", "shape/drag")
849
def annotated_circle(self, x0, y0, x1, y1, ratio,
850
title=None, subtitle=None):
852
Make an annotated circle `plot item`
853
(:py:class:`guiqwt.annotations.AnnotatedCircle` object)
854
* x0, y0, x1, y1: circle diameter coordinates
855
* title, subtitle: strings
857
return self.annotated_ellipse(x0, y0, x1, y1, 1., title, subtitle)
859
def annotated_segment(self, x0, y0, x1, y1, title=None, subtitle=None):
861
Make an annotated segment `plot item`
862
(:py:class:`guiqwt.annotations.AnnotatedSegment` object)
863
* x0, y0, x1, y1: segment coordinates
864
* title, subtitle: strings
866
return self.__annotated_shape(AnnotatedSegment,
867
x0, y0, x1, y1, title, subtitle)
869
def info_label(self, anchor, comps, title=""):
871
Make an info label `plot item`
872
(:py:class:`guiqwt.label.DataInfoLabel` object)
874
basename = _("Computation")
875
param = LabelParam(basename, icon='label.png')
876
param.read_config(CONF, "plot", "info_label")
882
param.label = make_title(basename, LABEL_COUNT)
885
param.anchor = anchor
886
c = ANCHOR_OFFSETS[anchor]
887
param.xc, param.yc = c
888
return DataInfoLabel(param, comps)
890
def computation(self, range, anchor, label, curve, function):
892
Make a computation label `plot item`
893
(:py:class:`guiqwt.label.DataInfoLabel` object)
894
(see example: :py:mod:`guiqwt.tests.computations`)
896
return self.computations(range, anchor, [ (curve, label, function) ])
898
def computations(self, range, anchor, specs):
900
Make computation labels `plot item`
901
(:py:class:`guiqwt.label.DataInfoLabel` object)
902
(see example: :py:mod:`guiqwt.tests.computations`)
905
for curve, label, function in specs:
906
comp = RangeComputation(label, curve, range, function)
908
return self.info_label(anchor, comps)
910
def computation2d(self, rect, anchor, label, image, function):
912
Make a 2D computation label `plot item`
913
(:py:class:`guiqwt.label.RangeComputation2d` object)
914
(see example: :py:mod:`guiqwt.tests.computations`)
916
return self.computations2d(rect, anchor, [ (image, label, function) ])
918
def computations2d(self, rect, anchor, specs):
920
Make 2D computation labels `plot item`
921
(:py:class:`guiqwt.label.RangeComputation2d` object)
922
(see example: :py:mod:`guiqwt.tests.computations`)
925
for image, label, function in specs:
926
comp = RangeComputation2d(label, image, rect, function)
928
return self.info_label(anchor, comps)
930
make = PlotItemBuilder()
b'\\ No newline at end of file'