change axis linewidth, axis font and ticks style

7,539 views
Skip to first unread message

damia...@gmail.com

unread,
Jan 17, 2013, 12:03:22 PM1/17/13
to pyqt...@googlegroups.com
Hi everyone, I have a question about how I can change axis  linewidth, axis font and axis ticks style...  for each plot in the example below
Have you some example about that?

Thank you very much in advance

Damián

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg


app = QtGui.QApplication([])

x = np.linspace(-50, 50, 1000)
y = np.sin(x) / x

win = pg.GraphicsWindow(title="View Linking Examples")
win.resize(800,600)

win.addLabel("Linked Views", colspan=2)
win.nextRow()

p1 = win.addPlot(x=x, y=y, name="Plot1", title="Plot1")
p2 = win.addPlot(x=x, y=y, name="Plot2", title="Plot2: Y linked with Plot1")
p2.setLabel('bottom', "Label to test offset")
p2.setYLink('Plot1')  ## test linking by name


## create plots 3 and 4 out of order
p4 = win.addPlot(x=x, y=y, name="Plot4", title="Plot4: X -> Plot3 (deferred), Y -> Plot1", row=2, col=1)
p4.setXLink('Plot3')  ## Plot3 has not been created yet, but this should still work anyway.
p4.setYLink(p1)
p3 = win.addPlot(x=x, y=y, name="Plot3", title="Plot3: X linked with Plot1", row=2, col=0)
p3.setXLink(p1)
p3.setLabel('left', "Label to test offset")
#QtGui.QApplication.processEvents()


## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Luke Campagnola

unread,
Jan 17, 2013, 12:47:52 PM1/17/13
to pyqt...@googlegroups.com
On Thu, Jan 17, 2013 at 12:03 PM, <damia...@gmail.com> wrote:
Hi everyone, I have a question about how I can change axis  linewidth, axis font and axis ticks style...  for each plot in the example below
Have you some example about that?

AxisItem has a setPen() method that you can use to change the color and line width of the axis line and ticks as well as the text color. 
For example:

    pen = pg.mkPen(color=(255,255,200), width=1)
    plotWidget.plotItem.getAxis('left').setPen(pen)
    plotWidget.plotItem.getAxis('bottom').setPen(pen)

AxisItem also has a 'tickLength' attribute which determines how long (and in which direction) the ticks should be. 

Does that answer your question? I'm not sure what you mean by 'tick style'. 

Luke



 

JHallas

unread,
Jan 17, 2013, 1:31:10 PM1/17/13
to pyqt...@googlegroups.com
Oddly enough I came here to ask about the axis font style.  I can use html to change the title and axes using setTitle or setLabel and <font size="5">AxisLabel</font>, but the font size of the units on the axes don't change and I would like to set the font size in pixels.  On the title, it works with

setTitle('Title',size='10px')

but if I try a similar thing with 

setLabel('bottom','BottomAxisLabel',size='10px')

the axis label disappears until I clear the plot.  I've tried 

a=window.getAxis('bottom')
a.font.setPixelSize(10)
but a.font.pixelSize() doesn't change and the font size on the plot doesn't change

I then tried 
b=QtGui.QFont()
b.setPixelSize(10)
a.setFont(b)

This correctly sets a.font.pixelSize() to 10, but the axis font size doesn't actually change.  I'm sure there's a simple solution I'm not seeing.

Thanks for your help in advance

damia...@gmail.com

unread,
Jan 17, 2013, 2:22:48 PM1/17/13
to pyqt...@googlegroups.com
Thank very much!!  It works perfect

Damián

Luke Campagnola

unread,
Jan 17, 2013, 3:36:20 PM1/17/13
to pyqt...@googlegroups.com
On Thu, Jan 17, 2013 at 1:31 PM, JHallas <jmha...@gmail.com> wrote:
Oddly enough I came here to ask about the axis font style.  I can use html to change the title and axes using setTitle or setLabel and <font size="5">AxisLabel</font>, but the font size of the units on the axes don't change and I would like to set the font size in pixels.  On the title, it works with

setTitle('Title',size='10px')

but if I try a similar thing with 

setLabel('bottom','BottomAxisLabel',size='10px')

the axis label disappears until I clear the plot.

The way the axis labels are implemented, the final HTML looks like:

    <span style="(style options)">Label Text (Units)</span>

The style options are constructed from the extra keyword arguments given to setLabel. 
So you just need to make sure the options you give are valid CSS (which is probably why the label disappeared).

    labelStyle = {'color': '#FFF'; 'font-size': '16px'}
    plt.setLabel('bottom', 'Label Text', 'Units', **labelStyle)

I'll update the documentation to make all of these issues more clear.

Luke

JHallas

unread,
Jan 18, 2013, 5:09:21 PM1/18/13
to pyqt...@googlegroups.com
That worked great, thanks.  Is there any easy way to axis the font size for the tick values?

JHallas

unread,
Jan 21, 2013, 6:28:30 PM1/21/13
to pyqt...@googlegroups.com
Just noticed that my question makes no sense.  How would I go about modifying the font size for the tick values on the axis?

Thanks,
J.

Luke Campagnola

unread,
Jan 21, 2013, 11:42:11 PM1/21/13
to pyqt...@googlegroups.com
On Mon, Jan 21, 2013 at 6:28 PM, JHallas <jmha...@gmail.com> wrote:
> How would I go about
> modifying the font size for the tick values on the axis?

Hmmmmm.
You can't. At least, not without modifying AxisItem.drawPicture().
Around line 625, there is a comment that looks like

## Draw text until there is no more room (or no more text)

You can add a call to p.setFont() there to set the font size. It's an
easy change; I'll try to get it into the next release.


Luke

JHallas

unread,
Jan 22, 2013, 12:18:49 AM1/22/13
to pyqt...@googlegroups.com
Thanks a lot for all of your help.  I appreciate it greatly. Your software package has made my work go much quicker and resulted in a much better looking and working program than if I had started from scratch.  

pranav parashar

unread,
Apr 2, 2014, 8:27:44 AM4/2/14
to pyqt...@googlegroups.com
well there is one way to directly change the font size for an axis item:
b=QtGui.QFont()

b.setPixelSize(20)

x_axisitem.tickFont = b

Upol Ryskulova

unread,
Apr 13, 2015, 9:01:21 AM4/13/15
to pyqt...@googlegroups.com
Hello Luke,
Sorry for opening this old issue, But I am facing with the same problem. I was not able to change the font of the tickValues. Right now I am using your latest release. 
Could you please guide me. I overwrited the AxisItem tickValues function and displaying time axis. 
       fontForTickValues = QtGui.QFont()
       fontForTickValues.setPixelSize(50)
       fontForTickValues.setBold(True)
       def addUpperLowerGraphs(self):
                self.upperName = self.l2.addLabel(vmicText)
                self.axis = TimeAxisItem('bottom')
                self.axis.setStyle(tickFont = fontForTickValues)
                self.axis.setPen(axisPen)
                self.upperGraph = self.l2.addPlot(name = "upper")
                self.upperGraph.showGrid(x=True,y=True)
                self.upperGraph.setContentsMargins(0.0,20.0,0.0,0.0)
                self.l2.nextRow()

                self.lowerName = self.l2.addLabel(quraText)
                self.lowerGraph = self.l2.addPlot(name = "lower",axisItems={'bottom':self.axis})


But unfortunately I cannot see any change in tick values' font.
I'd be very glad for your advice.
Thanks you.
Regards,
Upol

Upol Ryskulova

unread,
Apr 16, 2015, 11:06:59 AM4/16/15
to pyqt...@googlegroups.com
Hello again,
I was able to change tickFont as pranav parashar did. But now I have encountered with different problem. When I enlarged the tickFont, tick values are conciding with axis line.
Attached image.
image1.png

Justin Hallas

unread,
Apr 16, 2015, 1:12:55 PM4/16/15
to pyqt...@googlegroups.com

I believe there is a padding parameter that you have to modify to push text away from axis. I don't have access to my code currently but I'll update tomorrow if you haven't got it by then.


--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/jS1Ju8R6PXk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/f774fbd5-8077-4bc7-b446-89b70d8b7227%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Upol Ryskulova

unread,
Apr 17, 2015, 2:25:35 AM4/17/15
to pyqt...@googlegroups.com
Hello Justin,
I will be very glad if you share your solution.

Justin Hallas

unread,
Apr 17, 2015, 4:46:26 PM4/17/15
to pyqt...@googlegroups.com
w1 = pg.PlotWidget()
w1.plot(X,Y)

labelStyle = {'color': '#FFF', 'font-size': '14px'}

w1.setLabel('bottom', 'X', **labelStyle)
w1.setLabel('left', 'Y', **labelStyle)

w1.getAxis('bottom').setHeight(int(14 * 1.7 + 5))
w1.getAxis('left').setWidth(int(14 * 2.6 + 8))
It's kind of kluge-y but it works.   The formula inside of the setHeight and setWidth is just something I came up with after experimenting to keep the text away from the axis.  It only works over a certain range of font sizes.


On Thu, Apr 16, 2015 at 11:25 PM Upol Ryskulova <upol.ry...@gmail.com> wrote:
Hello Justin,
I will be very glad if you share your solution.

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/jS1Ju8R6PXk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.

Upol Ryskulova

unread,
Apr 18, 2015, 2:41:36 AM4/18/15
to pyqt...@googlegroups.com
Thank you Justin. 
Yesterday I did it by using this axis.setStyle(tickTextOffset=13). 
(for ones that will come across with the same problem)

Reply all
Reply to author
Forward
0 new messages