简体   繁体   English

在Matplotlib中使用LaTex渲染轴标签

[英]Rendering of axis label using LaTex in Matplotlib

I'm trying to have an antialiased render of all text on my plots using Matplotlib. 我正在尝试使用Matplotlib对图形中的所有文本进行抗锯齿渲染。 My plots are exported as pdf files. 我的地块导出为pdf文件。 I allowed all parameters that say antialiased in my matplotlibrc file: 我在matplotlibrc文件中允许使用所有抗锯齿参数:

### 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

And here follows a minimum non working example that generates an ugly 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()

This gives this figure: 这给出了这个数字:

Link to the pdf file, only valid 10 days 链接到pdf文件,仅有效10天

the figure 该图

And when zoomed in we clearly see that the text is really pixelly out of math mode: 当放大时,我们清楚地看到文本确实超出了数学模式的像素范围:

Figure zoomed in on legend 图放大了图例

Figure zoomed on axis label 图放大了轴标签

How could I avoid this ? 我如何避免这种情况?

Also: 也:

  • I know about svg, tikz and pgf export, but I would really like to have a file type that is easy to share with others, 我知道svg,tikz和pgf导出,但是我真的很想拥有一种易于与他人共享的文件类型,

  • I cannot switch all text to math mode because of typo rules. 由于输入错误,我无法将所有文本切换到数学模式。 On plots, variables have to be rendered as maths objects and text as text ones, 在绘图上,变量必须呈现为数学对象,而文本则呈现为文本对象,

  • adding \\usepackage{lmodern, mathtools} in the LaTex preamble to use Latin Modern instead of Computer Modern does not work. 在LaTex序言中添加\\usepackage{lmodern, mathtools}以使用Latin Modern代替Computer Modern是无效的。 Maybe because Matplotlib does not use LaTex to render the labels when out of math mode. 可能是因为Matplotlib在超出数学模式时不使用LaTex渲染标签。

Following the comment of ImprortanceOfBeingErnest, 根据ImprortanceOfBeingErnest的评论,

Usually to get some upright font in mathmode you would use \\mathrm{} 通常,要在mathmode模式下获得一些直立字体,您可以使用\\mathrm{}

I have a working example of the code generating the figure, without changing the matplotlibrc: 我有一个生成图的代码的工作示例,而没有更改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()

This is really good for the rendering. 这对于渲染确实很好。 But quite ugly in the code, where I always have to be in \\mathrm{} mode to write, and add \\: each time I want a space. 但是代码很丑陋,每次我想要空格时,我总是必须处于\\mathrm{}模式才能编写并添加\\: :。

Then, my question becomes: Is there a way to avoid using \\mathrm{} mode and still have a good figure? 然后,我的问题变成:有没有一种方法可以避免使用\\mathrm{}模式并且仍然有一个好的身材? If not, then I'm quite happy with this. 如果没有,那么我对此很满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM