1 import matplotlib.pyplot
as plt
2 import matplotlib.widgets
5 def findRangesOfEqualSign(data):
12 for index, value
in enumerate(data):
20 range_ = [lastPos, index]
21 ranges.append((range_, sign))
26 range_ = [lastPos, len(data)]
27 ranges.append((range_, sign))
31 def plotDashedIfNegative(x, y, yErrors=None, logY=False, defaultLowerErrorBound=1e-30, ** kwargs):
32 ax = kwargs.pop(
'ax', plt.gca())
33 kwargs.pop(
'linestyle',
None)
35 ranges = findRangesOfEqualSign(y)
38 return ax.plot(x, y, **kwargs)
42 for range_, sign
in ranges:
43 linestyle =
'-' if sign
else '--'
45 currentX = x[range_[0]:range_[1]]
46 currentY = y[range_[0]:range_[1]]
48 currentY = -numpy.array(currentY)
49 current_line, = ax.plot(currentX, currentY, linestyle=linestyle, **kwargs)
50 lines.append(current_line)
53 color = current_line.get_color()
54 kwargs[
'color'] = color
55 kwargs.pop(
'label',
None)
57 if yErrors
is not None:
61 for key, value
in enumerate(y):
62 lower = value - yErrors[key]
63 upper = value + yErrors[key]
66 lower, upper = -upper, -lower
70 lower = defaultLowerErrorBound
73 upper = defaultLowerErrorBound
75 lowerBounds.append(lower)
76 upperBounds.append(upper)
78 ax.fill_between(x, lowerBounds, upperBounds, alpha=0.3)
83 class DiscreteSliderWidget(matplotlib.widgets.Slider):
85 A matplotlib-compatible slider widget, the values of which are discrete.
92 The following keyword arguments are accepted for speacial treatment in
94 - `stepSize` specifies the step size with which the slider is allowed
95 to move. That is, values that can be taken on by the slider are
96 integer multiples of `stepSize`. This value must be of type `int` and
97 be positive. Defaults to `1`.
100 Throws if any of the parameters described in the main body of
101 the documentation of this class is of the wrong type.
103 Throws if any of the parameters described in the main body of
104 the documentation of this class has an invalid value.
107 Positional arguments, which will be passed to the base class'
110 Keyword arguments, which will be passed to the base class'
111 constructor, with the exception of the keyword arguments
112 described in the main body of the documentation of this
117 self.
_valinit = kwargs.get(
'valinit',
None)
121 kwargs[
'valinit'] = 1e-10
128 matplotlib.widgets.Slider.__init__(self, *args, **kwargs)
131 def set_val(self, continuousValue):
133 Called when the slider moves.
135 @param[in] continuousValue
136 The value the slider has been moved to, as a continuous
140 self.
val = continuousValue
144 self.poly.xy[2][0] = discreteValue
145 self.poly.xy[3][0] = discreteValue
147 self.valtext.set_text(self.valfmt % discreteValue)
150 self.ax.figure.canvas.draw()
153 for callback
in self.observers.values():
154 callback(discreteValue)