.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated_examples/multipanel_plots.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_generated_examples_multipanel_plots.py: Creating Multi-panel plots with hermpy ====================================== This example demonstrates how to construct multi-panel time series plots using ``hermpy.plotting.MultiPanel``. We fetch one hour of MESSENGER MAG and FIPS data, parse them into their respective data structures, and progressively build up a combined plot. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: Python import xarray as xr from astropy.table import QTable from sunpy.time import TimeRange from hermpy.data import parse_messenger_fips, parse_messenger_mag from hermpy.net import ClientMESSENGER from hermpy.plotting import MultiPanel, SpectrogramPanel, TimeseriesPanel .. GENERATED FROM PYTHON SOURCE LINES 20-27 First we must get some data. Here we fetch MESSENGER MAG and FIPS data for a particular time range. We use ``hermpy.net.ClientMESSENGER`` to download MAG and FIPS data for a one-hour window. See `here`_ for more details. .. _here: download_data.html .. GENERATED FROM PYTHON SOURCE LINES 27-36 .. code-block:: Python c = ClientMESSENGER() time_range = TimeRange("2011-06-01T00:00", "2011-06-01T01:00") c.query(time_range, "MAG") mag_file_paths = c.fetch() c.query(time_range, "FIPS") fips_file_paths = c.fetch() .. GENERATED FROM PYTHON SOURCE LINES 37-40 We use Astropy's Quantity Table (QTable) for time series data, and Xarray's Dataset for 2D data. These custom parsers are included in ``hermpy.data``, but may be replaced in later versions. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python mag_data: QTable = parse_messenger_mag(mag_file_paths, time_range) fips_data: xr.Dataset = parse_messenger_fips(fips_file_paths, time_range) .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python print(mag_data) .. rst-class:: sphx-glr-script-out .. code-block:: none UTC X MSO Y MSO Z MSO Bx By Bz km km km nT nT nT --------------------- -------- -------- --------- ------ ------ ------ 2011:152:00:00:27.153 4154.624 -736.386 -1295.122 0.184 27.34 64.725 2011:152:00:00:27.653 4155.009 -736.31 -1296.464 0.876 24.996 57.539 2011:152:00:00:28.153 4155.393 -736.234 -1297.805 9.611 28.089 55.024 2011:152:00:00:28.653 4155.778 -736.157 -1299.147 5.836 29.004 55.204 2011:152:00:00:29.153 4156.162 -736.081 -1300.488 7.116 31.518 50.639 2011:152:00:00:29.653 4156.546 -736.004 -1301.829 12.861 25.287 68.678 2011:152:00:00:30.153 4156.929 -735.928 -1303.171 10.287 23.3 61.738 2011:152:00:00:30.653 4157.312 -735.851 -1304.512 16.729 22.473 60.572 2011:152:00:00:31.153 4157.695 -735.774 -1305.853 9.353 22.904 55.89 ... ... ... ... ... ... ... 2011:152:00:59:55.203 3658.837 224.82 -8520.415 11.093 13.405 16.675 2011:152:00:59:55.703 3658.56 224.968 -8521.155 11.184 13.404 16.696 2011:152:00:59:56.203 3658.282 225.116 -8521.894 11.185 13.404 16.696 2011:152:00:59:56.703 3658.005 225.263 -8522.634 11.074 13.363 16.772 2011:152:00:59:57.203 3657.727 225.411 -8523.373 11.14 13.356 16.689 2011:152:00:59:57.703 3657.45 225.559 -8524.112 11.22 13.265 16.761 2011:152:00:59:58.203 3657.172 225.706 -8524.852 11.106 13.13 16.843 2011:152:00:59:58.703 3656.895 225.854 -8525.591 11.097 13.133 16.89 2011:152:00:59:59.203 3656.617 226.002 -8526.33 11.222 13.263 16.761 2011:152:00:59:59.703 3656.339 226.149 -8527.069 11.179 13.356 16.746 Length = 7146 rows .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python print(fips_data) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 245kB Dimensions: (UTC: 199, Energy Channel: 63, Mode: 5317) Coordinates: * UTC (UTC) datetime64[us] 2kB 2011-06-01T00:23:49.739000 ... ... * Energy Channel (Energy Channel) int64 504B 0 1 2 3 4 5 ... 58 59 60 61 62 * Mode (Mode) int64 43kB 0 0 0 0 0 0 0 0 0 0 ... 2 2 2 2 2 2 2 2 2 Data variables: Proton Flux (UTC, Energy Channel) float64 100kB 0.0 0.0 0.0 ... 0.0 0.0 Non-Proton Flux (UTC, Energy Channel) float64 100kB 0.0 0.0 ... 0.0 0.0 .. GENERATED FROM PYTHON SOURCE LINES 50-57 As MESSENGER MAG data contains both ephemeris and magnetic field data, adding this data as it is to a Panel object will result in an error, as the columns (excluding the time column) have multiple units, and hence can't be represented on the same axis. We therefore need to shorten this table to just the columns we want in this Panel object. .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python mag_data.keep_columns(["UTC", "Bx", "By", "Bz"]) .. GENERATED FROM PYTHON SOURCE LINES 60-69 ``hermpy`` provides the ``Panel`` abstract base class, which is expanded upon in ``TimeseriesPanel`` and ``SpectrogramPanel``. We use a ``TimeseriesPanel`` as this matches the data type. Each panel has a built-in plot function which returns a ``Figure`` and ``Axes`` for that panel in isolation. ``plt.show()`` is called within this function however uou can defer rendering until later by setting ``.plot(show=False)``, and then using ``plt.show()`` at a later point. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: Python mag_panel = TimeseriesPanel(mag_data) fig, ax = mag_panel.plot() .. image-sg:: /generated_examples/images/sphx_glr_multipanel_plots_001.png :alt: multipanel plots :srcset: /generated_examples/images/sphx_glr_multipanel_plots_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-81 Plotting with more than one panel --------------------------------- However, the power in Panel objects comes when we go to construct multipanel plots. We can combine Panel objects to construct a multi-panel plot by simply adding them. Lets make a second panel where we plot the FIPS data. We can combine the two using the addition operator. .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: Python proton_flux = fips_data["Proton Flux"] fips_panel = SpectrogramPanel(proton_flux) multipanel: MultiPanel = mag_panel + fips_panel fig, axes = multipanel.plot() .. image-sg:: /generated_examples/images/sphx_glr_multipanel_plots_002.png :alt: multipanel plots :srcset: /generated_examples/images/sphx_glr_multipanel_plots_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-90 You can also add multipanels to the same effect, and panels can also be added to multipanel objects. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python multipanel += multipanel multipanel.plot() .. image-sg:: /generated_examples/images/sphx_glr_multipanel_plots_003.png :alt: multipanel plots :srcset: /generated_examples/images/sphx_glr_multipanel_plots_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, array([, , , ], dtype=object)) .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: Python multipanel += mag_panel multipanel.plot() .. image-sg:: /generated_examples/images/sphx_glr_multipanel_plots_004.png :alt: multipanel plots :srcset: /generated_examples/images/sphx_glr_multipanel_plots_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, array([, , , , ], dtype=object)) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.864 seconds) .. _sphx_glr_download_generated_examples_multipanel_plots.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: multipanel_plots.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: multipanel_plots.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: multipanel_plots.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_