如何在 Matplotlib 中编写自己的 LaTeX 序言?
问题描述
我正在尝试在 matplotlib
中创建一个图形以插入到我的 LaTeX
文档中,我需要粗体 'i'
和'j'
符号不带圆点和帽子.
I'm trying to create a figure in matplotlib
to insert in my LaTeX
document, and I need the bold 'i'
and 'j'
symbols without their dots and a hat instead.
我一直在我的文档中使用代码 mathbf{hat{ extnormal{fseriesi}}}
,但是因为它使用了 amsmath
包(或者可能是 amssymb
包,对不起,我不知道我对 LaTeX
和 matplotlib
都很陌生)我不能在matplotlib
标签.
I've been using the code mathbf{hat{ extnormal{fseriesi}}}
in my document, but as this uses the amsmath
package (or possibly the amssymb
package, sorry I don't know I am very new to both LaTeX
and matplotlib
) I cannot use it in the matplotlib
label.
因此我很想知道如何在 matplotlib
中使用 LaTeX
以及如何编写序言来安装使用这些符号所需的包.我希望这是有道理的,我知道这可能已经存在或者很简单,但我是新手,所以请不要生气!
Hence I would love to know how to use LaTeX
in matplotlib
and how to write a preamble to install the package needed to use these symbols. I hope this makes sense, and I know this is probably already out there or is simple but I'm new so please dont get angry!
谢谢你们!
编辑
所以我以某种方式设法在编码方面做到了这一点,但是当我运行代码时,我的 python 内核
崩溃了.不幸的是,我并不真正理解我的代码或哪里出了问题,所以上传了它以防万一.抱歉上传时间长.再次提前感谢!
So I have somehow managed to get this far with the coding, but when ever I run the code, my python kernel
crashes. Unfortunately I don't really understand my code or where it is going wrong, so have uploaded it in case it helps. Sorry for the long upload. Again thanks in advance!
from mpl_toolkits.mplot3d import Axes3D
import numpy, matplotlib, matplotlib.pyplot as pyplot
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.preamble'] = [
r'usepackage{amsmath}',
r'usepackage{amssymb}']
matplotlib.rcParams['font.family'] = 'serif'
matplotlib.rcParams['font.serif'] = 'Computer Modern'
matplotlib.verbose.level = 'debug-annoying'
Module_Colour = '#F0AE1E'
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
X_arr = numpy.array([1.0,0.0,0.0])
Y_arr = numpy.array([0.0,1.0,0.0])
Z_arr = numpy.array([0.0,0.0,1.0])
O_arr = numpy.array([0.0,0.0,0.0])
pyplot.quiver(O_arr,O_arr,O_arr,X_arr,Y_arr,Z_arr,
pivot='tail', length=1.0, linewidth=2.5,
color = Module_Colour)
ax.set_xlabel('x-axis', fontsize=12.5)
ax.set_ylabel('y-axis', fontsize=12.5)
ax.set_zlabel('z-axis', fontsize=12.5)
ax.text(1.03, 0, 0, r'$mathbf{hat{ extnormal{fseries i}}}$',
fontsize=25, color = Module_Colour)
ax.text(0, 1.03, 0, r'$mathbf{hat{ extnormal{fseries j}}}$',
fontsize=25, color = Module_Colour)
ax.text(0, 0, 1.03, r'$mathbf{hat{ extnormal{fseries k}}}$',
fontsize=25, color = Module_Colour)
ax.set_xlim3d([0, 1.05])
ax.set_ylim3d([0, 1.05])
ax.set_zlim3d([0, 1.13])
ax.view_init(elev=17.0, azim=44.0)
pyplot.savefig('file/pathway/figure.jpeg',
bbox_inches='tight')
pyplot.show()
编辑
调试错误是:
FigureCanvasAgg.draw
RendererAgg.__init__
RendererAgg.__init__ width=800.0, height=600.0
RendererAgg.__init__ _RendererAgg done
RendererAgg.__init__ done
No LaTeX-compatible font found for the monospace font family in rcParams. Using default.
RendererAgg.points_to_pixels
No LaTeX-compatible font found for the monospace font family in rcParams. Using default.
C: && cd "C:Usersalexd.matplotlib ex.cache" && latex -interaction=nonstopmode 8160df9e5349057f736eef7cf8779b4d.tex > "C:Usersalexd.matplotlib ex.cache8160df9e5349057f736eef7cf8779b4d.output"
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (MiKTeX 2.9.6100 64-bit)
entering extended mode
(8160df9e5349057f736eef7cf8779b4d.tex
LaTeX2e <2016/03/31> patch level 3
Babel <3.9r> and hyphenation patterns for 75 language(s) loaded.
("C:Program FilesMiKTeX 2.9 exlatexasearticle.cls"
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
("C:Program FilesMiKTeX 2.9 exlatexasesize10.clo"))
("C:Program FilesMiKTeX 2.9 exlatex ype1cm ype1cm.sty")
("C:Program FilesMiKTeX 2.9 exlatexpsnfsshelvet.sty"
("C:Program FilesMiKTeX 2.9 exlatexgraphicskeyval.sty"))
("C:Program FilesMiKTeX 2.9 exlatexpsnfsscourier.sty")
("C:Program FilesMiKTeX 2.9 exlatexase extcomp.sty"
("C:Program FilesMiKTeX 2.9 exlatexase s1enc.def"))
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsmath.sty"
For additional information on amsmath, use the `?' option.
("C:Program FilesMiKTeX 2.9 exlatexamsmathamstext.sty"
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsgen.sty"))
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsbsy.sty")
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsopn.sty"))
("C:Program FilesMiKTeX 2.9 exlatexamsfontsamssymb.sty"
("C:Program FilesMiKTeX 2.9 exlatexamsfontsamsfonts.sty"))
("C:Program FilesMiKTeX 2.9 exlatexgeometrygeometry.sty"
("C:Program FilesMiKTeX 2.9 exgenericoberdiekifpdf.sty")
("C:Program FilesMiKTeX 2.9 exgenericoberdiekifvtex.sty")
("C:Program FilesMiKTeX 2.9 exgenericifxetexifxetex.sty")
("C:Program FilesMiKTeX 2.9 exlatexgeometrygeometry.cfg")
Package geometry Warning: Over-specification in `h'-direction.
`width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
`height' (5058.9pt) is ignored.
)
No file 8160df9e5349057f736eef7cf8779b4d.aux.
("C:Program FilesMiKTeX 2.9 exlatexase s1cmr.fd")
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
[1] (8160df9e5349057f736eef7cf8779b4d.aux) )
Output written on 8160df9e5349057f736eef7cf8779b4d.dvi (1 page, 240 bytes).
Transcript written on 8160df9e5349057f736eef7cf8779b4d.log.
Dvi: C:Usersalexd.matplotlib ex.cache8160df9e5349057f736eef7cf8779b4d.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
find_tex_file(cmr12.tfm): [u'kpsewhich', u'cmr12.tfm']
find_tex_file result: C:/Program Files/MiKTeX 2.9/fonts/tfm/public/cm/cmr12.tfm
opening tfm file C:/Program Files/MiKTeX 2.9/fonts/tfm/public/cm/cmr12.tfm
lh=18, bc=0, ec=127, nw=34, nh=16, nd=10
find_tex_file(cmr12.vf): [u'kpsewhich', u'cmr12.vf']
find_tex_file result:
RendererAgg.points_to_pixels
C: && cd "C:Usersalexd.matplotlib ex.cache" && latex -interaction=nonstopmode 27047192dde3a3a7381bad7b27d0fcb3.tex > "C:Usersalexd.matplotlib ex.cache27047192dde3a3a7381bad7b27d0fcb3.output"
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (MiKTeX 2.9.6100 64-bit)
entering extended mode
(27047192dde3a3a7381bad7b27d0fcb3.tex
LaTeX2e <2016/03/31> patch level 3
Babel <3.9r> and hyphenation patterns for 75 language(s) loaded.
("C:Program FilesMiKTeX 2.9 exlatexasearticle.cls"
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
("C:Program FilesMiKTeX 2.9 exlatexasesize10.clo"))
("C:Program FilesMiKTeX 2.9 exlatex ype1cm ype1cm.sty")
("C:Program FilesMiKTeX 2.9 exlatexpsnfsshelvet.sty"
("C:Program FilesMiKTeX 2.9 exlatexgraphicskeyval.sty"))
("C:Program FilesMiKTeX 2.9 exlatexpsnfsscourier.sty")
("C:Program FilesMiKTeX 2.9 exlatexase extcomp.sty"
("C:Program FilesMiKTeX 2.9 exlatexase s1enc.def"))
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsmath.sty"
For additional information on amsmath, use the `?' option.
("C:Program FilesMiKTeX 2.9 exlatexamsmathamstext.sty"
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsgen.sty"))
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsbsy.sty")
("C:Program FilesMiKTeX 2.9 exlatexamsmathamsopn.sty"))
("C:Program FilesMiKTeX 2.9 exlatexamsfontsamssymb.sty"
("C:Program FilesMiKTeX 2.9 exlatexamsfontsamsfonts.sty"))
("C:Program FilesMiKTeX 2.9 exlatexgeometrygeometry.sty"
("C:Program FilesMiKTeX 2.9 exgenericoberdiekifpdf.sty")
("C:Program FilesMiKTeX 2.9 exgenericoberdiekifvtex.sty")
("C:Program FilesMiKTeX 2.9 exgenericifxetexifxetex.sty")
("C:Program FilesMiKTeX 2.9 exlatexgeometrygeometry.cfg")
Package geometry Warning: Over-specification in `h'-direction.
`width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
`height' (5058.9pt) is ignored.
)
No file 27047192dde3a3a7381bad7b27d0fcb3.aux.
("C:Program FilesMiKTeX 2.9 exlatexase s1cmr.fd")
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
[1] (27047192dde3a3a7381bad7b27d0fcb3.aux) )
Output written on 27047192dde3a3a7381bad7b27d0fcb3.dvi (1 page, 288 bytes).
Transcript written on 27047192dde3a3a7381bad7b27d0fcb3.log.
Dvi: C:Usersalexd.matplotlib ex.cache27047192dde3a3a7381bad7b27d0fcb3.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
find_tex_file(cmmi12.tfm): [u'kpsewhich', u'cmmi12.tfm']
find_tex_file result: C:/Program Files/MiKTeX 2.9/fonts/tfm/public/cm/cmmi12.tfm
opening tfm file C:/Program Files/MiKTeX 2.9/fonts/tfm/public/cm/cmmi12.tfm
lh=18, bc=0, ec=127, nw=97, nh=15, nd=9
find_tex_file(cmmi12.vf): [u'kpsewhich', u'cmmi12.vf']
find_tex_file result:
C: && cd "C:Usersalexd.matplotlib ex.cache" && dvipng -bg Transparent -D 100.0 -T tight -o "da3e0f170d11553ae39831280c346c0f.png" "27047192dde3a3a7381bad7b27d0fcb3.dvi" > "C:Usersalexd.matplotlib ex.cacheda3e0f170d11553ae39831280c346c0f.output"
This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson
[1]
然后 python.exe
停止响应,不幸的是我不是很精通技术,所以很难理解为什么会发生这种情况.
Then python.exe
stops responding, and unfortunately I'm not very tech savvy so struggle to understand why this is happening.
解决方案
当我需要将 LaTeX
与 matplotlib
一起使用时,我在 python 脚本中添加以下内容 (+用于调试信息的附加命令)
When I need to use LaTeX
with matplotlib
, I add the following to the python script (+ additional commands for debug info)
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'usepackage{amsmath}
usepackage{foo-name} `...')
matplotlib.verbose.level = 'debug-annoying'
在此处制作情节的命令
相关文章