Search code examples
pythonpython-3.xpyqtpyqt4pyqtgraph

Modify graphic grid


How can I modify the grid of my graphic to taste? To make the pictures bigger or smaller.

I want to modify the tick and the subtitles.

Currently my graphic is like this.

enter image description here

But I need it to be like this.

enter image description here

This is all the code of my program.

# -*- coding: utf-8 -*-

try:
    from PySide import QtWidgets
except:
    from PyQt5 import QtWidgets


class ECG1:
    def __init__(self):

..........................


Solution

  • I guessed your tick spacings, you might have to adjust them:

    import pyqtgraph as pg
    import numpy as np
    
    if __name__ == '__main__':
    
        app = pg.mkQApp()
        pg.setConfigOption('background', 'w')
        pg.setConfigOption('foreground', 'k')
        pw = pg.PlotWidget()
        plotItem = pw.getPlotItem()
    
        axBottom = plotItem.getAxis('bottom') #get x axis
        xTicks = [0.2, 0.04]
        axBottom.setTickSpacing(xTicks[0], xTicks[1]) #set x ticks (major and minor)
        axLeft = plotItem.getAxis('left') #get y axis
        yTicks = [20000, 4000]
        axLeft.setTickSpacing(yTicks[0], yTicks[1]) #set y ticks (major and minor)
        plotItem.showGrid(x=True, y=True, alpha=0.3)
        plotItem.vb.state['aspectLocked'] = yTicks[0]/xTicks[0] # lock aspect ratio (major ticks form a square)
    
        # plot an unhealthy cardiogram
        x = np.linspace(0,5,1000)
        pw.plot(x,np.sin(x*10)*30000, pen='k')
        pw.show()
        app.exec()
    

    Result: result

    Edit:

    class ExampleApp(QtGui.QMainWindow, ui_main.Ui_MainWindow):
        def __init__(self, parent=None):
            pyqtgraph.setConfigOption('background', 'w') #before loading widget, fondo de la gráfica.
    
            super(ExampleApp, self).__init__(parent)
            self.setupUi(self)
        #### Insert the code here
            plotItem = self.grECG.plotItem
            axBottom = plotItem.getAxis('bottom') #get x axis
            xTicks = [0.2, 0.04]
            axBottom.setTickSpacing(xTicks[0], xTicks[1]) #set x ticks (major and minor)
            axLeft = plotItem.getAxis('left') #get y axis
            yTicks = [20000, 4000]
            axLeft.setTickSpacing(yTicks[0], yTicks[1]) #set y ticks (major and minor)
            plotItem.showGrid(x=True, y=True, alpha=0.3)
            plotItem.vb.state['aspectLocked'] = yTicks[0]/xTicks[0] # lock aspect ratio (major ticks form a square)
        #### Continue with your code
            #self.grECG.plotItem.setStyle(tickLength=1) #línea de walter
            #self.grECG.plotItem.setScale(2.0) #cambia la escala de la ventana de muestreo.
            #etc