9

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.

Antoine
  • 173
  • 1
  • 7
  • I'm not sure why this was down-voted, but I added an up-vote for you. Seems like a perfectly valid question to me (although I don't know of a way to do what you want) – three_pineapples May 27 '15 at 01:56

1 Answers1

3

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

user2070870
  • 101
  • 1
  • 7
  • indeed you can use HTML for writting formulas, but the output is very ugly compared to a latex output. – Antoine Oct 20 '16 at 12:51
  • Can you create a custom style sheet to make the text look better? How about putting in a feature request in to the pyqtgraph developers for allowing MathJax or Latex rendering? – user2070870 Oct 23 '16 at 17:16