如何将 pyqtgraph IsocurveItem 实例与空绘图轴(而不是 ImageItem)对齐?

How can I align an pyqtgraph IsocurveItem instance to empty plot axes (rather than an ImageItem)?

问题

IsocurveItem 的 pyqtgraph 文档很有帮助地建议使用此 class 绘制的轮廓可以通过 isocurve.setParentItem(image)ImageItem 实例对齐。但是,如果我不想显示图像数据,如何缩放轮廓输出以正确对齐给定 X、Y 数据的轴?

示例设置

使用此代码示例绘制:

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import sys

# Setup
app = QtGui.QApplication([])
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

win = pg.PlotWidget()
layout = pg.GraphicsLayout()
win.setCentralItem(layout)
ax = pg.PlotItem()
layout.addItem(ax)

# Generate data
x = np.linspace(0, 6.28, 30)
y = x[:]
xx, yy = np.meshgrid(x, y)
z = np.sin(xx) + np.cos(yy)

# Add data
ax.setXRange(x.min(), x.max())
ax.setYRange(y.min(), y.max())
c = pg.IsocurveItem(data=z, level=0.5, pen='r')
# c.setParentItem(ax)  # This doesn't work
ax.addItem(c)

# Finish up
win.show()
sys.exit(app.exec_())

输出这个:

或者,如果没有 setXRange 和 setYRange 部分,它看起来像这样:

我希望将第二个绘图中的绘图拉伸以适合第一个绘图的轴。 所以,我想我只需要告诉 IsocurveItem 如何在给定 X、Y、Z 数据而不只是 Z 的情况下挤压和对齐自身。我该怎么做?

P.S。等效* matplotlib 轮廓调用将是 contour(x, y, z, levels=[0.5], colors='r').

*必须处理轴顺序/行优先与列优先;没什么大不了的。

失败的解决方案

不可见缩放 ImageItem

添加一个 ImageItem 并对其进行缩放,稍后再使其不可见。尝试通过将其父级设置为 ImageItem:

来制作 IsocurveItem 比例尺
img = pg.ImageItem(z, axisOrder='row-major')
img.scale((x.max() - x.min()) / img.width(), (y.max() - y.min()) / img.height())
ax.addItem(img)
c.setParentItem(img)

ImageItem 秤,但 IsocurveItem 并没有随行。

IsocurveItem通过GraphicsObject继承了.scale().translate()方法,它是子类。因此,只需在定义 c:

之后将这些行添加到示例中
c.translate(x.min(), y.min())
c.scale((x.max() - x.min()) / np.shape(z)[0], (y.max() - y.min()) / np.shape(z)[1])

为了测试更严谨,我也改了x所以不从0开始:

x = np.linspace(10, 16.28, 30)

现在输出

在混合中使用 ImageItem 可能更容易可视化(也经过翻译和缩放):