.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_graphs/plot_graphs_contour.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_graphs_contour.py: A quick start guide to contours =============================== .. GENERATED FROM PYTHON SOURCE LINES 5-7 .. code-block:: Python # sphinx_gallery_thumbnail_number = 6 .. GENERATED FROM PYTHON SOURCE LINES 8-9 In this example we show how to create contour graphs and how to make the most of their display settings. .. GENERATED FROM PYTHON SOURCE LINES 12-16 The `draw` method, the `Graph` and `Contour` classes ---------------------------------------------------- The simplest way to create a contour graph is to use the `draw` method of a bidimensional function. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python import openturns as ot import openturns.viewer as viewer .. GENERATED FROM PYTHON SOURCE LINES 22-23 We build a bidimensional function (function of `x` and `y`), define the study domain and the sample size .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python f = ot.SymbolicFunction(["x", "y"], ["exp(-sin(cos(y)^2 * x^2 + sin(x)^2 * y^2))"]) XMin = -5.0 XMax = 5.0 YMin = -5.0 YMax = 5.0 NX = 75 NY = 75 .. GENERATED FROM PYTHON SOURCE LINES 34-35 We build the graph by calling the `draw` method and display it .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python graph = f.draw([XMin, YMin], [XMax, YMax], [NX, NY]) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_001.png :alt: y0 as a function of (x,y) :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-42 The graph contains a unique drawable whose implementation is of class :class:`~openturns.Contour` .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python contour = graph.getDrawable(0).getImplementation() print(type(contour).__name__) .. rst-class:: sphx-glr-script-out .. code-block:: none Contour .. GENERATED FROM PYTHON SOURCE LINES 48-49 Another way to build the contour is to build the data sample and give it to the constructor of the :class:`~openturns.Contour` class .. GENERATED FROM PYTHON SOURCE LINES 51-60 .. code-block:: Python inputData = ot.Box([NX, NY]).generate() inputData *= [XMax - XMin, YMax - YMin] inputData += [XMin, YMin] data = f(inputData) x = ot.Box([NX]).generate() * [XMax - XMin] + [XMin] y = ot.Box([NY]).generate() * [YMax - YMin] + [YMin] contour = ot.Contour(x, y, data) .. GENERATED FROM PYTHON SOURCE LINES 61-62 By creating an empty graph and adding the contour we can display the whole. .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python graph = ot.Graph("Complex iso lines", "u1", "u2", True) graph.add(contour) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_002.png :alt: Complex iso lines :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-72 The previous graph does not show the associated color bar. This point can be modified. We will also change the color map, the number of contour lines and hide the labels. .. GENERATED FROM PYTHON SOURCE LINES 74-81 .. code-block:: Python contour.setColorBarPosition("right") contour.setColorMap("inferno") contour.buildDefaultLevels(5) contour.setDrawLabels(False) graph.setDrawables([ot.Drawable(contour)]) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_003.png :alt: Complex iso lines :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-84 For such a function, contour lines are not easy to interpret. We will modify the contour to use filled areas. .. GENERATED FROM PYTHON SOURCE LINES 84-89 .. code-block:: Python contour.setIsFilled(True) graph.setTitle("Complex filled contour") graph.setDrawables([ot.Drawable(contour)]) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_004.png :alt: Complex filled contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-93 Sometimes the colors are not very distinct because some levels are very close while others are very far apart. In this case, it is possible to add hatching to the surfaces. Here we will also use transparency to soften the colors. .. GENERATED FROM PYTHON SOURCE LINES 95-101 .. code-block:: Python contour.setAlpha(0.3) contour.setHatches(["/", "\\", "/\\", "+", "*"]) graph.setTitle("Complex filled contour with hatches") graph.setDrawables([ot.Drawable(contour)]) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_005.png :alt: Complex filled contour with hatches :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-105 When the function takes values very different in magnitude, it may be useful to change the norm which is used to distribute the colors and to bound the color interval. Here we will also let `Matplotlib` calculate the levels by not giving any level to the contour .. GENERATED FROM PYTHON SOURCE LINES 107-116 .. code-block:: Python contour.setColorMapNorm("log") contour.setLevels([]) contour.setExtend("neither") contour.setVmin(0.5) contour.setVmax(2) graph.setTitle("Complex contour with log norm and automatic levels") graph.setDrawables([ot.Drawable(contour)]) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_006.png :alt: Complex contour with log norm and automatic levels :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-119 These capabilities can also be leveraged for distributions. We build here 2 distributions, Funky and Punk, which we mix. .. GENERATED FROM PYTHON SOURCE LINES 121-133 .. code-block:: Python corr = ot.CorrelationMatrix(2) corr[0, 1] = 0.2 copula = ot.NormalCopula(corr) x1 = ot.Normal(-1.0, 1) x2 = ot.Normal(2, 1) x_funk = ot.JointDistribution([x1, x2], copula) x1 = ot.Normal(1.0, 1) x2 = ot.Normal(-2, 1) x_punk = ot.JointDistribution([x1, x2], copula) mixture = ot.Mixture([x_funk, x_punk], [0.5, 1.0]) .. GENERATED FROM PYTHON SOURCE LINES 134-136 The constructed graph is composed of the superposition of a filled contour and iso lines. We also changed the thickness and style of the lines to show the effect although it is not useful here. .. GENERATED FROM PYTHON SOURCE LINES 138-153 .. code-block:: Python graph = mixture.drawPDF([-5.0, -5.0], [5.0, 5.0]) # Add level lines above filled contour contour = graph.getDrawable(0).getImplementation() contour.setColor("black") contour.setColorBarPosition("") contour.setLineWidth(3) contour.setLineStyle("dotdash") graph.add(contour) # Modify previous contour to fill the graph and use log norm contour = graph.getDrawable(0).getImplementation() contour.setIsFilled(True) contour.setColorMapNorm("log") graph.setDrawable(contour, 0) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_007.png :alt: X0 iso-PDF :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 154-156 If the color bar is not sufficiently meaningful, it is possible to add the labels of the values of each level line on the drawing. Here the labels are reformatted to use scientific notation and define precision. .. GENERATED FROM PYTHON SOURCE LINES 156-166 .. code-block:: Python contour = graph.getDrawable(0).getImplementation() contour.setColorBarPosition("") # Hide color bar graph.setDrawable(contour, 0) contour = graph.getDrawable(1).getImplementation() contour.setDrawLabels(True) contour.setLabels(["{:.3g}".format(level) for level in contour.getLevels()]) graph.setDrawable(contour, 1) view = viewer.View(graph) viewer.View.ShowAll() .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_contour_008.png :alt: X0 iso-PDF :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_008.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_graphs_plot_graphs_contour.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_graphs_contour.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_graphs_contour.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_graphs_contour.zip `