** PlotItem.enableAutoScale() 사용하면, 항상 전체 그래프 영역 모두 보여줌.

 

   -- PlotItem.enableAutoScale() 는 나중에 사라지므로, 대신에 PlotItem.enableAutoRange( ) 사용 권장함. 

 

 

** setXRange( ), setYRange( ) 로 차트의 x축, y축 가시영역을 지정해도...

   enableAutoRange( ) 사용하면, 자동으로 가시영역 변경된다..

 

 

 

** 대부분의 차트는 realtime update 시에 setData( ) 를 사용하면, 자동으로 이전차트 지우고, 새로운 data로 차트를 그린다.

    --> but, BarGraphItem 차트는 clear( ) 를 사용해서, 이전 차트 item 지우고, 새로 그려야함.

# https://freeprog.tistory.com/372?category=716617

import pyqtgraph as pg

from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, Qt, QThread, QTimer

import time, random


class ExMain(QWidget):
    def __init__(self):
        super().__init__()

        hbox = QHBoxLayout()
        self.pw1 = pg.PlotWidget(title="line chart")
        self.pw2 = pg.PlotWidget(title="이전 차트 제거 안됨")
        self.pw3 = pg.PlotWidget(title="clear 로 이전 차트 제거함")

        hbox.addWidget(self.pw1)
        hbox.addWidget(self.pw2)
        hbox.addWidget(self.pw3)
        self.setLayout(hbox)

        # self.setGeometry(300, 100, 800, 500)  # x, y, width, height
        self.setWindowTitle("pyqtgraph 예제 - realtime")

        self.x = [1, 2, 3]
        self.y = [4, 5, 6]
        # self.pw1.setXRange(1, 10)
        # self.pw1.setYRange(1, 10)
        # self.pw1.enableAutoScale()
        # self.pw1.enableAutoRange()  # x,y축 모두 autorange..
        # self.pw1.enableAutoRange(axis='x', enable=True)
        # self.pw1.enableAutoRange(axis='x')
        # self.pw1.enableAutoRange(axis='y')
        # self.pw1.enableAutoRange(axis='xy')

        # self.pw2.enableAutoRange()
        self.pl = self.pw1.plot(pen='g')

        self.mytimer = QTimer()
        self.mytimer.start(1000)  # 1초마다 차트 갱신 위함...
        self.mytimer.timeout.connect(self.get_data)

        self.draw_chart(self.x, self.y)
        self.show()

    def draw_chart(self, x, y):
        self.pl.setData(x=x, y=y)  # line chart 그리기

        cnt = len(y)
        new_y = []
        for i in range(cnt):
            new_y.append(random.random()*60)  # 0 이상 ~ 60 미만 random 숫자 만들기

        """ 이전에 그린 차트 잔상 남아있다 """
        bar_chart = pg.BarGraphItem(x=x, height=new_y, width=1, brush='y', pen='r', title="잔상남음")
        self.pw2.addItem(bar_chart)

        """ clear() 로 이전에 그린 차트 제거함. """
        self.pw3.clear()  # remove all items --> 이전 그래프 제거함.
        bar_chart2 = pg.BarGraphItem(x=x, height=new_y, width=1, brush='y', pen='r')
        self.pw3.addItem(bar_chart2)

    @pyqtSlot()
    def get_data(self):
        # print(time.localtime())
        # print(time.strftime("%H%M%S", time.localtime()))
        data: str = time.strftime("%S", time.localtime())  # 초 단위만 구함.

        last_x = self.x[-1]
        self.x.append(last_x + 1)

        self.y.append(int(data))
        self.draw_chart(self.x, self.y)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)

    ex = ExMain()

    sys.exit(app.exec_())

** bar char 그리기

    -- BarGraphItem( ) 사용하면됨.

       --> pyqtgraph 문서에는 설명 안나옴.  소스 주석에 사용법 있음.

 

 

 ** x축, y축 과  (0, 0) 사이 여백 제거하기

 

    setXRange(0, 22, padding=0)   # padding=0  --> 공백 제거함.

 



출처: https://freeprog.tistory.com/368?category=716617 [취미로 하는 프로그래밍 !!!]

 

 

#https://freeprog.tistory.com/368?category=716617

import pyqtgraph as pg
import numpy as np
from PyQt5.QtWidgets import *


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        pg.setConfigOptions(background='w', foreground='b')  # 배경 흰색으로.. ; global configuration options

        x = np.arange(20)
        y = np.linspace(0, 20, num=20)

        pw = pg.PlotWidget()
        pw.showGrid(x=True, y=True)

        pw.setXRange(0, 22, padding=0)   # padding=0  --> y축 공백 제거함.
        # pw.setYRange(0, 22, padding=0)   # padding=0  --> x축 공백 제거함.

        barchar = pg.BarGraphItem(x=x, height=y, width=0.6, brush='r')  # width = 막대 넓이
        # bar_chart = pg.BarGraphItem(x=x, width=0.9, height=y,  brush='r', pen='g')  
        pw.addItem(barchar)

        layout = QHBoxLayout()
        layout.addWidget(pw)
        self.setLayout(layout)

        self.setGeometry(300, 100, 550, 500)  # x, y, width, height
        self.setWindowTitle("pyqtgraph 예제 - bar chart")
        self.show()


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    ex = MyWidget()
    sys.exit(app.exec_())

+ Recent posts