.. 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-6 .. code-block:: Python # sphinx_gallery_thumbnail_number = 6 .. GENERATED FROM PYTHON SOURCE LINES 7-8 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 11-15 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 17-20 .. code-block:: Python import openturns as ot import openturns.viewer as viewer .. GENERATED FROM PYTHON SOURCE LINES 21-22 We build a bidimensional function (function of x and y), define the study domain and the sample size .. GENERATED FROM PYTHON SOURCE LINES 24-32 .. 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 33-34 We build the graph by calling the `draw` method and display it .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python graph = f.draw([YMin, 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 40-41 The graph contains an unique drawable whose implementation is of class `Contour` .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. 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 47-48 Another way to build the contour is to build the data sample and give it to the constructor of the `Contour` class .. GENERATED FROM PYTHON SOURCE LINES 50-59 .. 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 60-61 By creating an empty graph and adding the contour we can display the whole. .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. 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 68-71 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 73-80 .. 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 81-83 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 83-88 .. 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 89-92 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 94-100 .. 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 101-104 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 106-115 .. 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 116-118 These capabilities can also be leveraged for distributions. We build here 2 distributions, Funky and Punk, which we mix. .. GENERATED FROM PYTHON SOURCE LINES 120-132 .. 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.ComposedDistribution([x1, x2], copula) x1 = ot.Normal(1.0, 1) x2 = ot.Normal(-2, 1) x_punk = ot.ComposedDistribution([x1, x2], copula) mixture = ot.Mixture([x_funk, x_punk], [0.5, 1.0]) .. GENERATED FROM PYTHON SOURCE LINES 133-135 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 137-152 .. 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,X1] iso-PDF :srcset: /auto_graphs/images/sphx_glr_plot_graphs_contour_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 153-155 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 155-165 .. 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,X1] 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 `