简体   繁体   中英

Different font weight when using matplotlib with latex

I'm using matplotlib with text.usetex : True . I have a LaTeX document that uses \\usepackage{sourcesanspro} and in matplotlib, I also set text.latex.preamble: \\usepackage{sourcesanspro} . If I now include the figure into my LaTeX PDF, the font weight looks different, the matplotlib figure is not "bold enough". This screenshot illustrates what I mean. The words "Linear advection" at the top are part of the matplotlib figure, the words "Linear advection" are part of the LaTex caption. I want both of them to look exactly equal. 在此处输入图片说明

I use the following style in 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

The plot is created via

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")

I tried saving the figure as a PNG and PDF. My LaTeX file is large and it includes a lot of packages and I admit that I don't know what all of them do. So it might be possible that there is something set that makes the font "more bold". But can I achieve the same styling by modifying the matplotlib style?

The font in latex is usually 10pt. But your title is 20 pt. If they still appear the same size, this would require for the figure to shrink by 50%. But if you shrink the figure by 50%, why should the thickness of the font lines stay constant?

Hence I would advice to make the figure as large as the document width, not rescaling it. Then using the same font size as in the latex document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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