[PYTHON] Draw a graph with PyQtGraph Part 4-PlotItem settings

Part 4 PlotItem settings

Set the axis label, range, and scale.

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 Create a PlotWidget
        #3 Set the border and axis direction of PlotWidget
        pw = pg.PlotWidget(viewBox = pg.ViewBox(border = pg.mkPen(color='#000000'),
                                                invertX = False, invertY = True))

        #1 Set the widget in the window
        self.setCentralWidget(pw)

        #3 Set the background color(#FFFFFF00 : Transparent)
        pw.setBackground("#FFFFFF00")

        ##3 Fix the size of the graph
        pw.setMinimumSize(500, 400)
        pw.setMaximumSize(500, 400)

        #1 Call plotItem
        ##4 Set Graph Frame
        p1 = pw.plotItem
        self.setGraphFrameFont(p1)

        ##Set the 4-axis label
        p1.setLabels(bottom = "X Axis", 
                     left = "Y1 Axis")

        ##Set the font for 4-axis labels
        fontCss = {'font-family': "Times New Roman,Meiryo", 'font-size': '10.5pt', "color": "black"}
        p1.getAxis('bottom').setLabel(**fontCss)

        ##4 Set the graph range
        ##If you want to make a margin, change the padding value
        p1.setRange(xRange = (-2, 6), yRange = (-2, 6), padding = 0)

        ##Set the 4-axis scale
        p1.getAxis('bottom').setTickSpacing(major = 2.5, minor = 1)

        #1 Draw a scatter plot and a line graph
        #2 Set the plot details
        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 Set the color and font of the graph scale and the position of the axis label.
    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__':
    #Create a Qt Application
    app = QApplication(sys.argv)
    #Create and display form
    mainWin = GraphWindow()
    mainWin.show()
    #Start the main loop of Qt
    sys.exit(app.exec_())

Recommended Posts

Draw a graph with PyQtGraph Part 4-PlotItem settings
Draw a graph with PyQtGraph Part 3-PlotWidget settings
Draw a graph with PyQtGraph Part 2--Detailed plot settings
Draw a graph with PyQtGraph Part 1-Drawing
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 5-Increase the Y-axis
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
Draw a flat surface with a matplotlib 3d graph
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
Draw a graph with matplotlib from a csv file
[Python] How to draw a line graph with Matplotlib
Create a heatmap with pyqtgraph
[Visualization] I want to draw a beautiful graph with Plotly
Play with a turtle with turtle graphics (Part 1)
Draw a beautiful circle with numpy
Let's make a graph with python! !!
Make a nice graph with plotly
Easily draw a map with matplotlib.basemap
Count the maximum concatenated part of a random graph with NetworkX
[PyQt] Display a multi-axis graph with QtChart
How to draw a graph using Matplotlib
(Matplotlib) I want to draw a graph with a size specified in pixels
Draw a heart in Ruby with PyCall
Draw a heart in Python Part 2 (SymPy)
Draw a Mandelbrot set with Brainf * ck
How to draw a bar graph that summarizes multiple series with matplotlib
Simply draw a graph by specifying a file
Create a graph with borders removed with matplotlib
Draw a graph of a quadratic function in Python
How to draw a 3D graph before optimization
Draw a "breast curved surface" in a 3D graph (1)
A memo that made a graph animated with plotly
Try to draw a life curve with python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Draw a "breast curved surface" in a 3D graph (2)
I made a random number graph with Numpy
Draw multiple photos in a graph from multiple folders
Draw a graph in Julia ... I tried a little analysis
[Python] Draw a Qiita tag relationship diagram with NetworkX
I tried to draw a route map with Python
Visualize railway line data as a graph with Cytoscape 2
"My Graph Generation Application" by Python (PySide + PyQtGraph) Part 2
Create a 3D model viewer with PyQt5 and PyQtGraph
"My Graph Generation Application" by Python (PySide + PyQtGraph) Part 1
[Python] How to draw a scatter plot with Matplotlib
Data visualization with Python-It's too convenient to draw a graph by attribute with "Facet" at once