PyQwt 反转轴(或,设置 PyQwt 属性)

Posted

技术标签:

【中文标题】PyQwt 反转轴(或,设置 PyQwt 属性)【英文标题】:PyQwt Invert Axis (or, setting PyQwt Attributes) 【发布时间】:2014-02-01 01:04:47 【问题描述】:

我正在 PyQwt 中创建一个奈奎斯特图。它目前可以工作,除了我需要翻转 y 轴 - 约定底部为零,并且随着您的上升而增加负数。

我知道您将如何在 C++ 中执行此操作,但无法弄清楚如何在 Python 中执行此操作。我不完全理解 Qt/Qwt 如何转换为 PyQt/PyQwt。根据http://qwt.sourceforge.net/class_qwt_scale_engine.html,需要设置一个属性Inverted。在 C++ 中,这看起来像:

ui.plotWidget->axisScaleEngine(QwtPlot::yLeft)->setAttribute(QwtScaleEngine::Inverted, true);

我想不出办法,我在主窗口的__init__(self) 中尝试了几种方法:

self.ui.qwtPlot.setAxisTitle(Qwt.QwtPlot.yLeft, 'Imaginary Impedance')
self.ui.qwtPlot.setAxisScaleEngine(Qwt.QwtPlot.yLeft, Qwt.QwtScaleEngine.Inverted))
# TypeError: QwtPlot.setAxisScaleEngine(): argument 2 has unexpected type 'Attribute'
self.ui.qwtPlot.setAxisScaleEngine(Qwt.QwtPlot.yLeft,   Qwt.QwtScaleEngine.setAttribute(Inverted))
# NameError: global name 'Inverted' is not defined
self.ui.qwtPlot.setAxisScaleEngine(Qwt.QwtPlot.yLeft, Qwt.QwtScaleEngine.setAttribute(Qwt.QwtScaleEngine.Inverted)
# TypeError: QwtScaleEngine.setAttribute(): first argument of unbound method must have type 'QwtScaleEngine'

我还尝试定义一个新类,就像在 http://sourceforge.net/p/pyqwt/patches/1/ 中所做的那样,如下所示:

class InvertedAxisEngine(Qwt.QwtScaleEngine):
    def __init__(self):
        Qwt.QwtScaleEngine.__init__(self)
        self.setAttribute(Qwt.QwtScaleEngine.Inverted)
# back in Window.__init__(self)
self.ui.qwtPlot.setAxisScaleEngine(Qwt.QwtPlot.yLeft, InvertedAxisEngine)
#TypeError: QwtPlot.setAxisScaleEngine(): argument 2 has unexpected type 'PyQt4.QtCore.pyqtWrapperType'
self.ui.qwtPlot.setAxisScaleEngine(Qwt.QwtPlot.yLeft, InvertedAxisEngine())
# NotImplementedError: QwtScaleEngine.divideScale() is abstract and must be overridden
#NotImplementedError: QwtScaleEngine.transformation() is abstract and must be overridden

我觉得我把这件事弄得太复杂了。

编辑 - 下面的评论启发了一些事情,比如将 =True 添加到 Inverted(不,没有关键字参数,我也尝试了这个,这与 unexpected type (包装器)有关。

self.ui.inverted_engine = Qwt.QwtScaleEngine
self.ui.inverted_engine.Inverted = True        
self.ui.qwtPlot.setAxisScaleEngine(Qwt.QwtPlot.yLeft, self.ui.inverted_engine)

在 GUI 运行时我没有解释器提示访问权限,但我在 init 中添加了 print dir(Qwt.QwtScaleEngine),它确实存在:['Attribute', 'Floating', 'IncludeReference', 'Inverted', 'NoAttribute', 'Symmetric', '__class__',....

【问题讨论】:

你试过self.ui.qwPlot.axisScaleEngine(Qwt.QwtPlot.yLeft).setAttribute(Qwt.QwtScaleEngine.Inverted, true)吗?您也可以直接从 python 控制台窗口尝试,例如打印 dir(Qwt.QwtScaleEngine) 以查看它是否已定义。 该代码出现语法错误。 (我尝试添加一些括号,将 't' 添加回 qwtPlot [第一个])。它启发了其他一些效果不佳的东西,请参阅上面的编辑。 【参考方案1】:

在检查http://orange.biolab.si/svn/orange/externals/trunk/install-scripts/mac/bundle/Orange.app/Contents/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyQt4/Qwt5/qplt.py 的代码后,我终于明白了这一点。我认为可能还有一种方法可以使用setAxisScaleEngine() 来做到这一点,但这可行。

    QMainWindow.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    self.ui.qwtPlot.setAxisTitle(Qwt.QwtPlot.yLeft, 'Imaginary Impedance (ohms)')
    self.ui.qwtPlot.setCanvasBackground(Qt.white)
    engine = self.ui.qwtPlot.axisScaleEngine(Qwt.QwtPlot.yLeft)
    Qwt.QwtScaleEngine.setAttribute(engine, Qwt.QwtScaleEngine.Inverted)

【讨论】:

以上是关于PyQwt 反转轴(或,设置 PyQwt 属性)的主要内容,如果未能解决你的问题,请参考以下文章

PyQwt QwtPlot 慢

通过鼠标使用 PyQwt 从画布上记录曲线/多边形

架构 x86_64 的未定义符号:“hex(QTextStream&)”,引用自:(在 Mac 上安装 PyQwt-5.2.0)

qwtplot 的定义

拥有许多不同的数据类型有啥好处?

PyQtGraph PlotWidget:如何强制每次绘制(更改范围)以进行“实时”绘图