简体   繁体   English

如何在 OpenTurns Viewer 上设置轴限制?

[英]How to set Axes limits on OpenTurns Viewer?

I'm using openturns to find the best fit distribution for my data.我正在使用 openturns 来找到最适合我的数据的分布。 I got to plot it alright, but the X limit is far bigger than I'd like.我到了 plot 没问题,但是 X 限制比我想要的要大得多。 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.我想将 X 限制设置为“graph.setXLim”之类的东西,就像我们在 matplotlib 中所做的那样,但我坚持使用它,因为我是 OpenTurns 的新手。

Thanks in advance.提前致谢。

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]): 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:您可以在此处阅读视图 object 的定义:

https://github.com/openturns/openturns/blob/master/python/src/viewer.py 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.如您所见, View class 包含 matplotlib 对象,例如轴和图形。 Once accessed by the getAxes (or getFigure ) you can use the matplotlib methods.一旦被getAxes (或getFigure )访问,您就可以使用 matplotlib 方法。

Any OpenTURNS graph has a getBoundingBox method which returns the bounding box as a dimension 2 Interval .任何 OpenTURNS 图都有一个getBoundingBox方法,该方法将边界框作为维度 2 Interval返回。 We can get/set the lower and upper bounds of this interval with getLowerBound and getUpperBound .我们可以使用getLowerBoundgetUpperBound获取/设置此区间的下限和上限。 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.这些边界中的每一个都是一个维度为 2 的Point 。因此,我们可以在使用View class 之前设置图形的边界。

In the following example, I create a simple graph containing the PDF of the gaussian distribution.在以下示例中,我创建了一个简单的图形,其中包含高斯分布的 PDF。

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

钟形曲线

Suppose that I want to set the lower X axis to -1.假设我想将下 X 轴设置为 -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. Point中的第一个值是 X 下限,第二个是 Y 下限。 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.以下脚本将下限的第一个组件设置为 -1,将下限包装到边界框并将边界框设置到图中。

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

This produces the following graph.这将产生以下图表。

最小 X 轴等于 -1 的贝尔曲线

The advantage of these methods is that they configure the graphics from the library, before the rendering is done by Matplotlib.这些方法的优点是它们从库中配置图形,然后由 Matplotlib 完成渲染。 The drawback is that they are a little more verbose than the Matplotlib counterpart.缺点是它们比 Matplotlib 对应物更冗长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM