.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "base_examples/plot_baseclass1.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_base_examples_plot_baseclass1.py: Subclassing BaseObj - Simple Pendulum ===================================== This example shows how to subclass :class:`easyCore.Objects.Base.BaseObj` with parameters from :class:`easyCore.Objects.Base.Parameter`. For this example a simple pendulum will be modeled. .. math:: y = A \sin (2 \pi f t + \phi ) Imports ******* Firstly the necessary imports. Notice that we import numpy from easyCore. This is not done for any reason other than saving time from multiple imports. .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from easyCore.Objects.ObjectClasses import BaseObj from easyCore.Objects.ObjectClasses import Parameter .. GENERATED FROM PYTHON SOURCE LINES 24-30 Subclassing *********** To include embedded rST, use a line of >= 20 ``#``'s or ``#%%`` between your rST and your code. This separates your example into distinct text and code blocks. You can continue writing code below the embedded rST text block: .. GENERATED FROM PYTHON SOURCE LINES 30-55 .. code-block:: Python class Pendulum(BaseObj): def __init__(self, A: Parameter, f: Parameter, p: Parameter): super(Pendulum, self).__init__('SimplePendulum', A=A, f=f, p=p) @classmethod def from_pars(cls, A: float = 1, f: float = 1, p: float = 0): A = Parameter('Amplitude', A) f = Parameter('Frequency', f) p = Parameter('Phase', p) return cls(A, f, p) def __call__(self, t): return self.A.raw_value * np.sin(2 * np.pi * self.f.raw_value * t + self.p.raw_value) def plot(self, time, axis=None, **kwargs): if axis is None: axis = plt else: axis.set_title(f'A={self.A.raw_value}, F={self.f.raw_value}, P={self.p.raw_value}') p = axis.plot(time, self(time), **kwargs) return p .. GENERATED FROM PYTHON SOURCE LINES 56-62 Single Example ************** To include embedded rST, use a line of >= 20 ``#``'s or ``#%%`` between your rST and your code. This separates your example into distinct text and code blocks. You can continue writing code below the embedded rST text block: .. GENERATED FROM PYTHON SOURCE LINES 62-71 .. code-block:: Python p1 = Pendulum.from_pars() # Another pendulum with Amplitude = 5 p2 = Pendulum.from_pars(A=5) # Another pendulum with Frequency = 4 p3 = Pendulum.from_pars(A=5, f=4) # Another pendulum with Phase = pi/2 p4 = Pendulum.from_pars(A=5, f=4, p=np.pi / 2) .. GENERATED FROM PYTHON SOURCE LINES 72-73 Plotting .. GENERATED FROM PYTHON SOURCE LINES 73-84 .. code-block:: Python t = np.linspace(0, 3, 601) fig = plt.figure() gs = fig.add_gridspec(2, 2) (ax1, ax2), (ax3, ax4) = gs.subplots(sharex='col', sharey='row') p1.plot(t, axis=ax1) p2.plot(t, axis=ax2) p3.plot(t, axis=ax3) p4.plot(t, axis=ax4) fig.show() .. image-sg:: /base_examples/images/sphx_glr_plot_baseclass1_001.png :alt: A=1.0, F=1.0, P=0.0, A=5.0, F=1.0, P=0.0, A=5.0, F=4.0, P=0.0, A=5.0, F=4.0, P=1.5707963267948966 :srcset: /base_examples/images/sphx_glr_plot_baseclass1_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-91 Multiple Examples ***************** To include embedded rST, use a line of >= 20 ``#``'s or ``#%%`` between your rST and your code. This separates your example into distinct text and code blocks. You can continue writing code below the embedded rST text block: .. GENERATED FROM PYTHON SOURCE LINES 91-98 .. code-block:: Python pendulum_array = [Pendulum.from_pars(p=phase) for phase in np.linspace(0, 1, 3)] fig = plt.figure() for pendulum in pendulum_array: pendulum.plot(t, label=f'Phase = {pendulum.p}') plt.legend(loc='lower right') fig.show() .. image-sg:: /base_examples/images/sphx_glr_plot_baseclass1_002.png :alt: plot baseclass1 :srcset: /base_examples/images/sphx_glr_plot_baseclass1_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.274 seconds) .. _sphx_glr_download_base_examples_plot_baseclass1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_baseclass1.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_baseclass1.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_