Plot.CurveFit¶
- class CurveFit(expression=None)¶
A CurveFit can be added to a
Line
orScatter
, and will then fit the supplied expression to the parent data.- Parameters:
expression (str) – The expression for the fit function.
- addItem(item, emit=True)¶
Add item as child of this item.
- Parameters:
item (PlotItem based) – The item.
emit (bool) – True, if change should be emitted.
- addSubscriber(subscriber)¶
Add a subscriber. Each subscriber is called once every time the plot item updates.
- Parameters:
subscriber (bound method) – The subscriber. Must be callable.
- addTag(*args)¶
Add tag. Used for grouping plot items.
- Parameters:
*args (tuple of str) – The tags to add.
- alpha()¶
- Returns:
The opacity for the line.
- Return type:
float
- applyStyle(style_scheme=None, style_properties=None)¶
Apply style properties from a style scheme.
- Parameters:
style_scheme (dict) – The style scheme.
style_properties (dict | None) – A specific set of style properties to apply. If None, the style properties are retrieved from the plot model using the given style scheme.
- attributes(latex=False, detail_level='full')¶
A description of the fit attributes.
- Parameters:
latex (bool) – If True, return the attributes in Latex format.
detail_level (DETAIL_LEVELS enum) – The level of detail of the attributes.
- Returns:
A description of the fit attributes.
- Return type:
collections.OrderedDict
- bounds()¶
- Returns:
The bounds.
- Return type:
tuple of physicalQuantity
- color()¶
- Returns:
The line color.
- Return type:
str
- conditions()¶
- Returns:
The fit conditions.
- Return type:
collections.OrderedDict
- copy()¶
Make a copy of the plot storable by using the serialize/deserialize functionality.
This creates a new instance with a new id, but all other properties copied.
- determination()¶
- Returns:
The fit coefficient of determination.
- Return type:
float | NoneType
- emit(key, value=None)¶
Emit a single key and value change to all subscribers.
- Parameters:
key (str) – The key to emit.
value (object) – The value to emit.
- evaluate(x_data)¶
Evaluate the curve fit for the given x_data.
- Parameters:
x_data (PhysicalQuantity) – The x-data.
- Returns:
The evaluated x data and y data.
- Return type:
2-tuple of PhysicalQuantity
- expression()¶
- Returns:
The fit expression.
- Return type:
str
- extent(axis_name, padding=0.0)¶
Get the data limits.
- Parameters:
axis_name (str) – ‘x’ or ‘y’.
padding (float) – Padding around the data. Default: 0.0.
- Returns:
The min/max data values for the requested axis.
- Return type:
Sequence | PhysicalQuantity
- classmethod fromProperties(properties)¶
Create a plot storable from a dict of properties.
Used when deserializing a PlotStorable.
- Parameters:
properties (dict) – The properties which are used to populate the properties dict of the newly created plot storable.
- Returns:
The newly created PlotStorable.
- Return type:
PlotStorable
- hoverDetails()¶
- Returns:
The current hover details.
- Return type:
dict | None
- isAxisSpanned()¶
- Returns:
True if the fit line should span the x-axis range of the plot.
- Return type:
bool
- isBoundsVisible()¶
- Returns:
True, if fit bounds are shown in the plot.
- Return type:
bool
- isHoverable()¶
- Returns:
True, if hoverable.
- Return type:
bool
- isHovered()¶
- Returns:
True, if the item is hovered.
- Return type:
bool
- isInteractive()¶
- Returns:
Whether the user can interact with this line.
- Return type:
bool
- isSelectable()¶
- Returns:
True, if the item is selectable.
- Return type:
bool
- isSelected()¶
- Returns:
True, if the item is selected.
- Return type:
bool
- classmethod isValidParent(item)¶
Check if an item is derivable.
- Parameters:
item (PlotItem based) – The item to check.
- Returns:
True, if the item can be derived.
- Return type:
bool
- isVisible()¶
- Returns:
True, if the item is visible.
- Return type:
bool
- items(selected=None, tag=None)¶
Return the requested items.
The optional selected key word filter selected and deselected lines, with the options:
True : Only selected items
False : Only unselected items
None : All items.
- Parameters:
selected (None | bool) – True, if only selected items should be returned.
tag (str) – The tag to filter on.
- Returns:
The child items with the selected filtering.
- Return type:
items
- label()¶
- Returns:
The line label.
- Return type:
str
- lineStyle()¶
- Returns:
The line style.
- Return type:
str | None
- lineWidth()¶
- Returns:
The line width.
- Return type:
float
- markerEdgeColor()¶
- Returns:
The edge color of the marker in hex format.
- Return type:
str
- markerEvery()¶
- Returns:
The number of points between every marker.
- Return type:
int
- markerFaceColor()¶
- Returns:
The face color of the marker in hex format.
- Return type:
str
- markerSize()¶
- Returns:
The marker size.
- Return type:
float
- markerStyle()¶
. :returns: The marker style. :rtype: str | None
- order()¶
- Returns:
A number describing the current order of the item within the PlotModel. Can be used for sorting.
- Return type:
int | None
- owner()¶
- Returns:
The owner type.
- Return type:
int | enum
- parameters()¶
- Returns:
The polynomial coefficients.
- Return type:
numpy.ndarray
- parent()¶
- Returns:
The parent.
- Return type:
PlotItem based | None
- removeItem(item, emit=True)¶
Remove item from the child items.
- Parameters:
item (PlotItem based) – The item to remove.
emit (bool) – True, if removal should be emitted.
- removeSubscriber(subscriber)¶
Remove a subscriber method from the plot item updates.
- Parameters:
subscriber (bound method) – The subscriber. Must be callable.
- safeSetExpression(expression, update=True)¶
Safe setting of expression. Fails silently without changing anything.
- Parameters:
expression (str) – The expression.
update (bool) – True if the fit should be updated.
- selectionDetails()¶
- Returns:
The current selection details.
- Return type:
dict | None
- setAlpha(alpha)¶
Set the opacity for the line.
- Parameters:
alpha (float) – The opacity.
- setBounds(lower, upper, update=True)¶
Set the derived line bounds.
- Parameters:
lower (float | PhysicalQuantity) – The lower bound.
upper (float | PhysicalQuantity) – The upper bound.
update (bool) – True, if we should call update.
- setBoundsVisible(visible)¶
Set whether to indicate the fit bounds in the plot.
- Parameters:
visible (bool) – If True, fit bounds will be shown in the plot.
- setColor(color)¶
Set the line color.
- Parameters:
color (str) – The line color in hex format.
- setConditions(conditions, update=True)¶
Set conditions.
- Parameters:
conditions (dict or list of floats) – The conditions to set.
update (bool) – True, if the fit should be updated.
- setData(x=None, y=None)¶
Overload since derived lines should not expose data setting.
- Parameters:
x (iterable) – The x values.
y (iterable) – They values.
- setExpression(expression, update=True)¶
Set the fit expression.
- Parameters:
expression (str) – The fit expression to set.
update (bool) – True, if the fit should be updated.
- setHoverable(hoverable)¶
Enable or disable hover evetn. When enabled the line will be set to hovered, once the mouse hovers over the item.
- Parameters:
hoverable (bool) – True, if the item should be sensitive to hover events.
- setHovered(hovered, details=None)¶
Set hovered with some details. This is called once the mouse hovers over the item. Should only be called once hoverable has been enabled.
- Parameters:
hovered (bool) – True, if the item was hovered.
details (dict | NoneType) – The hover details.
- setInteractive(interactive)¶
Set whether the user can interact with this line.
- Parameters:
interactive (bool) – The desired state.
- setLabel(label)¶
Set the label.
- Parameters:
label (str) – The line label.
- setLineStyle(line_style)¶
Set the line style.
- Parameters:
line_style (str | None) – The line style.
- setLineWidth(line_width)¶
Set the line width.
- Parameters:
line_width (float) – The line width.
- setMarkerColor(color)¶
Set color of the marker. Remember to also set a valid marker style.
- Parameters:
color (valid color) – The color to set.
- setMarkerEdgeColor(color)¶
Set the marker edge color. Remember to also set a valid marker style.
- Parameters:
color (str) – The edge color of the marker.
- setMarkerEvery(every)¶
Set the number of points between each marker. Remember to also set a valid marker style.
- Parameters:
every (int) – The number of points between every marker.
- setMarkerFaceColor(color)¶
Set the marker face-color. Remember to also set a valid marker style.
- Parameters:
color (str) – The face color of the marker.
- setMarkerSize(size)¶
Set the marker size. Remember to also set a valid marker style.
- Parameters:
size (float) – The marker size.
- setMarkerStyle(marker)¶
Set the marker style.
- Parameters:
marker (str | None) – The marker style.
- setOwner(owner)¶
Set the owner of the item (either OWNERS.MODEL or OWNERS.VIEW).
- Parameters:
owner (int | enum) – The owner type.
- setSelectable(selectable)¶
Set whether this item is selectable.
- Parameters:
selectable (bool) – True, if the item should be selectable.
- setSelected(selected, details=None)¶
Set the item as selected or unselected.
- Parameters:
selected (bool) – True, if item should be selected.
details (dict | NoneType) – The selection details.
- setSpanAxis(span_axis)¶
Set whether the fit line should span the x-axis range of the plot.
- Parameters:
span_axis (bool) – If True, the fit line should span the x-axis range of the plot.
- setVisible(visible)¶
Set visible.
- Parameters:
visible (bool) – True, if the item should be visible.
- sourceData()¶
Checks if the source data is valid, and if yes, return the valid source data on the form: (xs, ys, x_min, x_max, x_unit, y_unit) Else returns
None
- Returns:
The validated and clipped source data.
- Return type:
tuple |
None
- style()¶
- Returns:
All the current style properties.
- Return type:
dict
- styleProperties(style_scheme)¶
Check if the given style scheme has a style for this item.
- Parameters:
style_scheme (dict) – The style scheme.
- Returns:
The style, if any.
- Return type:
dict | None
- tags()¶
- Returns:
The set of tags.
- Return type:
set
- update(data=None)¶
Perform the update.
- Parameters:
data (tuple | NoneType) – The source data to use.
- validSourceCoordinates(xs, ys)¶
Check valid source coordinates.
- Parameters:
xs (numpy.ndarray) – The x data values.
ys (numpy.ndarray) – The y data values.
- Returns:
True, if the source coordinates are valid input for the fit algorithm.
- Return type:
bool
- xData()¶
- Returns:
The x-data.
- Return type:
- yData()¶
- Returns:
The y-data.
- Return type:
Usage Examples¶
Let us first construct a line object containing the data that we wish to fit.:
line = Plot.Line([0, 1, 2, 3, 4, 5], [1.3, 2.3, 4.2, 4.6, 2.6, 1.1])
Fitting to a parabola, can e.g. be done like this:
fit = Plot.CurveFit('curvature * (x - x0)**2 + offset')
line.addItem(fit)
print(fit.parameters())
print('R2=', fit.determination())
This will output the following values for curvature
, x0
and offset
:
{
'curvature': -0.5017857159048791,
'x0': 2.50854092811564,
'offset': 4.14691159949586
}
R2=0.8912692142744284
Of course something similar could have been achieved using a Plot.PolynomialFit. However, CurveFit can also fit to e.g. a gaussian curve:
fit = Plot.CurveFit('a * gaussian(x - b, c)')
line.addItem(fit)
print(fit.parameters())
print('R2=', fit.determination())
And in this case it produces a slightly better fit as evidenced by the R2
value:
{
'a': 16.505234582600096,
'b': 2.5597234857461135,
'c': 1.6884506777324249
}
R2= 0.9739999352271602
Notes¶
CurveFit can fit to any expression y = f(x)
. The expression can be composed of all valid python algebraic
components like numbers, operators +, -, /, *`
, the power operator **
, the imaginary unit 1j
and many more.
In addition a number of special functions can be used.
Function |
Notes |
---|---|
|
Absolute value |
|
Real part |
|
Imaginary part |
|
Sine (radians) |
|
Cosine (radians) |
|
Tangent (radians) |
|
Inverse sine |
|
Inverse cosine |
|
Inverse tangent |
|
Inverse tangent of a/b in correct quadrant. |
|
Exponential |
|
exp(a) - 1 |
|
Hyperbolic sine |
|
Hyperbolic cosine |
|
Hyperbolic tangent |
|
Inverse sinh |
|
Inverse cosh |
|
Inverse tanh |
|
Natural logarithm |
|
Logarithm base 10 |
|
Logarithm base 2 |
|
Square root |
|
Error function |
|
1 - erf |
|
Euler gamma function |
|
Ceiling function |
|
Floor function |
|
Heaviside theta function, theta(a) = 0 for a < 0 and 1 for a > 0. |
|
Heaviside theta function |
|
Sign function |
|
Sum |
|
Cumulative sum |
|
Cumulative trapezoidal sum | |
|
1D gradient |
|
Faddeeva function |
|
Lorentzian function, where |
|
Gaussian function, wheere |
|
Voigt profile. Convolution of gaussian and lorentzian function. Here, |
|
Range of values from a to b with custom step. |