.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_graphs/plot_graphs_fill_area.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_fill_area.py: How to fill an area =================== .. GENERATED FROM PYTHON SOURCE LINES 5-6 .. code-block:: Python # sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 9-10 In this example, we show how to fill specified area with a given color. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 19-20 We generate a sample from a standard gaussian distribution. .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: Python dist = ot.Normal() graph = dist.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_fill_area_001.png :alt: plot graphs fill area :srcset: /auto_graphs/images/sphx_glr_plot_graphs_fill_area_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python def linearSample(xmin, xmax, npoints): """Returns a sample created from a regular grid from xmin to xmax with npoints points.""" step = (xmax - xmin) / (npoints - 1) rg = ot.RegularGrid(xmin, step, npoints) vertices = rg.getVertices() return vertices .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python a = 1.0 b = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. code-block:: Python nplot = 100 # Number of points in the plot x = linearSample(a, b, nplot) y = dist.computePDF(x) .. GENERATED FROM PYTHON SOURCE LINES 49-50 The following function uses the `PolygonArray` class to create a area filled with a given color. .. GENERATED FROM PYTHON SOURCE LINES 53-68 .. code-block:: Python def drawInTheBounds(vLow, vUp, n_test): """ Draw the area within the bounds. """ palette = ot.Drawable.BuildDefaultPalette(2) myPaletteColor = palette[1] polyData = [[vLow[i], vLow[i + 1], vUp[i + 1], vUp[i]] for i in range(n_test - 1)] polygonList = [ ot.Polygon(polyData[i], myPaletteColor, myPaletteColor) for i in range(n_test - 1) ] boundsPoly = ot.PolygonArray(polygonList) return boundsPoly .. GENERATED FROM PYTHON SOURCE LINES 69-70 Compute the bounds to fill: the lower vertical bound is zero and the upper vertical bound is the PDF. .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python vLow = [[x[i, 0], 0.0] for i in range(nplot)] vUp = [[x[i, 0], y[i, 0]] for i in range(nplot)] .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python area = dist.computeCDF(b) - dist.computeCDF(a) .. GENERATED FROM PYTHON SOURCE LINES 79-87 .. code-block:: Python boundsPoly = drawInTheBounds(vLow, vUp, nplot) graph = dist.drawPDF() graph.add(boundsPoly) graph.setTitle("Area = %.3f" % (area)) graph.setLegends([""]) view = viewer.View(graph) plt.show() .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_fill_area_002.png :alt: Area = 0.136 :srcset: /auto_graphs/images/sphx_glr_plot_graphs_fill_area_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-89 The CDF difference is equal to the area under the curve. .. _sphx_glr_download_auto_graphs_plot_graphs_fill_area.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_fill_area.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_graphs_fill_area.py `