File: plot_graphs_basics.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (306 lines) | stat: -rw-r--r-- 7,925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
A quick start guide to graphs
==============================
"""

# 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 graph is to use the `draw` method. The :class:`~openturns.Normal` distribution for example provides a method to draw its density function.

# %%
import openturns as ot
import openturns.viewer as otv
import matplotlib.pyplot as plt


# %%
n = ot.Normal()
n

# %%
graph = n.drawPDF()
view = otv.View(graph)

# %%
# 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`.

# %%
graph = n.drawPDF()
type(graph)

# %%
# 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.

# %%
graph.setXTitle("N")
graph.setYTitle("PDF")
graph.setTitle("Probability density function of the standard Gaussian distribution")
graph.setLegends(["N"])
graph.setColors(["blue"])
view = otv.View(graph)

# %%
# Combine several graphs
# ----------------------
#
# In order to combine several graphs, we can use the `add` method.

# %%
# Let us create an empirical histogram from a sample.

# %%
sample = n.getSample(100)

# %%
histo = ot.HistogramFactory().build(sample).drawPDF()
view = otv.View(histo)

# %%
# Then we add the histogram to the `graph` with the `add` method. The `graph` then contains two plots.

# %%
graph.add(histo)
view = otv.View(graph)

# %%
# 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.

# %%
# Create a Funky distribution
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)

# %%
# Create a Punk distribution
x1 = ot.Normal(1.0, 1)
x2 = ot.Normal(-2, 1)
x_punk = ot.JointDistribution([x1, x2], copula)

# %%
# Let us mix these two distributions.

# %%
mixture = ot.Mixture([x_funk, x_punk], [0.5, 1.0])

# %%
n = 500
sample = mixture.getSample(n)

# %%
graph = ot.Graph("n=%d" % (n), "X1", "X2", True, "")
cloud = ot.Cloud(sample)
graph.add(cloud)
view = otv.View(graph)

# %%
# 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.

# %%
ot.Drawable.GetValidPointStyles()

# %%
# The following method returns the list of colors.

# %%
ot.Drawable.GetValidColors()[0:10]

# %%
# In the following graph, we use the "aquamarine1" color with "fcircle" circles.

# %%
graph = ot.Graph("n=%d" % (n), "X1", "X2", True, "")
cloud = ot.Cloud(sample)
cloud.setColor("aquamarine1")
cloud.setPointStyle("fcircle")
graph.add(cloud)
view = otv.View(graph)

# %%
# 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.

# %%
g = ot.SymbolicFunction("x", "sin(x)")

# %%
graph = g.draw(-2, 2)
view = otv.View(graph)

# %%
# One would rather get a dashed curve: let us search for the available line styles.

# %%
ot.Drawable.GetValidLineStyles()


# %%
# 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.


# %%
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


# %%
x = linearSample(-2, 2, 50)
y = g(x)

# %%
graph = ot.Graph("Sinus", "x", "sin(x)", True)
curve = ot.Curve(x, y)
curve.setLineStyle("dashed")
curve.setLineWidth(4)
graph.add(curve)
view = otv.View(graph)


# %%
# 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.


# %%
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


# %%
pofa = ot.HermiteFactory()

# %%
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 = otv.View(graph)

# %%
# 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..

# %%
n = ot.Normal()
myPDF = n.drawPDF()
myCDF = n.drawCDF()

# %%
# Using `~openturns.GridLayout`.
grid = ot.GridLayout(1, 2)
grid.setGraph(0, 0, myPDF)
grid.setGraph(0, 1, myCDF)
_ = otv.View(grid)

# %%
# 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 `otv.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`.

# %%
fig = plt.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")

# %%
# Save a plot into a file
# -----------------------

# %%
# The :class:`openturns.viewer.View` class has a `save` method which saves the graph into an image.

# %%

# %%
n = ot.Normal()
graph = n.drawPDF()
view = otv.View(graph)
view.save("normal.png")

# %%
# We can use the `dpi` option to configure the resolution in dots per inch.

# %%
view.save("normal-100dpi.png", dpi=100)

# %%
# Configure the size of a graph with `matplotlib`
# -----------------------------------------------

# %%

# %%
# We first create a graph containing the PDF of a Normal distribution

# %%
n = ot.Normal()
graph = n.drawPDF()

# %%
# The `figure_kw` keyword argument sets the optional arguments of the figure. In the following statement, we set the figure size in inches

# %%
view = otv.View(graph, figure_kw={"figsize": (12, 8)})

# %%
# 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`.

# %%
fig = view.getFigure()
fig.suptitle("The suptitle")
fig

# %%
# The `plot_kw` optional argument sets the arguments of the plot. In the following example, we set the color of the plot in blue.

# %%
view = otv.View(graph, plot_kw={"color": "blue"})

# %%
# Display all figures
otv.View.ShowAll()