2013-05-22 4 views
4

pyqtgraph를 사용 중이고 InfiniteLines의 범례에 항목을 추가하고 싶습니다.pyqtgraph : 플롯의 줄에 범례 추가

# -*- coding: utf-8 -*- 
""" 
Demonstrates basic use of LegendItem 

""" 
import initExample ## Add path to library (just for examples; you do not need this) 

import pyqtgraph as pg 
from pyqtgraph.Qt import QtCore, QtGui 

plt = pg.plot() 
plt.setWindowTitle('pyqtgraph example: Legend') 
plt.addLegend() 

c1 = plt.plot([1,3,2,4], pen='r', name='red plot') 
c2 = plt.plot([2,1,4,3], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='green plot') 
c3 = plt.addLine(y=4, pen='y') 
# TODO: add legend item indicating "maximum value" 

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

내가 결과적으로 얻을 것은 : plot image

내가 적절한 범례 항목을 추가하려면 어떻게

나는 보여주기 위해 예제 코드를 적용했습니다?

이 예를 들어

답변

7

pyqtgraph가 자동으로가 "이름으로 작성된 경우 범례에 항목을 추가 "매개 변수.

c3 = plt.plot (y=4, pen='y', name="maximum value") 

을 즉시 당신이 자체적으로 따라 범례 항목을 만듭니다 곡선의 이름으로 pyqtgraph을 제공하기 때문에 다음과 같이 위의 코드에서 필요한 경우에만 조정이 될 것입니다. 커브를 만들기 전에 plt.addLegend()에 전화하는 것이 중요합니다.

+0

** ** plt.addLegend()를 호출하기 전에 ** 감사합니다. 나는 거의 그것을 놓쳤습니다. :) –

5

올바른 색상으로 빈 PlotDataItem을 만들 수 있으며,이 같은 전설에 추가 :

style = pg.PlotDataItem(pen='y') 
plt.plotItem.legend.addItem(l, "maximum value")