How to replace ticks with explicite multiples of pi

54 views
Skip to first unread message

Blafardman Breton

unread,
Sep 22, 2017, 9:43:12 AM9/22/17
to pyqtgraph
I want to display multiples of π on yaxis of my plot. I suceeded to space the ticks by 50π but i didn't find in the documentation how to associate the ticks float values (ex: 6.28...) i obtain with a string containing the corresponding factor of π (ex: 2π). No more lucky on the web...
So here is my code :

import pyqtgraph as pg
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

self.win = pg.GraphicsWindow()
self.win.resize(1100, 800)
self.win.ci.layout.setColumnFixedWidth(0, 1000)
self.win.ci.layout.setRowFixedHeight(0, 550)
self.win.ci.layout.setRowFixedHeight(1, 200)

PLOT1 = self.win.addPlot(title=titre, row=0, col=0)
if self.echel == 1:
    PLOT1.plot(freq_lim, periodo_WK_module_lim, pen=pg.mkPen((255, 192, 0), width=2))
elif self.echel == 2:
    PLOT1.plot(freq_lim, 20*np.log10(periodo_WK_module_lim), pen=pg.mkPen((255, 192, 0), width=2))
PLOT1.setLabel("bottom", xlabel1)
PLOT1.setLabel("left", ylabel1)
PLOT1.showGrid(x=True, y=True)
PLOT1.showAxis("top", show=True)
PLOT1.getAxis("top").setStyle(showValues=False)
PLOT1.showAxis("right", show=True)
PLOT1.getAxis("right").setStyle(showValues=False)
PLOT1.showButtons()

PLOT2 = self.win.addPlot(row=1, col=0)
PLOT2.plot(freq_lim, periodo_WK_phase_lim, pen=pg.mkPen((255, 192, 0), width=2))
PLOT2.setLabel("left", ylabel2)
PLOT2.getAxis("left").setTickSpacing(50*np.pi, 1)
PLOT2.showGrid(x=True, y=True)
PLOT2.showAxis("bottom", show=True)
PLOT2.getAxis("bottom").setStyle(showValues=False)
PLOT2.showAxis("top", show=True)
PLOT2.getViewBox().linkView(PLOT1.getViewBox().XAxis, PLOT1.getViewBox())
PLOT2.showAxis("right", show=True)
PLOT2.getAxis("right").setStyle(showValues=False)
PLOT2.showButtons()

Does anybody have a solution ? Maybe it's not possible ?

Regards


Blafardman Breton

unread,
Sep 23, 2017, 9:53:59 AM9/23/17
to pyqtgraph
Hello,
I found the answer myself by reading more efficiently the doc :-( Sorry for that...
Here is my working code :

    def editFigurePyQtGraph(self):

        # Mise en place
        freq_lim = self.freq_lim
        periodo_WK_module_lim = self.periodo_WK_module_lim
        periodo_WK_phase_lim = self.periodo_WK_phase_lim
        titre = self.titre
        xlabel1 = self.xlabel1
        xlabel2 = self.xlabel2
        ylabel1 = self.ylabel1
        ylabel2 = self.ylabel2

        # Affichage du périodogramme d'interaction de WK

        self.win = pg.GraphicsWindow()
        self.win.resize(1100, 800)
        self.win.ci.layout.setColumnFixedWidth(0, 1000)
        self.win.ci.layout.setRowFixedHeight(0, 550)
        self.win.ci.layout.setRowFixedHeight(1, 200)

        PLOT1 = self.win.addPlot(title=titre, row=0, col=0)
        if self.echel == 1:
            PLOT1.plot(freq_lim, periodo_WK_module_lim, pen=pg.mkPen((255, 192, 0), width=2))
        elif self.echel == 2:
            PLOT1.plot(freq_lim, 20*np.log10(periodo_WK_module_lim), pen=pg.mkPen((255, 192, 0), width=2))
        PLOT1.setLabel("bottom", xlabel1)
        PLOT1.setLabel("left", ylabel1)
        PLOT1.showGrid(x=True, y=True)
        PLOT1.showAxis("top", show=True)
        PLOT1.getAxis("top").setStyle(showValues=False)
        PLOT1.showAxis("right", show=True)
        PLOT1.getAxis("right").setStyle(showValues=False)
        PLOT1.showButtons()

        PLOT2 = self.win.addPlot(row=1, col=0)
        PLOT2.plot(freq_lim, periodo_WK_phase_lim, pen=pg.mkPen((255, 192, 0), width=2))
        PLOT2.getAxis("left").setStyle(tickFont=QtGui.QFont('times'))
        PLOT2.setLabel("left", ylabel2)

        # Fonction pour afficher les radians
        # en multiple de π
        def format_left(base):
            for i in range(-50, 51):
                if np.isclose(0, i, 0, 1e-3):
                    ticks_setting[0].append((0, "0"))
                else:
                    ticks_setting[0].append((i*base*np.pi, "{} π".format(i*base)))

        mini = int(np.floor(np.min(periodo_WK_phase_lim)/np.pi))
        maxi = int(np.ceil(np.max(periodo_WK_phase_lim)/np.pi))
        base = int(np.around((maxi-mini)/5, decimals=-1))
        ticks_setting = [[]]
        format_left(base)

        # On affiche les radians en multiples entiers de π
        PLOT2.getAxis("left").setTickSpacing(major=None, minor=base*np.pi)
        PLOT2.getAxis("left").setTicks(ticks_setting)

        PLOT2.showGrid(x=True, y=True)
        PLOT2.showAxis("bottom", show=True)
        PLOT2.getAxis("bottom").setStyle(showValues=False)
        PLOT2.showAxis("top", show=True)
        PLOT2.getViewBox().linkView(PLOT1.getViewBox().XAxis, PLOT1.getViewBox())
        PLOT2.showAxis("right", show=True)
        PLOT2.getAxis("right").setStyle(showValues=False)
        PLOT2.showButtons()

Hope that could help...

Regards
Reply all
Reply to author
Forward
0 new messages