반응형
pyqtgraph -- timeaxis 만들어, 최근 data 만 실시간 보여주기
참고 : http://www.pyqtgraph.org/documentation/graphicsItems/axisitem.html#pyqtgraph.AxisItem.tickStrings
https://gist.github.com/iverasp/9349dffa42aeffb32e48a0868edfa32d --> 이를 바탕으로 소스 변경함.
https://stackoverflow.com/questions/31775468/show-string-values-on-x-axis-in-pyqtgraph --> custom axis 만드느법
https://gist.github.com/cpascual/cdcead6c166e63de2981bc23f5840a98
** AxisItem class 를 subclass 하여, 원하는 axis 만들수 있다.
--> 특히, tickStrings( ) 메소드를 override 하여, tick 에 원하는 양식으로 출력 변경가능하다.
** 최근 시간만 보여주기
--> setData( ) 로 실시간 chart 그리기전에, setXRange( ) 이용하여, 최근 시간만 설정하면 ok!!!
<< 실행결과 >>
<< 소스 >>
--
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtCore import QTimer | |
from PyQt5.QtWidgets import * | |
import pyqtgraph as pg | |
import time | |
class TimeAxisItem(pg.AxisItem): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.setLabel(text='Time(초)', units=None) | |
self.enableAutoSIPrefix(False) | |
def tickStrings(self, values, scale, spacing): | |
""" override 하여, tick 옆에 써지는 문자를 원하는대로 수정함. | |
values --> x축 값들 ; 숫자로 이루어진 Itarable data --> ex) List[int] | |
""" | |
# print("--tickStrings valuse ==>", values) | |
return [time.strftime("%H:%M:%S", time.localtime(local_time)) for local_time in values] | |
class ExampleWidget(QWidget): | |
def __init__(self, parent=None): | |
QWidget.__init__(self, parent) | |
self.pw = pg.PlotWidget( | |
title="Example plot", | |
labels={'left': 'y 축'}, | |
axisItems={'bottom': TimeAxisItem(orientation='bottom')} | |
) | |
hbox = QHBoxLayout() | |
hbox.addWidget(self.pw) | |
self.setLayout(hbox) | |
self.pw.setYRange(0, 70, padding=0) | |
time_data = int(time.time()) | |
self.pw.setXRange(time_data - 10, time_data + 1) # 생략 가능. | |
self.pw.showGrid(x=True, y=True) | |
# self.pw.enableAutoRange() | |
self.pdi = self.pw.plot(pen='y') # PlotDataItem obj 반환. | |
self.plotData = {'x': [], 'y': []} | |
def update_plot(self, new_time_data: int): | |
data_sec = time.strftime("%S", time.localtime(new_time_data)) | |
self.plotData['y'].append(int(data_sec)) | |
self.plotData['x'].append(new_time_data) | |
self.pw.setXRange(new_time_data - 10, new_time_data + 1, padding=0) # 항상 x축 시간을 최근 범위만 보여줌. | |
self.pdi.setData(self.plotData['x'], self.plotData['y']) | |
if __name__ == "__main__": | |
import sys | |
app = QApplication(sys.argv) | |
ex = ExampleWidget() | |
def get_data(): | |
new_time_data = int(time.time()) | |
ex.update_plot(new_time_data) | |
mytimer = QTimer() | |
mytimer.start(1000) # 1초마다 갱신 위함... | |
mytimer.timeout.connect(get_data) | |
ex.show() | |
sys.exit(app.exec_()) | |
반응형
'PyQt5' 카테고리의 다른 글
pyqtgraph -- realtime chart 그리기 (0) | 2018.11.23 |
---|---|
pyqtgraph -- bar chart 그리기, 여백 제거 (0) | 2018.11.05 |
pyqtgraph -- line chart 예제 2 (0) | 2018.11.05 |
pyqtgraph -- pyqt5 에서 사용 -- line chart (1) | 2018.11.05 |
pyqt5 -- QObject 사용하는 singleton 만들기 (0) | 2018.10.26 |