Display LaTeX notation formulas in Python, matplotlib

1.First of all

Previous article: http://qiita.com/damyarou/items/818b20ea0def9ff43484 So, can't the fraction `` `\ frac``` be displayed? I wrote that, but when I looked it up, there was a way to display a considerable part of LaTeX. (http://matplotlib.org/users/usetex.html)

2. Preparation

2.1 Backend

It seems that it is necessary to specify a backend. In my case, matplotlib installed by pyenv did not work in the past, so I specified TkAgg.

(Correspondence) Create a file called matplotlibrc in ~ / .marplotlib, write one line``backend: TkAgg``` and save it.

(reference) http://blog.livedoor.jp/mt_kinabalu/archives/48592087.html http://qiita.com/katryo/items/918667f28301fdec89ba

2.2 type1cm.sty Also, during the trial, I got a message that "type1cm.sty does not exist", so I installed this in the folder read by TeX. In my case, I have BasicTeX on my Mac, so I put `` `type1cm.sty``` in the following folder. Folder mytool is a folder that stores your own style files that are not included as standard.

/usr/local/texlive/2015basic/texmf-local/tex/mytool/

3. Test program

3.1 Precautions

from matplotlib import rc

rc('text', usetex=True)
text14=r'$\displaystyle r=\sqrt{\frac{-2(1-\rho^2)\cdot\ln(1-p)}{1-2\rho\cdot\sin\theta\cdot\cos\theta}}$'
fnameF='fig_tex_test.pdf'
......
......
plt.savefig(fnameF)
text02='(2)   '+r'$\thickmuskip=0mu \medmuskip=0mu \thinmuskip=0mu \displaystyle f(x)=....

(Reference) http://oversleptabit.com/?p=556

3.2 Execution script

The program creates a pdf, which is converted to png with Imagemagick.

python py_tex_test.py

convert -trim -density 400 fig_tex_test.pdf -bordercolor "white" -border 10x10 fig_tex_test.png

3.3 Sample program code

python


import matplotlib.pyplot as plt
from matplotlib.ticker import *
from matplotlib import rc

#reference: http://oversleptabit.com/?p=556

rc('text', usetex=True)

# drawing area: w x h = 16 x 25 imaged A4 paper
xmin=0
xmax=16
ymin=0
ymax=25
dh=1.5 # height of one row
A=[]   # actural y-axis
B=[]   # grid number in y-axis
for i in range(0,int(ymax//dh)+1):
    s='{0:.1f}'.format(dh*i)
    A=A+[float(s)]
    B=B+[i]

fnameF='fig_tex_test.pdf'
fig = plt.figure()
ax1=plt.subplot(111)
ax1 = plt.gca()
ax1.set_xlim([xmin,xmax])
ax1.set_ylim([ymax,ymin])
aspect = (abs(ymax-ymin))/(abs(xmax-xmin))*abs(ax1.get_xlim()[1] - ax1.get_xlim()[0]) / abs(ax1.get_ylim()[1] - ax1.get_ylim()[0])
ax1.set_aspect(aspect)
#plt.axis('off')
ax1.tick_params(labelsize=6)
ax1.xaxis.set_major_locator(MultipleLocator(1))
ax1.yaxis.set_major_locator(MultipleLocator(dh))
plt.yticks(A,B)
plt.grid(which='both',lw=0.3, color='#cccccc',linestyle='-')

############################################
iis=1
title='Equations'
text01='(1)   '+r'$\thickmuskip=0mu \medmuskip=0mu \thinmuskip=0mu \displaystyle \int\int\int_V\left(\frac{\partial P}{\partial x}+\frac{\partial Q}{\partial y}+\frac{\partial R}{\partial z}\right)dx dy dz=\int\int_S(P dy dz+Q dz dx+R dx dy)$'
text02='(2)   '+r'$\thickmuskip=0mu \medmuskip=0mu \thinmuskip=0mu \displaystyle f(x)=\frac{a_0}{2}+\sum_{n=1}^{+\infty}\left(a_n\cos\frac{n\pi x}{\ell}+b_n\sin\frac{n\pi x}{\ell}\right)$'
text03='(3)   '+r'$\displaystyle f(x)=\frac{a_0}{2}+\sum_{n=1}^{+\infty}\left(a_n\cos\frac{n\pi x}{\ell}+b_n\sin\frac{n\pi x}{\ell}\right)$'
text04='(4)   '+r'$\displaystyle a_0=\frac{1}{\ell}\int_{-\ell}^{\ell}f(x) dx$'
text05='(5)   '+r'$\displaystyle a_n=\frac{1}{\ell}\int_{-\ell}^{\ell}f(x)\cos\frac{n\pi x}{\ell}dx \qquad (n=1,2,3, \cdots)$'
text06='(6)   '+r'$\displaystyle b_n=\frac{1}{\ell}\int_{-\ell}^{\ell}f(x)\sin\frac{n\pi x}{\ell}dx \qquad (n=1,2,3, \cdots)$'
text07='(7)   '+r'$\displaystyle e^x=1+\frac{x}{1!}+\cdots+\frac{x^{n-1}}{(n-1)!}+\frac{e^{\theta x}}{n!}x^n \qquad (0<\theta<1)$'
text08='(8)   '+r'$\displaystyle \lim_{n\to\pm\infty}\left(1+\frac{1}{x}\right)^x=e$'
text09='(9)   '+r'$\displaystyle \cosh z=\frac{e^z+e^{-z}}{2} \qquad \sinh z=\frac{e^z-e^{-z}}{2}$'
text10='(10)  '+r'$\displaystyle y=a\cdot x+b$'
text11='(11)  '+r'$\displaystyle a=\frac{n \sum xy-\sum x\cdot\sum y}{n \sum x^2-(\sum x)^2} \qquad \qquad b=\frac{\sum x^2\cdot \sum y-\sum x\cdot\sum xy}{n \sum x^2-(\sum x)^2}$'
text12='(12)  '+r'$\thickmuskip=0mu \medmuskip=0mu \thinmuskip=0mu \displaystyle r=\frac{n \sum xy-\sum x\cdot\sum y}{\sqrt{[n \sum x^2-(\sum x)^2]\cdot [n \sum y^2-(\sum y)^2]}}$'
text13='(13)  '+r'$\displaystyle r=\frac{n \sum xy-\sum x\cdot\sum y}{\sqrt{[n \sum x^2-(\sum x)^2]\cdot [n \sum y^2-(\sum y)^2]}}$'
text14='(14)  '+r'$\displaystyle x=x_m+\sigma_x\cdot r \cdot\cos\theta \qquad y=y_m+\sigma_y\cdot r \cdot\sin\theta$'
text15='(15)  '+r'$\displaystyle r=\sqrt{\frac{-2(1-\rho^2)\cdot\ln(1-p)}{1-2\rho\cdot\sin\theta\cdot\cos\theta}}$'


xv0=0
fsize=4 # fontsize
xs=xv0+1; ys=iis*dh- 0.5*dh; plt.text(xs,ys,title,rotation=0,ha='left',va='center',fontsize=fsize,fontweight='bold')
xs=xv0+1; ys=iis*dh+ 0.5*dh; plt.text(xs,ys,text01,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+ 1.5*dh; plt.text(xs,ys,text02,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+ 2.5*dh; plt.text(xs,ys,text03,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+2; ys=iis*dh+ 3.5*dh; plt.text(xs,ys,text04,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+2; ys=iis*dh+ 4.5*dh; plt.text(xs,ys,text05,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+2; ys=iis*dh+ 5.5*dh; plt.text(xs,ys,text06,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+ 6.5*dh; plt.text(xs,ys,text07,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+ 7.5*dh; plt.text(xs,ys,text08,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+ 8.5*dh; plt.text(xs,ys,text09,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+ 9.5*dh; plt.text(xs,ys,text10,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+10.5*dh; plt.text(xs,ys,text11,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+11.5*dh; plt.text(xs,ys,text12,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+12.5*dh; plt.text(xs,ys,text13,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+13.5*dh; plt.text(xs,ys,text14,rotation=0,ha='left',va='center',fontsize=fsize)
xs=xv0+1; ys=iis*dh+14.5*dh; plt.text(xs,ys,text15,rotation=0,ha='left',va='center',fontsize=fsize)
############################################

#plt.savefig(fnameF, dpi=200, bbox_inches="tight", pad_inches=0.2)
plt.savefig(fnameF)
#plt.show()

4. Deliverables

fig_tex_test.png

did it!

that's all

Recommended Posts

Display LaTeX notation formulas in Python, matplotlib
Class notation in Python
Display UTM-30LX data in Python
Convert CIDR notation in Python
Included notation in Python function arguments
LaTeX, Python tips in master's thesis
Waveform display of audio in Python
Display characters like AA in python
Heatmap with Dendrogram in Python + matplotlib
The first step in Python Matplotlib
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory
[Python, Julia] 3D display in Jupyter-Mayavi library
How to suppress display error in matplotlib
Displaying candlestick charts in Python (matplotlib edition)
Display a list of alphabets in Python 3
Display matplotlib diagrams in a web application
Display Python 3 in the browser with MAMP
Display Matplotlib xy graph in PySimple GUI.
How to display multiplication table in python
Display pyopengl in a browser (python + eel)
How to display Hello world in python
Why can't I install matplotlib in python! !!
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Receive and display HTML form data in Python
When I try matplotlib in Python, it says'cairo.Context'
#Python basics (#matplotlib)
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Correct half-width and full-width notation fluctuations in Python
Display candlesticks for FX (forex) data in Python
N-Gram in Python
Programming in python
[Python] Display the Altair legend in the plot area
Plink in Python
Constant in python
My matplotlib (python)
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
[Python] How to draw a histogram in Matplotlib
Reflection in Python
Constant in python
nCr in Python.
format in python