.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` 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
^^^^^^^^^^^^^^^
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.
.. code-block:: default
import csdmpy as cp
filename = "https://osu.box.com/shared/static/e81to3izj5yv5m7mjq9xw7gmqez2blto.csdf"
labeled_data = cp.load(filename)
The tuple of dimension and dependent variable objects from ``labeled_data`` instance
are
.. code-block:: default
x = labeled_data.dimensions
y = labeled_data.dependent_variables
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`.
.. code-block:: default
x[0].type
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
'linear'
.. code-block:: default
x[1].type
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
'labeled'
Here, the second dimension is the `labeled` dimension with [#f1]_
.. code-block:: default
x[1].count
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
263
labels, where the first five labels are
.. code-block:: default
print(x[1].labels[:5])
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
['Aruba' 'Afghanistan' 'Angola' 'Albania' 'Andorra']
.. note::
For labeled dimensions, the :attr:`~csdmpy.Dimension.coordinates`
attribute is an alias of the :attr:`~csdmpy.Dimension.labels`
attribute.
.. code-block:: default
print(x[1].coordinates[:5])
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
['Aruba' 'Afghanistan' 'Angola' 'Albania' 'Andorra']
The coordinates along the first dimension, viewed up to the first ten
points, are
.. code-block:: default
print(x[0].coordinates[:10])
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[1960. 1961. 1962. 1963. 1964. 1965. 1966. 1967. 1968. 1969.] yr
**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.
.. code-block:: default
print(x[0].coordinates[57])
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
2017.0 yr
To keep the plot simple, we only plot the first 20 country labels along
the ``x[1]`` dimension.
.. code-block:: default
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:: /auto_examples/2D_1_examples/images/sphx_glr_plot_3_labeled_001.png
:alt: plot 3 labeled
:class: sphx-glr-single-img
.. 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 1.352 seconds)
.. _sphx_glr_download_auto_examples_2D_1_examples_plot_3_labeled.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_3_labeled.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_3_labeled.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_