我想使用 pyqtgraph 绘制一些信号的光谱与以 nm 为单位的波长。更难的部分是沿着图的顶部绘制相应波长的能量是有用的。有关示例,请参见下图。
我的问题是如何在 pyqtgraph 中完成此操作。我考虑过尝试修改两个 y 轴的解决方案(例如 here ),但我认为这不太合适。轴应该是链接的,不能独立自由移动,所以添加一个新的 View 框似乎不是正确的路径,除非它要链接所有内容。
我想我可以通过添加一个新的 axisitem 并连接适当的调整大小信号来强制新的轴坐标工作来做一些事情,但这感觉很脏。
http://www.nature.com/nnano/journal/v10/n10/images/nnano.2015.178-f1.jpg
请您参考如下方法:
我找到了一个快速解决方法,它在某种程度上对我的目的有用。我想我会把它贴在这里,以防其他人好奇,这可能对他们有帮助。它涉及子类化 AxisItem 并指定 tickStrings。它工作不理想,因为它保持与主要底部轴相同的刻度位置,但它至少应该让我了解我正在查看的内容。
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
class CustomAxis(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
return ['{:.4f}'.format(1./i) for i in values]
pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
pw.setWindowTitle('pyqtgraph example: MultipleXAxes')
p1 = pw.plotItem
p1.setLabels(left='axis 1')
# Get rid of the item at the grid position where the top should be
p1.layout.removeItem(p1.getAxis('top'))
# make our own, setting the parent and orientation
caxis = CustomAxis(orientation='top', parent=p1)
caxis.setLabel('inverted')
caxis.linkToView(p1.vb)
# set the new one for internal plotitem
p1.axes['top']['item'] = caxis
# and add it to the layout
p1.layout.addItem(caxis, 1, 1)
p1.plot(np.arange(1, 7), [1,2,4,8,16,32])
#p2.addItem(pg.PlotCurveItem(1./np.arange(1, 7), [1,2,4,8,16,32], pen='b'))
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
显然,返回值应该替换为与两个轴相关的任何函数。
