Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How write \latex character in pyqtgraph?

I'm using pyqtgraph and I would like to write some formula on the graphs. How to write with the latex synthax? matplolib get its own TeX expression parser, but I can't find the solution for pyqtgraph.

like image 364
Antoine Avatar asked May 26 '15 13:05

Antoine


1 Answers

The labels and text items in pyqtgraph except HTML formatting. Here is an example:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
pg.setConfigOptions(antialias=True)
pg.setConfigOption('background', '#c7c7c7')
pg.setConfigOption('foreground', '#000000')
from pyqtgraph.ptime import time
app = QtGui.QApplication([])

p = pg.plot()
p.setLabel('bottom', '<font>The &Chi; Axi&Sigma;</font>', 
            units='<font>&Beta;-Juice</font>', **{'font-size':'20pt'})
p.getAxis('bottom').setPen(pg.mkPen(color='#000000', width=3))

p.setLabel('left', '<font>&mu;-cells</font>', units='<font>&mu;-meter</font>',
            color='#c4380d', **{'font-size':'20pt'})
p.getAxis('left').setPen(pg.mkPen(color='#c4380d', width=3))

p.showAxis('top')
p.getAxis('top').setPen(pg.mkPen(color='#77ab43', width=3))
p.setLabel('top', '<math>H(s) = &int;<sub>0</sub><sup>&infin;</sup> e<sup>-st</sup> h(t) dt</math>', color='#77ab43', **{'font-size':'30pt'})

p.showAxis('right')
p.setLabel('right', '<font>&Delta;</font> House', units="<font>&Omega;</font>",
            color='#025b94', **{'font-size':'20pt'})
p.getAxis('right').setPen(pg.mkPen(color='#025b94', width=3))

np.random.seed(1234)
data = 30*np.random.randn(10000)
mu = data.mean()
median = np.median(data)
sigma = data.std()

y,x = np.histogram(data, bins=50)

curve = p.plot(x=x,y=y, stepMode=True, pen=pg.mkPen(color="#636", width=2))

textstr = '<math><p>&mu;=%.2f</p><p>median=%.2f</p><p>&sigma;=%.2f</p></math>'%(mu, median, sigma)
text = pg.TextItem(html=textstr, border='#FFFFFF', fill="#ffffcc")
text.setParentItem(curve)
text.setPos(x.min(),y.max())

equation_string = '<math>H(s) = &int;<sub>0</sub><sup>&infin;</sup> e<sup>-st</sup> h(t) dt</math>'
eq_text = pg.TextItem(html=equation_string, border='#000000', fill='#ccffff')
eq_text.setParentItem(curve)
eq_text.setPos(-20, y.max()*0.5)

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Here is an image of the output. pyqtgraph_w_equations

like image 112
user2070870 Avatar answered Oct 11 '22 13:10

user2070870