如何在Matplotlib中编写自己的LaTeX导言?

21
我正在尝试在我的LaTeX文档中插入matplotlib图形,并需要加粗的'i'和'j'符号,但不需要它们的点,取而代之的是帽子。我一直在使用代码\mathbf{\hat{\textnormal{\bfseries\i}}},但由于这使用了amsmath包(或可能是amssymb包,抱歉我对LaTeX和matplotlib都很陌生),所以我不能在matplotlib标签中使用它。因此,我想知道如何在matplotlib中使用LaTeX,以及如何编写一个前导来安装使用这些符号所需的包。谢谢大家!编辑:我设法用这个代码进展到这里,但每当我运行代码时,我的python内核就会崩溃。不幸的是,我真的不理解我的代码或它出了什么问题,所以我上传了它,以防有所帮助。对于长时间的上传我感到抱歉。再次感谢您的帮助!
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{\textnormal{\bfseries \i}}}$',
        fontsize=25, color = Module_Colour)
ax.text(0, 1.03, 0, r'$\mathbf{\hat{\textnormal{\bfseries \j}}}$',
        fontsize=25, color = Module_Colour)
ax.text(0, 0, 1.03, r'$\mathbf{\hat{\textnormal{\bfseries 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:\Users\alexd\.matplotlib\tex.cache" && latex -interaction=nonstopmode 8160df9e5349057f736eef7cf8779b4d.tex > "C:\Users\alexd\.matplotlib\tex.cache\8160df9e5349057f736eef7cf8779b4d.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 Files\MiKTeX 2.9\tex\latex\base\article.cls"
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
("C:\Program Files\MiKTeX 2.9\tex\latex\base\size10.clo"))
("C:\Program Files\MiKTeX 2.9\tex\latex\type1cm\type1cm.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\psnfss\helvet.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\keyval.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\psnfss\courier.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\base\textcomp.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\base\ts1enc.def"))
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsmath.sty"
For additional information on amsmath, use the `?' option.
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amstext.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsgen.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsbsy.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsopn.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\amsfonts\amssymb.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\amsfonts\amsfonts.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\geometry\geometry.sty"
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\ifpdf.sty")
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\ifvtex.sty")
("C:\Program Files\MiKTeX 2.9\tex\generic\ifxetex\ifxetex.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\geometry\geometry.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 Files\MiKTeX 2.9\tex\latex\base\ts1cmr.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:\Users\alexd\.matplotlib\tex.cache\8160df9e5349057f736eef7cf8779b4d.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:\Users\alexd\.matplotlib\tex.cache" && latex -interaction=nonstopmode 27047192dde3a3a7381bad7b27d0fcb3.tex > "C:\Users\alexd\.matplotlib\tex.cache\27047192dde3a3a7381bad7b27d0fcb3.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 Files\MiKTeX 2.9\tex\latex\base\article.cls"
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
("C:\Program Files\MiKTeX 2.9\tex\latex\base\size10.clo"))
("C:\Program Files\MiKTeX 2.9\tex\latex\type1cm\type1cm.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\psnfss\helvet.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\keyval.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\psnfss\courier.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\base\textcomp.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\base\ts1enc.def"))
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsmath.sty"
For additional information on amsmath, use the `?' option.
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amstext.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsgen.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsbsy.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\amsmath\amsopn.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\amsfonts\amssymb.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\amsfonts\amsfonts.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\geometry\geometry.sty"
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\ifpdf.sty")
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\ifvtex.sty")
("C:\Program Files\MiKTeX 2.9\tex\generic\ifxetex\ifxetex.sty")
("C:\Program Files\MiKTeX 2.9\tex\latex\geometry\geometry.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 Files\MiKTeX 2.9\tex\latex\base\ts1cmr.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:\Users\alexd\.matplotlib\tex.cache\27047192dde3a3a7381bad7b27d0fcb3.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:\Users\alexd\.matplotlib\tex.cache" && dvipng -bg Transparent -D 100.0 -T tight -o "da3e0f170d11553ae39831280c346c0f.png" "27047192dde3a3a7381bad7b27d0fcb3.dvi" > "C:\Users\alexd\.matplotlib\tex.cache\da3e0f170d11553ae39831280c346c0f.output"
This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson
[1]

然后python.exe停止响应,不幸的是我对技术不是很熟悉,所以很难理解为什么会发生这种情况。


1
请查看以下问题的答案:https://dev59.com/uHE85IYBdhLWcg3w8IQ4#20709149 - Serenity
嘿@Serenity,感谢您的帮助,我尝试使用那些行代码,但Python似乎总是崩溃。然而,@JimParker的代码(如下)似乎可以工作,但会出现错误!如果我看起来很笨拙(我可能确实如此),只是对LaTeXPythonmatplotlib)有非常基本的理解。 - Aldahunter
2个回答

35

当我需要在matplotlib中使用LaTeX时,我将以下代码添加到Python脚本中(加上其他的调试信息命令)

 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'

制作您的图表的命令


1
@AldaHunter 请看上面的编辑。我使用了<foo>而不是{foo}来表示可选包名。你收到的错误是LaTex版本的“文件未找到”。 - Jim Parker
1
如果您可以接受非粗体标签,则使用LaTeX字符串r'$\mathbf{\hat{j}}$'可行。 - Jim Parker
1
很抱歉,我不明白为什么我们无法获得任何调试信息。没有它,我束手无策。你可以尝试删除foo-file.txt.fileo的行。这将导致输出默认到stdout,但如果之前没有生成任何内容,我很难有希望。等待更有经验的人发言。 - Jim Parker
1
@AldaHunter,很抱歉我已经不知道该如何给你建议了。调试信息中没有列出任何错误。最后一行说明dvipng正在尝试创建一个.png文件,并且日志可以在C:\ Users \ alexd \ .matplotlib \ tex.cache \ da3e0f170d11553ae39831280c346c0f.output找到。请尝试查看该文件以获取更多信息。 - Jim Parker
显示剩余25条评论

-1

如果 @Jim Parker 的答案对您无效 - 就像我遇到同样的问题时发生的那样 - 我认为这将正常工作,因为上述方法会更改标准前言,但此方法将所需的软件包添加到前言中。

代码

plt.rcParams["text.latex.preamble"].join([
        r"\usepackage{dashbox}",              
        r"\setmainfont{xcolor}",
])

实际上,这并没有为pyplot使用的前导添加任何内容。它只是创建了一个新字符串,并没有对其进行任何操作。您仍然需要使用新字符串调用plt.rc(...)来实际设置它。此外,如果有超过两个项目,它将会中断,因为它将在您指定的列表中的每对项目之间插入旧的前导。 - Fritz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接