繁体   English   中英

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

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

我正在使用 openturns 来找到最适合我的数据的分布。 我到了 plot 没问题,但是 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 的新手。

提前致谢。

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()

您可以在此处阅读视图 object 的定义:

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

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

任何 OpenTURNS 图都有一个getBoundingBox方法,该方法将边界框作为维度 2 Interval返回。 我们可以使用getLowerBoundgetUpperBound获取/设置此区间的下限和上限。 这些边界中的每一个都是一个维度为 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)

这将产生以下图表。

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

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

暂无
暂无

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

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