将字体更改为avant Matplotlib
我正在尝试实施matplotlib图的保管程序,以便使用乳胶作品。有关更多参考信息,请查看以下链接: 目标是将字体族更改为avant,以便与完整报告的字体匹配。以下是所选先锋字体的快照: 下面的代码显示了我的尝试。我实现了以下代码:将字体更改为avant Matplotlib,matplotlib,fonts,latex,Matplotlib,Fonts,Latex,我正在尝试实施matplotlib图的保管程序,以便使用乳胶作品。有关更多参考信息,请查看以下链接: 目标是将字体族更改为avant,以便与完整报告的字体匹配。以下是所选先锋字体的快照: 下面的代码显示了我的尝试。我实现了以下代码: import matplotlib.pyplot as plt import numpy as np import pandas as pd import matplotlib from math import sqrt SPINE_COLOR = 'gray'
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib
from math import sqrt
SPINE_COLOR = 'gray'
def latexify(fig_width=None, fig_height=None, columns=1):
"""Set up matplotlib's RC params for LaTeX plotting.
Call this before plotting a figure.
Parameters
----------
fig_width : float, optional, inches
fig_height : float, optional, inches
columns : {1, 2}
"""
# code adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples
# Width and max height in inches for IEEE journals taken from
# computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf
assert(columns in [1,2])
if fig_width is None:
fig_width = 3.39 if columns==1 else 6.9 # width in inches
if fig_height is None:
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_height = fig_width*golden_mean # height in inches
MAX_HEIGHT_INCHES = 8.0
if fig_height > MAX_HEIGHT_INCHES:
print("WARNING: fig_height too large:" + fig_height +
"so will reduce to" + MAX_HEIGHT_INCHES + "inches.")
fig_height = MAX_HEIGHT_INCHES
params = {'backend': 'ps',
'text.latex.preamble':[r'\usepackage{gensymb}', r'\usepackage{avant}'],
'axes.labelsize': 8, # fontsize for x and y labels (was 10)
'axes.titlesize': 8,
'font.size': 8, # was 10
'legend.fontsize': 8, # was 10
'xtick.labelsize': 8,
'ytick.labelsize': 8,
'text.usetex': True,
'figure.figsize': [fig_width,fig_height],
'font.family': 'avant'
}
matplotlib.rcParams.update(params)
def format_axes(ax):
for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)
for spine in ['left', 'bottom']:
ax.spines[spine].set_color(SPINE_COLOR)
ax.spines[spine].set_linewidth(0.5)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
for axis in [ax.xaxis, ax.yaxis]:
axis.set_tick_params(direction='out', color=SPINE_COLOR)
return ax
df = pd.DataFrame(np.random.randn(10,2))
df.columns = ['Column 1', 'Column 2']
ax = df.plot()
ax.set_xlabel("X label")
ax.set_ylabel("Y label")
ax.set_title("Title")
plt.tight_layout()
plt.savefig("image1.pdf")
latexify()
ax = df.plot()
ax.set_xlabel("X label")
ax.set_ylabel("Y label")
ax.set_title("Title")
plt.tight_layout()
format_axes(ax)
plt.savefig("image2.pdf")
问题是它似乎无法识别前卫字体。这是我收到的错误:
findfont: Font family ['avant'] not found. Falling back to DejaVu Sans.
是否有人知道如何调整代码以获得所需的字体系列(avant)?您可以忽略findfont警告。它表示在您的系统上找不到字体
avant
。但是,由于使用latex进行文本渲染,matplotlib找不到字体这一事实与此无关
重要的一点是告诉matplotlib使用无衬线字体(因为前卫字体中只有无衬线字体)。同样对于标签或其他数学模式,您需要告诉它也使用无衬线
from matplotlib import pyplot as plt
plt.rcParams.update({"text.usetex" : True,
'font.family' : 'sans-serif',
"text.latex.preamble" : r"\usepackage{avant} \usepackage{sansmath} \sansmath"})
x = ["Apples", "Bananas", "Cherry", "Dosenfrüchte"]
y = [400,500,300,100]
plt.bar(x,y, width=0.5, label="Fruits")
plt.legend()
plt.title("A juicy diagram")
plt.show()
有关更多通用信息,请查看