.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/2D_1_examples/plot_3_labeled.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_2D_1_examples_plot_3_labeled.py: Labeled Dataset ^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 6-12 The CSD model also supports labeled dimensions. In the following example, we present a mixed `linear` and `labeled` two-dimensional dataset representing the population of the country as a function of time. The dataset is obtained from `The World Bank `_. Import the `csdmpy` model and load the dataset. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import csdmpy as cp filename = "https://www.ssnmr.org/sites/default/files/CSDM/labeled/population.csdf" labeled_data = cp.load(filename) .. GENERATED FROM PYTHON SOURCE LINES 18-20 The tuple of dimension and dependent variable objects from ``labeled_data`` instance are .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python x = labeled_data.dimensions y = labeled_data.dependent_variables .. GENERATED FROM PYTHON SOURCE LINES 24-27 Since one of the dimensions is a `labeled` dimension, let's make use of the :attr:`~csdmpy.Dimension.type` attribute of the dimension instances to find out which dimension is `labeled`. .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. code-block:: Python print(x[0].type) .. rst-class:: sphx-glr-script-out .. code-block:: none linear .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python print(x[1].type) .. rst-class:: sphx-glr-script-out .. code-block:: none labeled .. GENERATED FROM PYTHON SOURCE LINES 33-34 Here, the second dimension is the `labeled` dimension with [#f1]_ .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python print(x[1].count) .. rst-class:: sphx-glr-script-out .. code-block:: none 263 .. GENERATED FROM PYTHON SOURCE LINES 37-38 labels, where the first five labels are .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python print(x[1].labels[:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Aruba' 'Afghanistan' 'Angola' 'Albania' 'Andorra'] .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. note:: For labeled dimensions, the :attr:`~csdmpy.Dimension.coordinates` attribute is an alias of the :attr:`~csdmpy.Dimension.labels` attribute. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python print(x[1].coordinates[:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Aruba' 'Afghanistan' 'Angola' 'Albania' 'Andorra'] .. GENERATED FROM PYTHON SOURCE LINES 48-50 The coordinates along the first dimension, viewed up to the first ten points, are .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python print(x[0].coordinates[:10]) .. rst-class:: sphx-glr-script-out .. code-block:: none [1960. 1961. 1962. 1963. 1964. 1965. 1966. 1967. 1968. 1969.] yr .. GENERATED FROM PYTHON SOURCE LINES 53-59 **Plotting the dataset** You may plot this dataset however you like. Here, we use a bar graph to represent the population of countries in the year 2017. The data corresponding to this year is a cross-section of the dependent variable at index 57 along the ``x[0]`` dimension. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python print(x[0].coordinates[57]) .. rst-class:: sphx-glr-script-out .. code-block:: none 2017.0 yr .. GENERATED FROM PYTHON SOURCE LINES 62-64 To keep the plot simple, we only plot the first 20 country labels along the ``x[1]`` dimension. .. GENERATED FROM PYTHON SOURCE LINES 64-79 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np x_data = x[1].coordinates[:20] x_pos = np.arange(20) y_data = y[0].components[0][:20, 57] plt.bar(x_data, y_data, align="center", alpha=0.5) plt.xticks(x_pos, x_data, rotation=90) plt.ylabel(y[0].axis_label[0]) plt.yscale("log") plt.title(y[0].name) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/2D_1_examples/images/sphx_glr_plot_3_labeled_001.png :alt: plot 3 labeled :srcset: /auto_examples/2D_1_examples/images/sphx_glr_plot_3_labeled_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-87 .. rubric:: Footnotes .. [#f1] In the CSD model, the attribute count is only valid for the :ref:`linearDimension_uml`. In `csdmpy`, however, the :attr:`~csdmpy.Dimension.count` attribute is valid for all dimension objects and returns an integer with the number of grid points along the dimension. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.762 seconds) .. _sphx_glr_download_auto_examples_2D_1_examples_plot_3_labeled.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_3_labeled.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_3_labeled.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_