.. 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_basics.py:
A quick start guide to graphs
==============================
.. code-block:: default
# sphinx_gallery_thumbnail_number = 4
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.
The `draw` method the `Graph` class
-----------------------------------
The simplest way to create a graphics is to use the `draw` method. The `Normal` distribution for example provides a method to draw the density function of the gaussian distribution.
.. code-block:: default
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
.. code-block:: default
n = ot.Normal()
n
.. raw:: html
Normal(mu = 0, sigma = 1)
.. code-block:: default
graph = n.drawPDF()
view = viewer.View(graph)
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_001.png
:alt: plot graphs basics
:class: sphx-glr-single-img
To configure the look of the plot, we can first observe the type of graphics returned by the `drawPDF` method returns: it is a `Graph`.
.. code-block:: default
graph = n.drawPDF()
type(graph)
The `Graph` class provides several methods to configure the legends, the title and the colors. Since a graphics can contain several sub-graphics, the `setColors` takes a list of colors as inputs argument: each item of the list corresponds to the sub-graphics.
.. code-block:: default
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:: /auto_graphs/images/sphx_glr_plot_graphs_basics_002.png
:alt: Probability density function of the standard gaussian distribution
:class: sphx-glr-single-img
Combine several graphics
------------------------
In order to combine several graphics, we can use the `add` method.
Let us create an empirical histogram from a sample.
.. code-block:: default
sample = n.getSample(100)
.. code-block:: default
histo = ot.HistogramFactory().build(sample).drawPDF()
view = viewer.View(histo)
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_003.png
:alt: X0 PDF
:class: sphx-glr-single-img
Then we add the histogram to the `graph` with the `add` method. The `graph` then contains two plots.
.. code-block:: default
graph.add(histo)
view = viewer.View(graph)
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_004.png
:alt: Probability density function of the standard gaussian distribution
:class: sphx-glr-single-img
Draw a cloud
------------
The `Cloud` class creates clouds of bidimensional points. To demonstrate it, let us create two gaussian distributions in two dimensions.
Create a Funky distribution
.. code-block:: default
corr = ot.CorrelationMatrix(2)
corr[0, 1] = 0.2
copula = ot.NormalCopula(corr)
x1 = ot.Normal(-1., 1)
x2 = ot.Normal(2, 1)
x_funk = ot.ComposedDistribution([x1, x2], copula)
Create a Punk distribution
.. code-block:: default
x1 = ot.Normal(1.,1)
x2 = ot.Normal(-2,1)
x_punk = ot.ComposedDistribution([x1, x2], copula)
Let us mix these two distributions.
.. code-block:: default
mixture = ot.Mixture([x_funk, x_punk], [0.5,1.])
.. code-block:: default
n=500
sample = mixture.getSample(n)
.. code-block:: default
graph = ot.Graph("n=%d" % (n), "X1", "X2", True, '')
cloud = ot.Cloud(sample)
graph.add(cloud)
view = viewer.View(graph)
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_005.png
:alt: n=500
:class: sphx-glr-single-img
We sometimes want to customize the graphics 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.
.. code-block:: default
ot.Drawable.GetValidPointStyles()
.. raw:: html
In the following graphics, we use the "aquamarine1" color with "fcircle" circles.
.. code-block:: default
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:: /auto_graphs/images/sphx_glr_plot_graphs_basics_006.png
:alt: n=500
:class: sphx-glr-single-img
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.
.. code-block:: default
g = ot.SymbolicFunction("x","sin(x)")
.. code-block:: default
graph = g.draw(-2,2)
view = viewer.View(graph)
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_007.png
:alt: y0 as a function of x
:class: sphx-glr-single-img
I would rather get a dashed curve: let us search for the available line styles.
.. code-block:: default
ot.Drawable.GetValidLineStyles()
.. raw:: html
In order to use the `Curve` class, it will be easier if we have a method to generate a `Sample` containing points regularly spaced in an interval.
.. 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
x = linearSample(-2,2,50)
y = g(x)
.. code-block:: default
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:: /auto_graphs/images/sphx_glr_plot_graphs_basics_008.png
:alt: Sinus
:class: sphx-glr-single-img
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.
.. code-block:: default
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
.. code-block:: default
pofa = ot.HermiteFactory()
.. code-block:: default
graph = ot.Graph("Orthonormal Hermite polynomials","x","y",True,"bottomright")
degreemax = 5
for k in range(degreemax):
pk = pofa.build(k)
curve = pk.draw(-3.,3.,50)
curve.setLegends(["P%d" % (k)])
curve.setColors([createHSVColor(k,degreemax)])
graph.add(curve)
view = viewer.View(graph)
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_009.png
:alt: Orthonormal Hermite polynomials
:class: sphx-glr-single-img
Create matrices of graphics
---------------------------
The library does *not* has objects to create a grid of graphics. However, we can use the `add_subplot` function from Matplotlib.
Let us create two graphics of the PDF and CDF of the following gaussian distribution..
.. code-block:: default
n = ot.Normal()
myPDF = n.drawPDF()
myCDF = n.drawCDF()
.. code-block:: default
import pylab as pl
import openturns.viewer as otv
We create a figure with the `figure` function from Matplotlib, then we add two graphics 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`.
.. code-block:: default
fig = pl.figure(figsize=(12, 4))
ax_pdf = fig.add_subplot(1, 2, 1)
_ = otv.View(myPDF, figure=fig, axes=[ax_pdf])
ax_cdf = fig.add_subplot(1, 2, 2)
_ = otv.View(myCDF, figure=fig, axes=[ax_cdf])
_ = fig.suptitle("The gaussian")
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_010.png
:alt: The gaussian
:class: sphx-glr-single-img
Save a plot into a file
-----------------------
The `View` class has a `save` method which saves the graph into an image.
.. code-block:: default
import openturns.viewer as otv
.. code-block:: default
n = ot.Normal()
graph = n.drawPDF()
view = otv.View(graph)
view.save("normal.png")
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_011.png
:alt: plot graphs basics
:class: sphx-glr-single-img
We can use the `dpi` option to configure the resolution in dots per inch.
.. code-block:: default
view.save("normal-100dpi.png", dpi=100)
Configure the size of a graph with matplotlib
---------------------------------------------
.. code-block:: default
import openturns.viewer as otv
We first create a graph containing the PDF of a gaussian distribution
.. code-block:: default
n = ot.Normal()
graph = n.drawPDF()
The `figure_kwargs` keyword argument sets the optional arguments of the figure. In the following statement, we set the figure size in inches
.. code-block:: default
view = otv.View(graph, figure_kwargs = {"figsize": (12, 8)})
.. image:: /auto_graphs/images/sphx_glr_plot_graphs_basics_012.png
:alt: plot graphs basics
:class: sphx-glr-single-img
The `getFigure` method returns the current figure. This allows to configure it as any other Matplotlib figure. In the following example, we configure the `suptitle`.
.. code-block:: default
fig = view.getFigure()
fig.suptitle("The suptitle")
fig
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none