Change Legend font size

2,801 views
Skip to first unread message

Jan

unread,
Mar 20, 2016, 10:25:54 AM3/20/16
to pyqtgraph
Hi pyqtgraph users

I am currently creating a line plot using the following sequence:

app  = pg.QtGui.QApplication([])
main_window = pg.QtGui.QMainWindow()
main_window.setGeometry(10,40,1380,800)
rhs_widget = pg.QtGui.QWidget(main_window)
rhs_widget.setGeometry(1200,0,180,800)
plot_window = pg.GraphicsLayoutWidget(main_window)
plot_window.setGeometry(0,0,1200,800)

I then add plot panels to the main_window using, for example:

    p1 = plot_window.addPlot(col=1,row=1)
    p1.setTitle('<font size="3">Active Power</font>',**titleStyle)
    p1.setLabel(axis='left',text='<font size="3">Power (kW) </font>')
    p1.setLabel(axis='top',)
    p1.setLabel(axis='bottom',text='<font size="3">Time elapsed (s)</font>')
    p1.setYRange(00,2500)
    a = p1.getAxis('top')
    a.tickFont = b
    a.showValues='false'
    a = p1.getAxis('bottom')
    a.tickFont = b
    p1.showAxis('left')
    a = p1.getAxis('left')
    a.tickFont = b
    p1.showAxis('right')
    a = p1.getAxis('right')
    a.tickFont = b
    p1.showLabel('left',show=True)
    p1.showLabel('right',show=True)
    p1.showGrid(x=True,y=True,alpha=0.5)
    p1.addLegend(size=(10,30),offset=(10,10))

After the plot panels are created, I add the curves with, for example:

 curve1 = p1.plot(data1,pen={'color':(0,0,255),'width':0.6},
                   symbolPen=(0,0,255),symbolBrush=(0,0,255),symbolSize=4,
                   name='WTG1',symbol='o')
 curve2 = p1.plot(data2,pen={'color':(0,255,0),'width':0.6},
                   symbolPen=(0,255,0),symbolBrush=(0,255,0),symbolSize=4,
                   name='WTG2',symbol='o')
 curve3 = p1.plot(data3,pen={'color':(255,0,0),'width':0.6},
                   symbolPen=(255,0,0),symbolBrush=(255,0,0),symbolSize=4,
                   name='WTG3',symbol='o')

The legend is added, all the plots work, but the legend font size needs adjustment. Can anybody help?

It seems that the size parameter in p1.addLegend(size=(10,30),offset=(10,10)) does not change anything. Can I change the font size without having to resort to manually creating the legend and adding individual legend items?

Resulting image is attached...

Thanks!

Jan

I tried
pyqt.tiff

vas...@gmail.com

unread,
Mar 21, 2016, 7:39:46 AM3/21/16
to pyqt...@googlegroups.com
Hi Jan.

I can't test your code as an independently app, so I commented and added some lines that could help to change some font size and color.

P.S. I am not sure to this is the right way, but it can work for you.

Cheers,
Vasilije


import pyqtgraph as pg
# import numpy as np


app = pg.QtGui.QApplication([])
main_window = pg.QtGui.QMainWindow()
main_window.setGeometry(10,40,1380,800)
rhs_widget = pg.QtGui.QWidget(main_window)
rhs_widget.setGeometry(1200,0,180,800)
plot_window = pg.GraphicsLayoutWidget(main_window)
plot_window.setGeometry(0,0,1200,800)

p1 = plot_window.addPlot(col=1,row=1)

# THESE PARAMETERS ARE FOR RESIZING AND MOVING THE POSITION OF THE LEGEND BOX
# I INCREASED X-SIZE AND Y-OFFSET

p1.addLegend(size=(110, 0) ,offset=(10, 230))

# YOU CAN COMMENT NEXT FOUR ROWS IF WILL USE THE BOTTOM (OF THIS CODE) TECHNIQUE
p1.setTitle('<font size="3">Active Power</font>') #,**titleStyle)

p1.setLabel(axis='left',text='<font size="3">Power (kW) </font>')
p1.setLabel(axis='top',)
p1.setLabel(axis='bottom',text='<font size="3">Time elapsed (s)</font>')


p1.setYRange(00,2500)

a = p1.getAxis('top')
#a.tickFont = b

a.showValues='false'
a = p1.getAxis('bottom')
#a.tickFont = b

p1.showAxis('left')
a = p1.getAxis('left')
#a.tickFont = b

p1.showAxis('right')
a = p1.getAxis('right')
#a.tickFont = b
p1.showLabel('left', show=True)
p1.showLabel('right', show=True)
p1.showGrid(x=True, y=True, alpha=0.5)


data1 = [2300, 2450, 2350]
data2 = [2250, 2375, 2345]
data3 = [2390, 2350, 2200]


curve1 = p1.plot(data1,pen={'color':(0,0,255),'width':0.6},
               symbolPen=(0,0,255),symbolBrush=(0,0,255),symbolSize=4,
               name='WTG1',symbol='+')


curve2 = p1.plot(data2,pen={'color':(0,255,0),'width':0.6},
               symbolPen=(0,255,0),symbolBrush=(0,255,0),symbolSize=4,
               name='WTG2',symbol='o')
curve3 = p1.plot(data3,pen={'color':(255,0,0),'width':0.6},
               symbolPen=(255,0,0),symbolBrush=(255,0,0),symbolSize=4,
               name='WTG3',symbol='o')


# CHANGE THE FONT SIZE AND COLOR OF ALL LEGENDS LABEL
legendLabelStyle = {'color': '#FFF', 'size': '12pt', 'bold': True, 'italic': False}
for item in p1.legend.items:
    for single_item in item:
        if isinstance(single_item, pg.graphicsItems.LabelItem.LabelItem):
            single_item.setText(single_item.text, **legendLabelStyle)


# SET AND CHANGE THE FONT SIZE AND COLOR OF THE PLOT AXIS LABEL
labelStyle = {'color': '#FFF', 'font-size': '24px'}
p1.setLabel('bottom', 'Time elapsed (s)', **labelStyle)
p1.setLabel('left', 'Power (kW)', **labelStyle)
p1.setLabel('top',)

# SET AND CHANGE THE FONT SIZE AND COLOR OF THE PLOT TITLE
titleStyle = {'color': '#FFF', 'size': '26pt'}
p1.setTitle('Active Power', **titleStyle)



main_window.show()
while main_window.isVisible():
    app.processEvents()



--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/3292d12e-07ee-48bf-a832-2a34dd623b84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

KATAYAMA Daiki

unread,
Mar 21, 2016, 11:04:09 AM3/21/16
to pyqt...@googlegroups.com
Hi all.

By the way, Tick Font's property can be changed by this code.
But, I don't know how to change the color and so on.

        b = QFont("Times New Roman", 10.5)  # QFont(const QString & family, int pointSize = -1, int weight = -1, bool italic = false)
        p1.getAxis('bottom').tickFont = b
        p1.getAxis('left').tickFont = b

Thanks.

Jan

unread,
Mar 22, 2016, 4:36:32 PM3/22/16
to pyqtgraph
Vasilje

Thank you for your excellent reply! I was banging my head against the wall.... Thanks to your two responses I have made fast and very satisfying progress....

Jan
Reply all
Reply to author
Forward
0 new messages