[PYTHON] Draw a graph with PyQtGraph Part 5-Increase the Y-axis

Part 5 Increase the Y axis

5.png

Delete comments up to part 4

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)
        
        pw = pg.PlotWidget(viewBox = pg.ViewBox(border = pg.mkPen(color='#000000'),
                                                invertX = False, invertY = False))

        self.setCentralWidget(pw)

        pw.setBackground("#FFFFFF00")

        pw.setMinimumSize(500, 400)
        pw.setMaximumSize(500, 400)

        ##5 Create 3 plotItems and ViewBoxes and a 3rd Y-axis
        ##Set GraphFrame
        ##Combine plotItem and 3 ViewBoxes and 3rd Y-axis into one graph
        p1 = pw.plotItem
        p2 = pg.ViewBox()
        p3 = pg.ViewBox()
        ax3 = pg.AxisItem(orientation = 'right')
        self.setGraphFrameFont(p1, p2, p3, ax3)
        self.setGraphMultipleAxis(p1, p2, p3, ax3)

        ##Set the 5-axis label
        p1.setLabels(bottom = "X Axis", 
                     left = "Y1 Axis", 
                     right = "Y2 Axis")
        ax3.setLabel("Y3 Axis")

        ##Set the font for the 5-axis label
        fontCss = {'font-family': "Times New Roman,Meiryo", 'font-size': '10.5pt', "color": "black"}
        p1.getAxis('bottom').setLabel(**fontCss)
        fontCss["color"] = "red"
        p1.getAxis('left').setLabel(**fontCss)
        fontCss["color"] = "blue"
        p1.getAxis('right').setLabel(**fontCss)
        fontCss["color"] = "green"
        ax3.setLabel(**fontCss)

        ##5 Set the range
        p1.setRange(xRange = (-2, 6), yRange = (-2, 6), padding = 0)
        p2.setRange(yRange = (-1, 5), padding = 0)
        p3.setRange(yRange = (0, 4), padding = 0)

        ##5 Set the scale
        p1.getAxis('bottom').setTickSpacing(major = 2.5, minor = 1)
        p1.getAxis('left').setTickSpacing(major = 2.5, minor = 1)
        p1.getAxis('right').setTickSpacing(major = 2.5, minor = 1)
        ax3.setTickSpacing(major = 2.5, minor = 1)
        
        ##5 Draw a graph
        p1.addItem(pg.PlotCurveItem(x = [0, 1, 2, 3 ,4], 
                                    y = [0, 1, 2, 3 ,4], 
                                    pen = pg.mkPen(color = "r", style = Qt.SolidLine), 
                                    name = "plot1", 
                                    antialias = True))
        p2.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,
                                      name = "plot2", 
                                      antialias = True))
        p3.addItem(pg.PlotCurveItem(x = [0, 1, 2, 3 ,4], 
                                    y = [0, 4, 0, 4, 0], 
                                    pen = pg.mkPen(color = "g", style = Qt.DashLine),
                                    name = "plot3", 
                                    antialias = True))


    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.5 * 10.5)
        p1.getAxis('left').setWidth(4 * 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 * 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 * 10.5)

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

    ##5 Make multiple Y-axis graphs
    def setGraphMultipleAxis(self, p1, p2, p3 = None, ax3 = None):
        p1.showAxis('right')
        p1.scene().addItem(p2)
        p1.getAxis('right').linkToView(p2)
        p2.setXLink(p1)

        p2.sigRangeChanged.connect(lambda: p2.setGeometry(p1.vb.sceneBoundingRect()))
        
        if p3 != None and ax3 != None:
            spacer = QGraphicsWidget()
            spacer.setMaximumSize(15,15)
            p1.layout.addItem(spacer, 2, 3)

            p1.layout.addItem(ax3, 2, 4)
            p1.scene().addItem(p3)
            ax3.linkToView(p3)
            p3.setXLink(p1)
            
            p3.sigRangeChanged.connect(lambda: p3.setGeometry(p1.vb.sceneBoundingRect()))

Recommended Posts

Draw a graph with PyQtGraph Part 5-Increase the Y-axis
Draw a graph with PyQtGraph Part 1-Drawing
Draw a graph with PyQtGraph Part 3-PlotWidget settings
Draw a graph with PyQtGraph Part 4-PlotItem settings
Draw a graph with PyQtGraph Part 6-Displaying a legend
Draw a graph with Julia + PyQtGraph (2)
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with PyQtGraph Part 2--Detailed plot settings
Draw a graph with NetworkX
Draw a graph with networkx
Draw a loose graph with matplotlib
Draw a graph with pandas + XlsxWriter
Draw a graph with PySimple GUI
Count the maximum concatenated part of a random graph with NetworkX
Draw a flat surface with a matplotlib 3d graph
Draw a graph with Japanese labels in Jupyter
Tweet the weather forecast with a bot Part 2
How to draw a 2-axis graph with pyplot
Draw a graph by processing with Pandas groupby
[Python] Draw a directed graph with Dash Cytoscape
Increase the font size of the graph with matplotlib
Draw a graph with matplotlib from a csv file
[Python] How to draw a line graph with Matplotlib
Create a heatmap with pyqtgraph
Study math with Python: Draw a sympy (scipy) graph with matplotlib
[pyqtgraph] Add region to the graph and link it with the graph region
[Visualization] I want to draw a beautiful graph with Plotly
Play with a turtle with turtle graphics (Part 1)
Find the optimal value of a function with a genetic algorithm (Part 2)
[Statistics] Grasp the image of the central limit theorem with a graph
Draw a beautiful circle with numpy
Output the call graph with PyCallGraph
I tried using "Asciichart Py" which can draw a beautiful graph on the console with Python.
Let's make a graph with python! !!
Make a nice graph with plotly
Easily draw a map with matplotlib.basemap
Every time I draw a graph with python, I check it, so I will summarize only the simplest usage
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
(Matplotlib) I want to draw a graph with a size specified in pixels
Deb package development with Git: (Part 1) Creating a stray package with the minimum configuration
How to draw a bar graph that summarizes multiple series with matplotlib
Draw a graph that can be moved around with HoloViews and Bokeh
Tweet the weather forecast with a bot
[Python] Set the graph range with matplotlib
[pyqtgraph] Set the size ratio of the graph
[PyQt] Display a multi-axis graph with QtChart
How to draw a graph using Matplotlib
Draw a heart in Ruby with PyCall
Create a graph using the Sympy module
Search the maze with the python A * algorithm
Draw a heart in Python Part 2 (SymPy)
Draw a Mandelbrot set with Brainf * ck
Simply draw a graph by specifying a file
Create a graph with borders removed with matplotlib
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
[pyqtgraph] Understand SignalProxy and create a crosshair that follows the cursor on the graph
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Let's draw the voltage of the digital multimeter 34461A of the measuring instrument Keysight with CircuitPython
Get the stock price of a Japanese company with Python and make a graph
A model that identifies the guitar with fast.ai