.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_graphs/plot_graphs_basics.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_basics.py: A quick start guide to graphs ============================== .. GENERATED FROM PYTHON SOURCE LINES 5-7 .. code-block:: Python # sphinx_gallery_thumbnail_number = 4 .. GENERATED FROM PYTHON SOURCE LINES 8-9 In this example, we show how to create graphs. We show how to create and configure its axes and its colors. We show how to create a plot based on the combination of several plots. .. GENERATED FROM PYTHON SOURCE LINES 12-16 The `draw` method the `Graph` class ----------------------------------- The simplest way to create a graph is to use the `draw` method. The :class:`~openturns.Normal` distribution for example provides a method to draw its density function. .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python import openturns as ot import openturns.viewer as viewer import matplotlib.pyplot as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python n = ot.Normal() n .. raw:: html
Normal


.. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python graph = n.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_001.png :alt: plot graphs basics :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 33-34 To configure the look of the plot, we can first observe the type of graph returned by the `drawPDF` method returns: it is a :class:`~openturns.Graph`. .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python graph = n.drawPDF() type(graph) .. GENERATED FROM PYTHON SOURCE LINES 40-42 The class:`~openturns.Graph` class provides several methods to configure the legends, the title and the colors. Since a graph can contain several sub-graphs, the `setColors` method takes a list of colors as inputs argument: each item of the list corresponds to the sub-graphs. .. GENERATED FROM PYTHON SOURCE LINES 44-51 .. code-block:: Python graph.setXTitle("N") graph.setYTitle("PDF") graph.setTitle("Probability density function of the standard Gaussian distribution") graph.setLegends(["N"]) graph.setColors(["blue"]) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_002.png :alt: Probability density function of the standard Gaussian distribution :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-56 Combine several graphs ---------------------- In order to combine several graphs, we can use the `add` method. .. GENERATED FROM PYTHON SOURCE LINES 58-59 Let us create an empirical histogram from a sample. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python sample = n.getSample(100) .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python histo = ot.HistogramFactory().build(sample).drawPDF() view = viewer.View(histo) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_003.png :alt: X0 PDF :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-69 Then we add the histogram to the `graph` with the `add` method. The `graph` then contains two plots. .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python graph.add(histo) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_004.png :alt: Probability density function of the standard Gaussian distribution :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-79 Draw a cloud ------------ The :class:`~openturns.Cloud` class creates clouds of bidimensional points. To illustrate it, let us create two Normal distributions in two dimensions. .. GENERATED FROM PYTHON SOURCE LINES 81-82 Create a Funky distribution .. GENERATED FROM PYTHON SOURCE LINES 82-89 .. 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) .. GENERATED FROM PYTHON SOURCE LINES 90-91 Create a Punk distribution .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: Python x1 = ot.Normal(1.0, 1) x2 = ot.Normal(-2, 1) x_punk = ot.JointDistribution([x1, x2], copula) .. GENERATED FROM PYTHON SOURCE LINES 96-97 Let us mix these two distributions. .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: Python mixture = ot.Mixture([x_funk, x_punk], [0.5, 1.0]) .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python n = 500 sample = mixture.getSample(n) .. GENERATED FROM PYTHON SOURCE LINES 106-111 .. code-block:: Python graph = ot.Graph("n=%d" % (n), "X1", "X2", True, "") cloud = ot.Cloud(sample) graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_005.png :alt: n=500 :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-116 We sometimes want to customize the graph by choosing the type of point (square, triangle, circle, etc.), of line (continuous, dashed, etc.) or another parameter. We can know the list of possible values with the corresponding `getValid` method. For example, the following function returns the possible values of the `PointStyle` parameter. .. GENERATED FROM PYTHON SOURCE LINES 118-120 .. code-block:: Python ot.Drawable.GetValidPointStyles() .. raw:: html
[square,circle,triangleup,plus,times,diamond,triangledown,star,fsquare,fcircle,ftriangleup,fdiamond,bullet,dot,none,.,,,o,v,^,<,>,1,2,3,4,8,s,p,P,*,h,H,+,x,X,d,D,|,_,, ,None]


.. GENERATED FROM PYTHON SOURCE LINES 121-122 The following method returns the list of colors. .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python ot.Drawable.GetValidColors()[0:10] .. raw:: html
[aliceblue,antiquewhite,antiquewhite1,antiquewhite2,antiquewhite3,antiquewhite4,aquamarine,aquamarine1,aquamarine2,aquamarine3]


.. GENERATED FROM PYTHON SOURCE LINES 127-128 In the following graph, we use the "aquamarine1" color with "fcircle" circles. .. GENERATED FROM PYTHON SOURCE LINES 130-137 .. code-block:: Python graph = ot.Graph("n=%d" % (n), "X1", "X2", True, "") cloud = ot.Cloud(sample) cloud.setColor("aquamarine1") cloud.setPointStyle("fcircle") graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_006.png :alt: n=500 :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-142 Configure the style of points and the thickness of a curve ---------------------------------------------------------- Assume that we want to plot the `sine` curve from -2 to 2. The simplest way is to use the `draw` method of the function. .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: Python g = ot.SymbolicFunction("x", "sin(x)") .. GENERATED FROM PYTHON SOURCE LINES 147-150 .. code-block:: Python graph = g.draw(-2, 2) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_007.png :alt: y0 as a function of x :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 151-152 One would rather get a dashed curve: let us search for the available line styles. .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: Python ot.Drawable.GetValidLineStyles() .. raw:: html
[blank,solid,dashed,dotted,dotdash,longdash,twodash]


.. GENERATED FROM PYTHON SOURCE LINES 158-159 In order to use the :class:`~openturns.Curve` class, it will be easier if we have a method to generate a :class:`~openturns.Sample` containing points regularly spaced in an interval. .. GENERATED FROM PYTHON SOURCE LINES 162-171 .. 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 172-175 .. code-block:: Python x = linearSample(-2, 2, 50) y = g(x) .. GENERATED FROM PYTHON SOURCE LINES 176-184 .. code-block:: Python graph = ot.Graph("Sinus", "x", "sin(x)", True) curve = ot.Curve(x, y) curve.setLineStyle("dashed") curve.setLineWidth(4) graph.add(curve) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_008.png :alt: Sinus :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 185-190 Create colored curves --------------------- In some situations, we want to create curves with different colors. In this case, the following function generates a color corresponding to the `indexCurve` integer in a ensemble of `maximumNumberOfCurves` curves. .. GENERATED FROM PYTHON SOURCE LINES 193-202 .. code-block:: Python def createHSVColor(indexCurve, maximumNumberOfCurves): """Create a HSV color for the indexCurve-th curve from a sample with maximum size equal to maximumNumberOfCurves""" color = ot.Drawable.ConvertFromHSV( indexCurve * 360.0 / maximumNumberOfCurves, 1.0, 1.0 ) return color .. GENERATED FROM PYTHON SOURCE LINES 203-205 .. code-block:: Python pofa = ot.HermiteFactory() .. GENERATED FROM PYTHON SOURCE LINES 206-216 .. code-block:: Python graph = ot.Graph("Orthonormal Hermite polynomials", "x", "y", True, "lower right") degreemax = 5 for k in range(degreemax): pk = pofa.build(k) curve = pk.draw(-3.0, 3.0, 50) curve.setLegends(["P%d" % (k)]) curve.setColors([createHSVColor(k, degreemax)]) graph.add(curve) view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_009.png :alt: Orthonormal Hermite polynomials :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 217-223 Create matrices of graphs ------------------------- The library provides features to create a grid of graphs. However, we can use the `add_subplot` function from `Matplotlib`. Let us create two graphs of the PDF and CDF of the following Normal distribution.. .. GENERATED FROM PYTHON SOURCE LINES 225-229 .. code-block:: Python n = ot.Normal() myPDF = n.drawPDF() myCDF = n.drawCDF() .. GENERATED FROM PYTHON SOURCE LINES 230-231 Using `~openturns.GridLayout`. .. GENERATED FROM PYTHON SOURCE LINES 231-236 .. code-block:: Python grid = ot.GridLayout(1, 2) grid.setGraph(0, 0, myPDF) grid.setGraph(0, 1, myCDF) _ = viewer.View(grid) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_010.png :alt: plot graphs basics :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 237-242 Another method is to create a figure with the `figure` function from `Matplotlib`, then add two graphs with the `add_subplot` function. We use the `viewer.View` function to create the required `Matplotlib` object. Since we are not interested by the output of the `View` function, we use the dummy variable `_` as output. The title is finally configured with `suptitle`. .. GENERATED FROM PYTHON SOURCE LINES 244-251 .. code-block:: Python fig = plt.figure(figsize=(12, 4)) ax_pdf = fig.add_subplot(1, 2, 1) _ = viewer.View(myPDF, figure=fig, axes=[ax_pdf]) ax_cdf = fig.add_subplot(1, 2, 2) _ = viewer.View(myCDF, figure=fig, axes=[ax_cdf]) _ = fig.suptitle("The gaussian") .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_011.png :alt: The gaussian :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 252-254 Save a plot into a file ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 256-257 The :class:`openturns.viewer.View` class has a `save` method which saves the graph into an image. .. GENERATED FROM PYTHON SOURCE LINES 261-266 .. code-block:: Python n = ot.Normal() graph = n.drawPDF() view = viewer.View(graph) view.save("normal.png") .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_012.png :alt: plot graphs basics :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_012.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 267-268 We can use the `dpi` option to configure the resolution in dots per inch. .. GENERATED FROM PYTHON SOURCE LINES 270-272 .. code-block:: Python view.save("normal-100dpi.png", dpi=100) .. GENERATED FROM PYTHON SOURCE LINES 273-275 Configure the size of a graph with `matplotlib` ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 279-280 We first create a graph containing the PDF of a Normal distribution .. GENERATED FROM PYTHON SOURCE LINES 282-285 .. code-block:: Python n = ot.Normal() graph = n.drawPDF() .. GENERATED FROM PYTHON SOURCE LINES 286-287 The `figure_kw` keyword argument sets the optional arguments of the figure. In the following statement, we set the figure size in inches .. GENERATED FROM PYTHON SOURCE LINES 289-291 .. code-block:: Python view = viewer.View(graph, figure_kw={"figsize": (12, 8)}) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_013.png :alt: plot graphs basics :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_013.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 292-293 The `getFigure` method returns the current figure. This allows one to configure it as any other Matplotlib figure. In the following example, we configure the `suptitle`. .. GENERATED FROM PYTHON SOURCE LINES 295-299 .. code-block:: Python fig = view.getFigure() fig.suptitle("The suptitle") fig .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 300-301 The `plot_kw` optional argument sets the arguments of the plot. In the following example, we set the color of the plot in blue. .. GENERATED FROM PYTHON SOURCE LINES 303-305 .. code-block:: Python view = viewer.View(graph, plot_kw={"color": "blue"}) plt.show() .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_basics_014.png :alt: plot graphs basics :srcset: /auto_graphs/images/sphx_glr_plot_graphs_basics_014.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_graphs_plot_graphs_basics.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_basics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_graphs_basics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_graphs_basics.zip `