.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/vector/plot_1_vector.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_auto_examples_vector_plot_1_vector.py: Vector, 2D{2} dataset ^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 6-10 The 2D{2} datasets are two-dimensional, :math:`d=2`, with one two-component dependent variable, :math:`p=2`. The following is an example of a simulated electric field vector dataset of a dipole as a function of two linearly sampled spatial dimensions. .. GENERATED FROM PYTHON SOURCE LINES 10-18 .. code-block:: Python import csdmpy as cp domain = "https://www.ssnmr.org/sites/default/files/CSDM" filename = f"{domain}/vector/electric_field/electric_field_base64.csdf" vector_data = cp.load(filename) print(vector_data.data_structure) .. rst-class:: sphx-glr-script-out .. code-block:: none { "csdm": { "version": "1.0", "read_only": true, "timestamp": "2014-09-30T11:16:33Z", "description": "A simulated electric field dataset from an electric dipole.", "dimensions": [ { "type": "linear", "count": 64, "increment": "0.0625 cm", "coordinates_offset": "-2.0 cm", "quantity_name": "length", "label": "x", "reciprocal": { "quantity_name": "wavenumber" } }, { "type": "linear", "count": 64, "increment": "0.0625 cm", "coordinates_offset": "-2.0 cm", "quantity_name": "length", "label": "y", "reciprocal": { "quantity_name": "wavenumber" } } ], "dependent_variables": [ { "type": "internal", "name": "Electric field lines", "unit": "C^-1 * N", "quantity_name": "electric field strength", "numeric_type": "float32", "quantity_type": "vector_2", "components": [ [ "3.7466873e-07, 3.3365018e-07, ..., 3.5343004e-07, 4.0100363e-07" ], [ "1.6129676e-06, 1.6765767e-06, ..., 1.846712e-06, 1.7754871e-06" ] ] } ] } } .. GENERATED FROM PYTHON SOURCE LINES 19-21 The tuple of the dimension and dependent variable instances from this example are .. GENERATED FROM PYTHON SOURCE LINES 21-24 .. code-block:: Python x = vector_data.dimensions y = vector_data.dependent_variables .. GENERATED FROM PYTHON SOURCE LINES 25-26 with the respective coordinates (viewed only up to five values), as .. GENERATED FROM PYTHON SOURCE LINES 26-28 .. code-block:: Python print(x[0].coordinates[:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none [-2. -1.9375 -1.875 -1.8125 -1.75 ] cm .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python print(x[1].coordinates[:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none [-2. -1.9375 -1.875 -1.8125 -1.75 ] cm .. GENERATED FROM PYTHON SOURCE LINES 32-35 The components of the dependent variable are vector components as seen from the :attr:`~csdmpy.DependentVariable.quantity_type` attribute of the corresponding dependent variable instance. .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python print(y[0].quantity_type) .. rst-class:: sphx-glr-script-out .. code-block:: none vector_2 .. GENERATED FROM PYTHON SOURCE LINES 38-43 **Visualizing the dataset** Let's visualize the vector data using the *streamplot* method from the matplotlib package. Before we could visualize, however, there is an initial processing step. We use the Numpy library for processing. .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python import numpy as np X, Y = np.meshgrid(x[0].coordinates, x[1].coordinates) # (x, y) coordinate pairs U, V = y[0].components[0], y[0].components[1] # U and V are the components R = np.sqrt(U**2 + V**2) # The magnitude of the vector R /= R.min() # Scaled magnitude of the vector Rlog = np.log10(R) # Scaled magnitude of the vector on a log scale .. GENERATED FROM PYTHON SOURCE LINES 52-56 In the above steps, we calculate the X-Y grid points along with a scaled magnitude of the vector dataset. The magnitude is scaled such that the minimum value is one. Next, calculate the log of the scaled magnitude to visualize the intensity on a logarithmic scale. .. GENERATED FROM PYTHON SOURCE LINES 58-59 And now, the streamplot vector plot .. GENERATED FROM PYTHON SOURCE LINES 59-78 .. code-block:: Python import matplotlib.pyplot as plt plt.streamplot( X.value, Y.value, U, V, density=1, linewidth=Rlog, color=Rlog, cmap="viridis" ) plt.xlim([x[0].coordinates[0].value, x[0].coordinates[-1].value]) plt.ylim([x[1].coordinates[0].value, x[1].coordinates[-1].value]) # Set axes labels and figure title. plt.xlabel(x[0].axis_label) plt.ylabel(x[1].axis_label) plt.title(y[0].name) # Set grid lines. plt.grid(color="gray", linestyle="--", linewidth=0.5) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/vector/images/sphx_glr_plot_1_vector_001.png :alt: Electric field lines :srcset: /auto_examples/vector/images/sphx_glr_plot_1_vector_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.162 seconds) .. _sphx_glr_download_auto_examples_vector_plot_1_vector.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1_vector.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1_vector.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_