使用Matplotlib中的LaTex渲染轴标签

时间:2017-12-12 17:22:22

标签: python matplotlib figure

我尝试使用Matplotlib对我的图表中的所有文本进行抗锯齿渲染。我的图表导出为pdf文件。我在matplotlibrc文件中允许使用 antialiased 的所有参数:

### MATPLOTLIBRC FORMAT

lines.antialiased   : True         # render lines in antialiased (no jaggies)
patch.antialiased   : True    # render patches in antialiased (no jaggies)
font.family         : serif
font.weight         : normal
font.size           : 12.0
font.serif          : Computer modern, DejaVu Serif, Bitstream Vera Serif,New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif

text.usetex         : True  # use latex for all text handling. The following fonts
text.latex.preamble :  \usepackage{amsmath},\usepackage{pgfplots},\usepackage[T1]{fontenc} 

text.antialiased : True # If True (default), the text will be antialiased.
                     # This only affects the Agg backend.


mathtext.fontset : cm # Should be 'dejavusans' (default),
                           # 'dejavuserif', 'cm' (Computer Modern), 'stix',
                           # 'stixsans' or 'custom'


axes.unicode_minus  : True    # use unicode for the minus symbol
                           # rather than hyphen.  See
                           # http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes

legend.loc           : best
legend.frameon       : False     # if True, draw the legend on a background patch
legend.framealpha    : 0.7      # legend patch transparency
legend.facecolor     : inherit  # inherit from axes.facecolor; or color spec
legend.edgecolor     : 0      # background patch boundary color
legend.fancybox      : False     # if True, use a rounded box for the
                             # legend background, else a rectangle

figure.autolayout : True  # When True, automatically adjust subplot
                        # parameters to make the plot fit the figure

以下是一个产生丑陋数字的最小非工作示例:

import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,1],[0,10], linewidth=2, label = r'I am a line')
leg = plt.legend(title=r'$The_{\text{legend}}$ : ' ,ncol=1)
leg._legend_box.align = 'left'
plt.xlabel(r'$D \text{(mm)}$')
plt.ylabel(r'Arbitrary unit')
plt.savefig('example.pdf')
plt.close()

这给出了这个数字:

Link to the pdf file, only valid 10 days

the figure

当放大时,我们清楚地看到文本实际上已经超出了数学模式:

Figure zoomed in on legend

Figure zoomed on axis label

我怎么能避免这个?

此外:

  • 我知道svg,tikz和pgf export,但我真的希望有一个易于与他人分享的文件类型,

  • 由于拼写错误,我无法将所有文本切换到数学模式。在图中,变量必须呈现为数学对象,文本呈现为文本对象,

  • 在LaTex序言中添加\usepackage{lmodern, mathtools}以使用Latin Modern而不是Computer Modern不起作用。也许是因为Matplotlib在数学模式之外不使用LaTex来渲染标签。

1 个答案:

答案 0 :(得分:0)

根据ImprortanceOfBeingErnest的评论,

  

通常在mathmode中获取一些直立字体,您可以使用\mathrm{}

我有一个生成图的代码的工作示例,而不更改matplotlibrc:

import numpy as np
import matplotlib.pyplot as plt

plt.plot([1,1],[0,10], linewidth=2, label = r'$\mathrm{I \:  am \:  a  \: line}$')
leg = plt.legend(title=r'$The_{\mathrm{legend}}$ : ', ncol=1)
leg._legend_box.align = 'left'
plt.xlabel(r'$D \:  \mathrm{(mm)}$')
plt.ylabel(r'$\mathrm{Arbitrary \: unit}$')
plt.savefig('example.pdf')
plt.close()

这对渲染非常有用。但在代码中非常难看,我必须始终以\mathrm{}模式编写,并在每次需要空格时添加\:

然后,我的问题变成:有没有办法避免使用\mathrm{}模式并且仍然有一个好身材?如果没有,那么我对此非常满意。