[英]Rendering of axis label using LaTex in Matplotlib
我正在嘗試使用Matplotlib對圖形中的所有文本進行抗鋸齒渲染。 我的地塊導出為pdf文件。 我在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
以下是一個最小的不工作示例,該示例會生成一個丑陋的圖形:
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()
這給出了這個數字:
當放大時,我們清楚地看到文本確實超出了數學模式的像素范圍:
我如何避免這種情況?
也:
我知道svg,tikz和pgf導出,但是我真的很想擁有一種易於與他人共享的文件類型,
由於輸入錯誤,我無法將所有文本切換到數學模式。 在繪圖上,變量必須呈現為數學對象,而文本則呈現為文本對象,
在LaTex序言中添加\\usepackage{lmodern, mathtools}
以使用Latin Modern代替Computer Modern是無效的。 可能是因為Matplotlib在超出數學模式時不使用LaTex渲染標簽。
根據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{}
模式並且仍然有一個好的身材? 如果沒有,那么我對此很滿意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.