.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_graphs/plot_combine_openturns_matplotlib.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_graphs_plot_combine_openturns_matplotlib.py: Combine native graphics with `matplotlib` ========================================= This example shows how to combine native graphs with `matplotlib`. Every :class:`~openturns.Graph` displayed with :class:`~openturns.viewer.View` is a `matplotlib` figure. Hence we can: - retrieve the `matplotlib` ``Figure`` and ``Axes`` from a ``View`` and enrich them with `matplotlib`, - draw one or several native graphs into an existing `matplotlib` figure. The example uses a standard Normal distribution: its PDF is drawn using the library, the failure threshold, the shaded tail and the annotations are added with `matplotlib`. .. GENERATED FROM PYTHON SOURCE LINES 19-26 Create a native graph and retrieve the `matplotlib` objects ----------------------------------------------------------- We start from a basic native graph: the density of a Normal distribution. Every graph displayed with :class:`~openturns.viewer.View` is a `matplotlib` :meth:`~openturns.viewer.View.getFigure` and :meth:`~openturns.viewer.View.getAxes` give access to the underlying `matplotlib` objects. Any `matplotlib` customization then applies. .. GENERATED FROM PYTHON SOURCE LINES 26-45 .. code-block:: Python import openturns as ot import openturns.viewer as otv import matplotlib.pyplot as plt ot.RandomGenerator.SetSeed(0) normal = ot.Normal() graph_pdf = normal.drawPDF() graph_pdf.setTitle("Standard Normal PDF") graph_pdf.setLegends(["Density of X"]) graph_pdf.setXTitle("X") view = otv.View(graph_pdf) fig = view.getFigure() ax = view.getAxes()[0] ax.grid(True, alpha=0.3) ax.set_xlabel("X") fig.suptitle("A native graph is a Matplotlib figure", fontsize=10) .. image-sg:: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_001.svg :alt: A native graph is a Matplotlib figure :srcset: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_001.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 0.98, 'A native graph is a Matplotlib figure') .. GENERATED FROM PYTHON SOURCE LINES 46-52 Customize graphs with `matplotlib` ---------------------------------- Let us highlight the event ``X > 2``. The curve itself was drawn with the library: we reuse its line data to shade the tail exactly under the curve, then add a threshold line and a text. .. GENERATED FROM PYTHON SOURCE LINES 52-79 .. code-block:: Python threshold = 2.0 prob = 1.0 - normal.computeCDF(threshold) # Fresh view to keep the previous figure simple. view = otv.View(graph_pdf) ax = view.getAxes()[0] # Data of the PDF curve drawn with the library. line = ax.get_lines()[0] x, y = line.get_xdata(), line.get_ydata() mask = x >= threshold ax.fill_between(x[mask], y[mask], alpha=0.3, label=f"P(X > 2) = {prob:.3f}") ax.axvline(threshold, color="red", linestyle="--", label="threshold s = 2") ax.text( threshold + 0.1, 0.3, f"Failure probability\n{prob:.4f}", color="red", fontsize=9, ) # sphinx_gallery_thumbnail_number = 2 # The legend must be redrawn after adding `matplotlib` artists. ax.legend() ax.grid(True, alpha=0.3) .. image-sg:: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_002.svg :alt: Standard Normal PDF :srcset: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-83 Apply a `matplotlib` transform ------------------------------ Any Axes method applies, e.g. a manual rescaling to inspect the upper tail. .. GENERATED FROM PYTHON SOURCE LINES 83-90 .. code-block:: Python view_scale = otv.View(graph_pdf, figure_kw={"figsize": (6, 4)}) new_scale = view_scale.getAxes()[0] new_scale.set_xlim([3.0, 3.8]) new_scale.set_ylim([0.0, 0.005]) new_scale.grid(True, which="both", alpha=0.3) new_scale.set_xlabel("X") .. image-sg:: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_003.svg :alt: Standard Normal PDF :srcset: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_003.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 10.679999999999996, 'X') .. GENERATED FROM PYTHON SOURCE LINES 91-99 Draw native graphs into an existing `matplotlib` figure ------------------------------------------------------- The reverse direction is also possible: create the `matplotlib` figure first with :func:`~matplotlib.pyplot.subplots`, then draw each native graph on a chosen ``Axes`` with the ``figure`` and ``axes`` arguments. Extra `matplotlib`-only elements can then be mixed freely. Function `subplots_adjust` of `matplotlib` can be used to separate the graphs within the grid layout. .. GENERATED FROM PYTHON SOURCE LINES 99-128 .. code-block:: Python graph_cdf = normal.drawCDF() graph_cdf.setLegends(["CDF of X"]) graph_cdf.setXTitle("X") graph_logpdf = normal.drawLogPDF() graph_logpdf.setLegends(["LogPDF of X"]) graph_logpdf.setXTitle("X") graph_survival = normal.drawSurvivalFunction() graph_survival.setLegends(["Survival function of X"]) graph_survival.setXTitle("X") fig, axs = plt.subplots(2, 2, figsize=(12, 8)) _ = otv.View(graph_pdf, figure=fig, axes=[axs[0, 0]]) _ = otv.View(graph_cdf, figure=fig, axes=[axs[0, 1]]) _ = otv.View(graph_logpdf, figure=fig, axes=[axs[1, 0]]) _ = otv.View(graph_survival, figure=fig, axes=[axs[1, 1]]) # Pure `matplotlib` additions: median point and decorations. axs[0, 1].axhline(0.5, color="black", linestyle=":", label="median level") axs[0, 1].plot([0.0], [0.5], "ro", label="median") axs[0, 1].legend() fig.suptitle("Four native graphs embedded in a Matplotlib figure") fig.subplots_adjust( left=0.07, right=0.95, bottom=0.08, top=0.90, wspace=0.3, hspace=0.5 ) otv.View.ShowAll() .. image-sg:: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_004.svg :alt: Four native graphs embedded in a Matplotlib figure :srcset: /auto_graphs/images/sphx_glr_plot_combine_openturns_matplotlib_004.svg :class: sphx-glr-single-img .. _sphx_glr_download_auto_graphs_plot_combine_openturns_matplotlib.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_combine_openturns_matplotlib.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_combine_openturns_matplotlib.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_combine_openturns_matplotlib.zip `