【问题标题】:Different font weight when using matplotlib with latex将 matplotlib 与乳胶一起使用时字体粗细不同
【发布时间】:2020-02-21 08:07:25
【问题描述】:

我正在使用 text.usetex : True 的 matplotlib。我有一个使用 \usepackage{sourcesanspro} 的 LaTeX 文档,在 matplotlib 中,我还设置了 text.latex.preamble: \usepackage{sourcesanspro}。如果我现在将该图形包含在我的 LaTeX PDF 中,字体粗细看起来会有所不同,matplotlib 图形不够“大胆”。 此屏幕截图说明了我的意思。顶部的“线性平流”字样是 matplotlib 图形的一部分,“线性平流”字样是 LaTex 标题的一部分。我希望他们两个看起来完全一样。

我在 matplotlib 中使用以下样式。

### Control the fonts
text.usetex : True
text.latex.preamble:  \usepackage{sourcesanspro}
font.family :  STIXGeneral
mathtext.fontset : stix



#font.weight : bold
font.size : 20
axes.titlesize : 22
axes.titleweight : normal # bold title

### Control the colors
text.color:         (0.0,0.0,0.0) # instead of black we use a more grey color
axes.labelcolor :   (0.0,0.0,0.0)
axes.edgecolor :    (0.0,0.0,0.0)
xtick.color:        (0.0,0.0,0.0)
ytick.color:        (0.0,0.0,0.0)


### Control title and margins 
axes.titlepad : 20  # move title up
axes.xmargin: 0.0   # don't add space in x direction 
axes.ymargin: 0.05 

### Control spines
axes.spines.top    : False # no bounding box right and top
axes.spines.right  : False


### Control default sizes and widths
lines.linewidth : 4
lines.markersize : 12



### Control the color cycle. These are KIT Colors
axes.prop_cycle    : cycler('color', [ (0.0, 0.5882352941176471, 0.5098039215686274),(0.0,0.0,0.0),(0.27450980392156865, 0.39215686274509803, 0.6666666666666666), (0.8745098039215686, 0.6078431372549019, 0.10588235294117647), (0.6392156862745098, 0.06274509803921569, 0.48627450980392156), (0.5490196078431373, 0.7137254901960784, 0.235294117647), (0.6352941176470588, 0.13333333333333333, 0.13725490196078433), (0.13725490196078433, 0.6313725490196078, 0.8784313725490196), (0.6549019607843137, 0.5098039215686274, 0.1803921568627451), (0.0,0.0,0.0)])


### Control the figure dimension and resolution
figure.figsize   : 10, 3.75
figure.constrained_layout.use: True
figure.dpi : 100
savefig.dpi : 200


### Control the underlying grid 
axes.grid : True
grid.color: .1
grid.linestyle: -
grid.alpha: .5
grid.linewidth: 0.1


### Control the legend box
legend.fancybox : True
legend.facecolor: white
legend.loc : upper right
legend.framealpha : 0.975
legend.edgecolor : darkgray

情节是通过

创建的
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
plt.style.use("THENAMEOFTHESTYLE")
fig,ax = plt.subplots(1,1)
a = 1
x = np.linspace(-5,10,1000)
rho = np.exp(-x**2)
ax.plot(x,rho,label = r"$t=0$")

T = 2
rho = np.exp(-(x-a*T)**2)
ax.plot(x,rho,label = r"$t={}$".format(T))

T = 4
rho = np.exp(-(x-a*T)**2)
ax.plot(x,rho,label = r"$t={}$".format(T))


T = 6
rho = np.exp(-(x-a*T)**2)
ax.plot(x,rho,label = r"$t={}$".format(T))


ax.set_xlim([-5,10])
ax.set_ylim([0,1.1])
ax.set_title(r"Linear advection equation $\partial_t\rho(t,x) + \partial_x \rho(t,x)= 0$.")
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$\rho(t,x)$")
plt.legend()


plt.savefig("kitlinear.pdf")

我尝试将图形保存为 PNG 和 PDF。 我的 LaTeX 文件很大,它包含很多包,我承认我不知道它们都做了什么。因此,可能有一些设置使字体“更粗”。但是我可以通过修改matplotlib样式来实现相同的样式吗?

【问题讨论】:

  • latex中的字体通常为10pt。但你的标题是 20 pt。如果它们仍然显示相同的大小,则需要将数字缩小 50%。但是如果把图形缩小50%,为什么字体线的粗细要保持不变呢?
  • 以上是一个好点。我还建议将图形保存为.tex 而不是 PDF。通常这有助于图形文本和文档文本之间的一致性。
  • @ImportanceOfBeingErnest,谢谢。这确实是解决方案。如果您将此作为答案,我会接受。

标签: python matplotlib latex


【解决方案1】:

latex 中的字体通常为 10pt。但你的标题是 20 pt。如果它们仍然显示相同的大小,则需要将数字缩小 50%。但是如果把图形缩小50%,为什么字体线的粗细要保持不变呢?

因此,我建议使图形与文档宽度一样大,而不是重新缩放它。然后使用与 Latex 文档中相同的字体大小。

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 2013-07-31
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多