diff --git a/docs/changes.md b/docs/changes.md index b805d613..9ec2f452 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -44,6 +44,7 @@ Changes that will break existing code and need active thinking and work to adapt tests/issues/issue_1146.py examples/advanced/spline_draw2.py examples/volumetric/isosurfaces2.py +examples/pyplot/fit_curve2.py ``` diff --git a/docs/examples_db.js b/docs/examples_db.js index 4d501a66..7374c048 100755 --- a/docs/examples_db.js +++ b/docs/examples_db.js @@ -1453,7 +1453,7 @@ vedo_example_db = imgsrc: 'images/pyplot/fit_erf.png', }, { - pyname: 'fit_curve', + pyname: 'fit_curve1', kbd : '', categ : 'plotting', short : 'fit data w/ error bars', diff --git a/examples/pyplot/fit_curve.py b/examples/pyplot/fit_curve1.py similarity index 100% rename from examples/pyplot/fit_curve.py rename to examples/pyplot/fit_curve1.py diff --git a/examples/pyplot/fit_curve2.py b/examples/pyplot/fit_curve2.py new file mode 100644 index 00000000..a35855c5 --- /dev/null +++ b/examples/pyplot/fit_curve2.py @@ -0,0 +1,44 @@ +"""Create slider that changes the value of the k parameter in the function.""" +import numpy as np +from vedo.pyplot import plot +from vedo import settings, Line, Text2D, Plotter + + +settings.default_font = "Brachium" +settings.remember_last_figure_format = True + + +def func(x, h, a, x0, k): + return h + a * (x-x0) * np.sin((x-x0)**2 / k) + + +def callback(w, e): + y = func(xdata, *true_params[:3], slider.value) + res = np.sum((ydata - y)**2 / 100) + txt2d.text(f"Residuals: {res:.3f}") + # remove previous fit line and insert the new one + line = Line(np.c_[xdata, y], c="green4", lw=3) + p.remove(p[2]).insert(line) + + +true_params = [20, 2, 8, 3] +xdata = np.linspace(3, 10, 100) +ydata_true = func(xdata, *true_params) +ydata = np.random.normal(ydata_true, 3, 100) + +p = plot( + xdata, + ydata, + "o", + mc="blue2", + title="f = h + a*(x-x_0 )*sin((x-x_0 )**2 /k)", + label="Data", +) +p += plot(xdata, ydata_true, "-g", lw=2, label="Fit") +p.add_legend(pos="top-right") + +txt2d = Text2D(pos="bottom-left", bg='yellow5', s=1.2) + +plt = Plotter(size=(900, 650)) +slider = plt.add_slider(callback, 1, 5, value=3, title="k-value") +plt.show(p, txt2d, __doc__, zoom=1.3, mode="2d") diff --git a/vedo/assembly.py b/vedo/assembly.py index 832578f2..5d70132f 100644 --- a/vedo/assembly.py +++ b/vedo/assembly.py @@ -133,9 +133,7 @@ def __str__(self): return out.rstrip() + "\x1b[0m" def __iadd__(self, obj): - """ - Add an object to the group - """ + """Add an object to the group.""" if not vedo.utils.is_sequence(obj): obj = [obj] for a in obj: @@ -144,7 +142,30 @@ def __iadd__(self, obj): self.AddPart(a) except TypeError: self.AddPart(a.actor) - self.objcects.append(a) + self.objects.append(a) + return self + + def __isub__(self, obj): + """Remove an object to the group.""" + if not vedo.utils.is_sequence(obj): + obj = [obj] + for a in obj: + if a: + try: + self.RemovePart(a) + except TypeError: + self.RemovePart(a.actor) + self.objects.append(a) + return self + + def add(self, obj): + """Add an object to the group.""" + self.__iadd__(obj) + return self + + def remove(self, obj): + """Remove an object to the group.""" + self.__isub__(obj) return self def _unpack(self): @@ -402,6 +423,36 @@ def unpack_group(scalarbar): self.pipeline = vedo.utils.OperationNode("add mesh", parents=[self, obj], c="#f08080") return self + def __isub__(self, obj): + """ + Remove an object to the assembly. + """ + if not vedo.utils.is_sequence(obj): + obj = [obj] + for a in obj: + if a: + try: + self.RemovePart(a) + self.objects.remove(a) + except TypeError: + self.RemovePart(a.actor) + self.objects.remove(a) + return self + + def add(self, obj): + """ + Add an object to the assembly. + """ + self.__add__(obj) + return self + + def remove(self, obj): + """ + Remove an object to the assembly. + """ + self.__isub__(obj) + return self + def __contains__(self, obj): """Allows to use `in` to check if an object is in the `Assembly`.""" return obj in self.objects diff --git a/vedo/version.py b/vedo/version.py index 80206f35..0771df0c 100644 --- a/vedo/version.py +++ b/vedo/version.py @@ -1 +1 @@ -_version = '2024.5.2+dev10' +_version = '2024.5.2+dev11'