Help us understand the problem. What is going on with this article?

PyQtGraphでグラフを描写する その4 - PlotItemの設定

More than 3 years have passed since last update.

その4 PlotItemの設定

軸ラベル、レンジ、目盛りを設定する。

4.png

import sys
from PySide.QtCore import *
from PySide.QtGui import *
import pyqtgraph as pg


class GraphWindow(QMainWindow):

    def __init__(self, parent = None):

        super(GraphWindow, self).__init__(parent)

        # 1 PlotWidgetを作成する
        # 3 PlotWidgetの枠線、軸の方向を設定する
        pw = pg.PlotWidget(viewBox = pg.ViewBox(border = pg.mkPen(color='#000000'),
                                                invertX = False, invertY = True))

        # 1 ウィンドウにウィジェットを設定する
        self.setCentralWidget(pw)

        # 3 背景色を設定する(#FFFFFF00 : Transparent)
        pw.setBackground("#FFFFFF00")

        ## 3 グラフのサイズを固定する
        pw.setMinimumSize(500, 400)
        pw.setMaximumSize(500, 400)

        # 1 plotItemを呼び出す
        ## 4  GraphFrameを設定する
        p1 = pw.plotItem
        self.setGraphFrameFont(p1)

        ## 4 軸ラベルを設定する
        p1.setLabels(bottom = "X Axis(えっくす軸)", 
                     left = "Y1 Axis(わい1軸)")

        ## 4 軸ラベルのフォントを設定する
        fontCss = {'font-family': "Times New Roman, メイリオ", 'font-size': '10.5pt', "color": "black"}
        p1.getAxis('bottom').setLabel(**fontCss)

        ## 4 グラフのレンジを設定する
        ##   余白を作る場合、paddingの値を変更する
        p1.setRange(xRange = (-2, 6), yRange = (-2, 6), padding = 0)

        ## 4 軸の目盛りを設定する
        p1.getAxis('bottom').setTickSpacing(major = 2.5, minor = 1)

        # 1 散布図と折れ線グラフを描写する
        # 2 プロットの詳細を設定する
        p1.addItem(pg.PlotCurveItem(x = [0, 1, 2, 3 ,4], 
                                    y = [0, 1, 2, 3 ,4], 
                                    pen = pg.mkPen(color = "r", style = Qt.SolidLine),
                                    antialias = True))
        p1.addItem(pg.ScatterPlotItem(x = [0, 1, 2, 3 ,4], 
                                      y = [4, 3, 2, 1, 0], 
                                      symbol = "x", 
                                      pen = pg.mkPen(None), 
                                      brush = pg.mkBrush("b"),
                                      size = 7.5,
                                      antialias = True))

    ## 4 グラフの目盛りの色とフォント、軸ラベルの位置を設定する
    def setGraphFrameFont(self, p1, p2 = None, p3 = None, ax3 = None):
        font = QFont("Times New Roman", 7)

        p1.getAxis('bottom').setPen(pg.mkPen(color='#000000'))
        p1.getAxis('left').setPen(pg.mkPen(color='#000000'))
        p1.getAxis('bottom').setHeight(3 * 10.5)
        p1.getAxis('left').setWidth(4.5 * 10.5)

        #p1.getAxis('bottom').setLabel(**self.fontCss)
        p1.getAxis('bottom').tickFont = font
        #p1.getAxis('left').setLabel(**self.fontCss)
        p1.getAxis('left').tickFont = font

        if p2 != None:
            p1.getAxis('right').setPen(pg.mkPen(color='#000000'))
            p1.getAxis('right').setWidth(4.5 * 10.5)

            #p1.getAxis('right').setLabel(**self.fontCss)
            p1.getAxis('right').tickFont = font

        if p3 != None and ax3 != None:
            ax3.setPen(pg.mkPen(color='#000000'))
            ax3.setWidth(4.5 * 10.5)

            #ax3.setLabel(**self.fontCss)
            ax3.tickFont = font


if __name__ == '__main__':
    # Qt Applicationを作ります
    app = QApplication(sys.argv)
    # formを作成して表示します
    mainWin = GraphWindow()
    mainWin.show()
    # Qtのメインループを開始します
    sys.exit(app.exec_())
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away