2015-10-26 36 views
1

我想用pyqtgraph来绘制一些信号的光谱与波长的单位nm。更难的部分是绘制沿着图形顶部的相应波长的能量将是有用的。看下面的图例子。Pyqtgraph多个水平轴

我的问题是如何在pyqtgraph中完成此操作。我想过尝试修改两个y轴解决方案(例如here),但我认为它不合适。轴应该被链接,而不是自由地独立移动,所以添加一个新的viewbox似乎不是正确的路径,除非它是链接所有东西。

我想我可以通过添加一个新的axisitem并连接适当的调整大小信号来强制新的坐标轴工作,但这感觉很肮脏。

http://www.nature.com/nnano/journal/v10/n10/images/nnano.2015.178-f1.jpg

回答

0

我发现了一个快速的工作围绕这在一定程度适合我的目的。我想我会在这里发布它,以防其他人感到好奇,并且可能对他们有所帮助。它涉及继承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_() 

很明显,返回值应该被任何与两轴相关的函数取代。

+0

我有一个类似的问题,但在我的情况下,它也取决于如何改变轴的情节。我用以下方式解决了这个问题,但感觉不正确。该问题的链接http://stackoverflow.com/questions/43545364/pyqtgraph-imageitems-new-axis-labels。也许你可以帮助我? –