[英]Different font weight when using matplotlib with latex
我將 matplotlib 與text.usetex : True
。 我有一個使用\\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 樣式來實現相同的樣式嗎?
乳膠中的字體通常是 10pt。 但是你的標題是 20 pt。 如果它們仍然顯示相同的大小,這將需要數字縮小 50%。 但是如果將圖形縮小 50%,為什么字體線的粗細要保持不變?
因此,我建議使圖形與文檔寬度一樣大,而不是重新縮放它。 然后使用與乳膠文檔中相同的字體大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.