.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/plot_customizations.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_tutorials_plot_customizations.py: Plot customizations =================== This example shows how to include some advanced options in the spatial visualization of Gridded Forecasts and Evaluation Results Overview: 1. Define optional plotting arguments 2. Set extent of maps 3. Visualizing selected magnitude bins 4. Plot global maps 5. Plot multiple Evaluation Results .. GENERATED FROM PYTHON SOURCE LINES 18-20 Example 1: Spatial dataset plot arguments ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-23 **Load required libraries** .. GENERATED FROM PYTHON SOURCE LINES 23-31 .. code-block:: Python import csep import cartopy import numpy from csep.utils import datasets, plots import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 32-34 **Load a Grid Forecast from the datasets** .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python forecast = csep.load_gridded_forecast(datasets.hires_ssm_italy_fname, name='Werner, et al (2010) Italy') .. GENERATED FROM PYTHON SOURCE LINES 37-40 **Selecting plotting arguments** Create a dictionary containing the plot arguments .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: Python args_dict = {'title': 'Italy 10 year forecast', 'grid_labels': True, 'borders': True, 'feature_lw': 0.5, 'basemap': 'ESRI_imagery', 'cmap': 'rainbow', 'alpha_exp': 0.8, 'projection': cartopy.crs.Mercator()} .. GENERATED FROM PYTHON SOURCE LINES 49-61 These arguments are, in order: * Assign a title * Set labels to the geographic axes * Draw country borders * Set a linewidth of 0.5 to country borders * Select ESRI Imagery as a basemap. * Assign ``'rainbow'`` as colormap. Possible values from from ``matplotlib.cm`` library * Defines 0.8 for an exponential transparency function (default is 0 for constant alpha, whereas 1 for linear). * An object cartopy.crs.Projection() is passed as Projection to the map The complete description of plot arguments can be found in :func:`csep.utils.plots.plot_spatial_dataset` .. GENERATED FROM PYTHON SOURCE LINES 63-66 **Plotting the dataset** The map `extent` can be defined. Otherwise, the extent of the data would be used. The dictionary defined must be passed as argument .. GENERATED FROM PYTHON SOURCE LINES 66-71 .. code-block:: Python ax = forecast.plot(extent=[3, 22, 35, 48], show=True, plot_args=args_dict) .. image-sg:: /tutorials/images/sphx_glr_plot_customizations_001.png :alt: Italy 10 year forecast :srcset: /tutorials/images/sphx_glr_plot_customizations_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-79 Example 2: Plot a global forecast and a selected magnitude bin range -------------------------------------------------------------------- **Load a Global Forecast from the datasets** A downsampled version of the `GEAR1 `_ forecast can be found in datasets. .. GENERATED FROM PYTHON SOURCE LINES 79-83 .. code-block:: Python forecast = csep.load_gridded_forecast(datasets.gear1_downsampled_fname, name='GEAR1 Forecast (downsampled)') .. GENERATED FROM PYTHON SOURCE LINES 84-87 **Filter by magnitudes** We get the rate of events of 5.95<=M_w<=7.5 .. GENERATED FROM PYTHON SOURCE LINES 87-94 .. code-block:: Python low_bound = 6.15 upper_bound = 7.55 mw_bins = forecast.get_magnitudes() mw_ind = numpy.where(numpy.logical_and( mw_bins >= low_bound, mw_bins <= upper_bound))[0] rates_mw = forecast.data[:, mw_ind] .. GENERATED FROM PYTHON SOURCE LINES 95-96 We get the total rate between these magnitudes .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: Python rate_sum = rates_mw.sum(axis=1) .. GENERATED FROM PYTHON SOURCE LINES 100-101 The data is stored in a 1D array, so it should be projected into `region` 2D cartesian grid. .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python rate_sum = forecast.region.get_cartesian(rate_sum) .. GENERATED FROM PYTHON SOURCE LINES 105-108 **Define plot arguments** We define the arguments and a global projection, centered at $lon=-180$ .. GENERATED FROM PYTHON SOURCE LINES 108-117 .. code-block:: Python plot_args = {'figsize': (10,6), 'coastline':True, 'feature_color':'black', 'projection': cartopy.crs.Robinson(central_longitude=180.0), 'title': forecast.name, 'grid_labels': False, 'cmap': 'magma', 'clabel': r'$\log_{10}\lambda\left(M_w \in [{%.2f},\,{%.2f}]\right)$ per ' r'${%.1f}^\circ\times {%.1f}^\circ $ per forecast period' % (low_bound, upper_bound, forecast.region.dh, forecast.region.dh)} .. GENERATED FROM PYTHON SOURCE LINES 118-121 **Plotting the dataset** To plot a global forecast, we must assign the option ``set_global=True``, which is required by :ref:cartopy to handle internally the extent of the plot .. GENERATED FROM PYTHON SOURCE LINES 121-126 .. code-block:: Python ax = plots.plot_spatial_dataset(numpy.log10(rate_sum), forecast.region, show=True, set_global=True, plot_args=plot_args) .. image-sg:: /tutorials/images/sphx_glr_plot_customizations_002.png :alt: GEAR1 Forecast (downsampled) :srcset: /tutorials/images/sphx_glr_plot_customizations_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 131-133 Example 3: Plot a catalog ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 135-136 **Load a Catalog from ComCat** .. GENERATED FROM PYTHON SOURCE LINES 136-152 .. code-block:: Python start_time = csep.utils.time_utils.strptime_to_utc_datetime('1995-01-01 00:00:00.0') end_time = csep.utils.time_utils.strptime_to_utc_datetime('2015-01-01 00:00:00.0') min_mag = 3.95 catalog = csep.query_comcat(start_time, end_time, min_magnitude=min_mag, verbose=False) # **Define plotting arguments** plot_args = {'basemap': 'ESRI_terrain', 'markersize': 2, 'markercolor': 'red', 'alpha': 0.3, 'mag_scale': 7, 'legend': True, 'legend_loc': 3, 'mag_ticks': [4.0, 5.0, 6.0, 7.0]} .. rst-class:: sphx-glr-script-out .. code-block:: none Fetched ComCat catalog in 22.360023736953735 seconds. .. GENERATED FROM PYTHON SOURCE LINES 153-163 These arguments are, in order: * Assign as basemap the ESRI_terrain webservice * Set minimum markersize of 2 with red color * Set a 0.3 transparency * mag_scale is used to exponentially scale the size with respect to magnitude. Recommended 1-8 * Set legend True and location in 3 (lower-left corner) * Set a list of Magnitude ticks to display in the legend The complete description of plot arguments can be found in :func:`csep.utils.plots.plot_catalog` .. GENERATED FROM PYTHON SOURCE LINES 165-170 .. code-block:: Python # **Plot the catalog** ax = catalog.plot(show=False, plot_args=plot_args) .. image-sg:: /tutorials/images/sphx_glr_plot_customizations_003.png :alt: plot customizations :srcset: /tutorials/images/sphx_glr_plot_customizations_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 171-173 Example 4: Plot multiple evaluation results ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 175-177 Load L-test results from example .json files (See :doc:`gridded_forecast_evaluation` for information on calculating and storing evaluation results) .. GENERATED FROM PYTHON SOURCE LINES 177-190 .. code-block:: Python L_results = [csep.load_evaluation_result(i) for i in datasets.l_test_examples] args = {'figsize': (6,5), 'title': r'$\mathcal{L}-\mathrm{test}$', 'title_fontsize': 18, 'xlabel': 'Log-likelihood', 'xticks_fontsize': 9, 'ylabel_fontsize': 9, 'linewidth': 0.8, 'capsize': 3, 'hbars':True, 'tight_layout': True} .. GENERATED FROM PYTHON SOURCE LINES 191-194 Description of plot arguments can be found in :func:`plot_poisson_consistency_test`. We set ``one_sided_lower=True`` as usual for an L-test, where the model is rejected if the observed is located within the lower tail of the simulated distribution. .. GENERATED FROM PYTHON SOURCE LINES 194-200 .. code-block:: Python ax = plots.plot_poisson_consistency_test(L_results, one_sided_lower=True, plot_args=args) # Needed to show plots if running as script plt.show() .. image-sg:: /tutorials/images/sphx_glr_plot_customizations_004.png :alt: $\mathcal{L}-\mathrm{test}$ :srcset: /tutorials/images/sphx_glr_plot_customizations_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 49.988 seconds) .. _sphx_glr_download_tutorials_plot_customizations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_customizations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_customizations.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_