.. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` 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 =================== .. code-block:: default # sphinx_gallery_thumbnail_number = 2 In this example, we show how to fill specified area with a given color. .. code-block:: default import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) We generate a sample from a standard gaussian distribution. .. code-block:: default dist = ot.Normal() graph = dist.drawPDF() view = viewer.View(graph) .. image:: /auto_graphs/images/sphx_glr_plot_graphs_fill_area_001.png :alt: plot graphs fill area :class: sphx-glr-single-img .. code-block:: default 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 .. code-block:: default a = 1. b = 2. .. code-block:: default nplot = 100 # Number of points in the plot x = linearSample(a,b,nplot) y = dist.computePDF(x) The following function uses the `PolygonArray` class to create a area filled with a given color. .. code-block:: default 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 Compute the bounds to fill: the lower vertical bound is zero and the upper vertical bound is the PDF. .. code-block:: default vLow = [[x[i,0],0.] for i in range(nplot)] vUp = [[x[i,0],y[i,0]] for i in range(nplot)] .. code-block:: default area = dist.computeCDF(b) - dist.computeCDF(a) .. code-block:: default 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:: /auto_graphs/images/sphx_glr_plot_graphs_fill_area_002.png :alt: Area = 0.136 :class: sphx-glr-single-img The CDF difference is equal to the area under the curve. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.179 seconds) .. _sphx_glr_download_auto_graphs_plot_graphs_fill_area.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_graphs_fill_area.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_graphs_fill_area.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_