2

我正在使用 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 的新手。

提前致谢。

4

2 回答 2

1

这是一个改编自 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该类包含 matplotlib 对象,例如轴和图形。一旦被getAxes(or getFigure) 访问,您就可以使用 matplotlib 方法。

于 2020-08-31T13:25:50.253 回答
1

任何 OpenTURNS 图都有一个getBoundingBox方法,该方法将边界框作为维度 2 返回IntervalgetLowerBound我们可以用 和 获取/设置这个区间的上下限getUpperBound。这些边界中的每一个Point都是维度为 2 的。因此,我们可以在使用View类之前设置图形的边界。

在以下示例中,我创建了一个包含高斯分布 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)

这将产生以下图表。

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

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

于 2020-09-01T09:12:40.070 回答