.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_slice_example.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_plot_slice_example.py: Data Slicing Examples =============================== This example demonstrates different ways to slice data in gerg_plotting .. GENERATED FROM PYTHON SOURCE LINES 10-12 Load the sample data ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python from gerg_plotting import data_from_netcdf,ScatterPlot,Variable data = data_from_netcdf('example_data/sample_glider_data.nc' ,interp_glider=True,bounds_padding=1.5) # Show the default unsliced data plotter_data = ScatterPlot(data) plotter_data.hovmoller('temperature') plotter_data.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_slice_example_001.png :alt: plot slice example :srcset: /auto_examples/images/sphx_glr_plot_slice_example_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 23-26 Time-based slicing ---------------------------------- Get the dates and define time bounds .. GENERATED FROM PYTHON SOURCE LINES 26-38 .. code-block:: Python import numpy as np dates = data.time.values start_date = np.datetime64('2024-08-20') end_date = np.datetime64('2024-08-22') # Slice the data based on the dates data_time_sliced = data[(dates >= start_date) & (dates <= end_date)] plotter_data_time_sliced = ScatterPlot(data_time_sliced) plotter_data_time_sliced.hovmoller('temperature') plotter_data_time_sliced.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_slice_example_002.png :alt: plot slice example :srcset: /auto_examples/images/sphx_glr_plot_slice_example_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-41 Numeric variable slicing ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python data_temp_sliced = data[(data.temperature.values > 20)] plotter_data_temp_sliced = ScatterPlot(data_temp_sliced) plotter_data_temp_sliced.hovmoller('temperature') plotter_data_temp_sliced.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_slice_example_003.png :alt: plot slice example :srcset: /auto_examples/images/sphx_glr_plot_slice_example_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-49 Categorical variable slicing ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-65 .. code-block:: Python import random def generate_sample_dataset(n): categories = ["small", "medium", "large"] weights = [0.01, 0.09, 0.9] # Very skewed distribution return random.choices(categories, weights=weights, k=n) n = len(data.temperature.values) sample_data = generate_sample_dataset(n) example_var = Variable(name="string_var",values=sample_data) data.add_custom_variable(example_var) data_string_sliced = data[(data.string_var.values == 'small')] plotter_data_string_sliced = ScatterPlot(data_string_sliced) plotter_data_string_sliced.hovmoller('temperature') plotter_data_string_sliced.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_slice_example_004.png :alt: plot slice example :srcset: /auto_examples/images/sphx_glr_plot_slice_example_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.301 seconds) .. _sphx_glr_download_auto_examples_plot_slice_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_slice_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_slice_example.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_slice_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_