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

How to set Axes limits on OpenTurns Viewer?

我正在使用 openturns 为我的数据找到最合适的分布。我得把它画好,但 X 限制比我想要的要大得多。我的代码是:

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)

我想将 X 限制设置为类似于“graph.setXLim”,就像我们在 matplotlib 中所做的那样,但由于我是 OpenTurns 的新手,所以我坚持使用它。

提前致谢。

这是一个改编自 openTURNS 示例(参见 http://openturns.github.io/openturns/latest/examples/graphs/graphs_basics.html)的最小示例,用于设置 x 范围(最初从 [-4,4] 到 [-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()

您可以在此处阅读 View 对象的定义:

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

如您所见,View class 包含坐标轴和图形等 matplotlib 对象。一旦被 getAxes(或 getFigure)访问,您就可以使用 matplotlib 方法。

任何 OpenTURNS 图都有一个 getBoundingBox 方法,其中 returns 边界框作为维度 2 Interval。我们可以用 getLowerBoundgetUpperBound get/set 这个区间的下限和上限。这些边界中的每一个都是维度为 2 的 Point。因此,我们可以在使用 View class.

之前设置图形的边界

在下面的示例中,我创建了一个包含高斯分布 PDF 的简单图形。

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

假设我想将下X轴设置为-1。 剧本:

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

产生:

[-4.10428,-0.0195499]

Point中的第一个值是X下限,第二个是Y下限。以下脚本将下限的第一个组件设置为 -1,将下限包裹到边界框中并将边界框设置到图形中。

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

这会产生下图。

这些方法的优点是它们在由 Matplotlib 完成渲染之前从库中配置图形。缺点是它们比 Matplotlib 对应物更冗长。