itsaMeMathi0x
itsaMeMathi0x

Reputation: 172

How to set Axes limits on OpenTurns Viewer?

I'm using openturns to find the best fit distribution for my data. I got to plot it alright, but the X limit is far bigger than I'd like. My code is:

import statsmodels.api as sm
import openturns as ot
import openturns.viewer as otv

data = in_seconds

sample = ot.Sample(data, 1)
tested_factories = ot.DistributionFactory.GetContinuousUniVariateFactories()
best_model, best_bic = ot.FittingTest.BestModelBIC(sample, tested_factories)
print(best_model)


graph = ot.HistogramFactory().build(sample).drawPDF()
bestPDF = best_model.drawPDF()
bestPDF.setColors(["blue"])
graph.add(bestPDF)

name = best_model.getImplementation().getClassName()
graph.setLegends(["Histogram",name])
graph.setXTitle("Latências (segundos)")
graph.setYTitle("Frequência")


otv.View(graph)

I'd like to set X limits as something like "graph.setXLim", as we'd do in matplotlib, but I'm stuck with it as I'm new to OpenTurns.

Thanks in advance.

Upvotes: 2

Views: 340

Answers (2)

Michael Baudin
Michael Baudin

Reputation: 1151

Any OpenTURNS graph has a getBoundingBox method which returns the bounding box as a dimension 2 Interval. We can get/set the lower and upper bounds of this interval with getLowerBound and getUpperBound. Each of these bounds is a Point with dimension 2. Hence, we can set the bounds of the graphics prior to the use of the View class.

In the following example, I create a simple graph containing the PDF of the gaussian distribution.

import openturns as ot
import openturns.viewer as otv
n = ot.Normal()
graph = n.drawPDF()
_ = otv.View(graph)

Bell curve

Suppose that I want to set the lower X axis to -1. The script:

boundingBox = graph.getBoundingBox()
lb = boundingBox.getLowerBound()
print(lb)

produces:

[-4.10428,-0.0195499]

The first value in the Point is the X lower bound and the second is the Y lower bound. The following script sets the first component of the lower bound to -1, wraps the lower bound into the bounding box and sets the bounding box into the graph.

lb[0] = -1.0
boundingBox.setLowerBound(lb)
graph.setBoundingBox(boundingBox)
_ = otv.View(graph)

This produces the following graph.

Bell curve with minimum X axis equal to -1

The advantage of these methods is that they configure the graphics from the library, before the rendering is done by Matplotlib. The drawback is that they are a little more verbose than the Matplotlib counterpart.

Upvotes: 1

KieranD
KieranD

Reputation: 25

Here is a minimal example adapted from openTURNS examples (see http://openturns.github.io/openturns/latest/examples/graphs/graphs_basics.html) to set the x range (initially from [-4,4] to [-2,2]) :

import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt

n = ot.Normal()

# 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`.
graph = n.drawPDF()

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

graph.setXTitle("N")
graph.setYTitle("PDF")
graph.setTitle("Probability density function of the standard gaussian distribution")
graph.setLegends(["N"])
graph.setColors(["blue"])

# Combine several graphics
# In order to combine several graphics, 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()

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

# Using openturns.viewer
view = viewer.View(graph)

# Get the matplotlib.axes.Axes member with getAxes()
# Similarly, there is a getFigure() method as well
axes = view.getAxes()  # axes is a matplotlib object
_ = axes[0].set_xlim(-2.0, 2.0)

plt.show()

You can read the definition of the View object here :

https://github.com/openturns/openturns/blob/master/python/src/viewer.py

As you will see, the View class contains matplotlib objects such as axes and figure. Once accessed by the getAxes (or getFigure) you can use the matplotlib methods.

Upvotes: 1

Related Questions