0

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):

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

1 Answers1

1

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
Jonas
  • 1,838
  • 4
  • 19
  • 35
  • Hello, thank you very much for your response, it is exactly what I should do, but excuse the question, how could I implement that in my code? I'm really new, I tried it but it gives me an error. " File "go.py", line 30 xTicks = [0.2, 0.04] ^ IndentationError: unexpected indent" – Antonio vg Jan 28 '19 at 01:43
  • Jonas, Please help :( – Antonio vg Jan 28 '19 at 15:30
  • @Antoniovg I added where to insert the code to my answer. (assuming `self.grECG.plotItem` is a [PlotItem](http://www.pyqtgraph.org/documentation/graphicsItems/plotitem.html)) – Jonas Jan 28 '19 at 18:50
  • Hello, thank you very much for your response, I added the code where you indicated me. But I get this error: File "go.py", line 28 plotItem = self.grECG.plotItem ^ IndentationError: unexpected indent – Antonio vg Jan 28 '19 at 22:10
  • Like the error says, you have an indent, where there shouldnt be one. You didnt post all your code, but you might have to remove the indentation before all lines starting with `class ExampleApp...`. [indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) – Jonas Jan 28 '19 at 22:20
  • The self-adjustment is deactivated because you have a fixed aspect ratio now, so the y-range is defined by the x-range (or the other way around, depending on the size of the plot) if you remove the line `plotItem.vb.state['aspectLocked'] = yTicks[0]/xTicks[0]` you see what i mean. – Jonas Jan 28 '19 at 22:55
  • @Antoniovg if thats the answer you where looking for, please accept it ;) – Jonas Jan 29 '19 at 17:15
  • Hi yes, you solved the problem in an exact and great way, what should I do to mark this question as resolved? – Antonio vg Jan 30 '19 at 02:13
  • @Antoniovg click on the checkmark next to my answer ;) – Jonas Jan 30 '19 at 16:11
  • Hello, excuse one last question, do you know how I could compile these five files into one .exe? I try but I do not succeed. – Antonio vg Feb 02 '19 at 02:08
  • I usually use pyinstaller. [This](https://stackoverflow.com/questions/47769904/size-of-executable-using-pyinstaller-and-numpy) and [this](https://stackoverflow.com/questions/47532133/exe-does-not-find-dlls-using-pyinstaller-with-pyqt) questions that I asked (and their answers) explain my workflow pretty well. If you still have a problem you should open a new question with all the information: whats your error, code etc. I suggest you start with turning a very simple program (eg that just shows an empty window) into an executable.. if that works you can continue from there – Jonas Feb 02 '19 at 12:28
  • @Antoniovg are you trying to create an executable from the [Serial Port Plotter](https://github.com/CieNTi/serial_port_plotter), that you have linked in your profile? – Jonas Feb 02 '19 at 12:40