如何在使用usetex的情况下,为matplotlib的坐标轴标签(数字)设置Helvetica字体?

4

我将matplotlibrc参数文件中的字体样式设置为Helvetica(无衬线字体),这种字体已经安装好了,我将text.usetex设置为“true”,但坐标轴字体仍使用衬线字体。我的matplotlibrc参数文件如下:

lines.linewidth : 1.0               # Line width (in points).
lines.linestyle : -                 # Line style.
lines.color : blue                  # Line color.


font.family : sans-serif
font.sans-serif : Helvetica, Arial, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Avant Garde, sans-serif

text.color : black
text.usetex : true


axes.facecolor : FFFFFF         # axes background color
axes.edgecolor : 000000         # axes edge color
axes.linewidth : 1.0            # edge linewidth
axes.grid : true                # display grid or not
axes.titlesize : large          # fontsize of the axes title
axes.labelsize : 9              # fontsize of the x any y labels
axes.labelweight : normal       # weight of the x and y labels
axes.labelcolor : 000000
axes.axisbelow : true
axes.formatter.limits : -7, 7
axes.color_cycle : E41A1C, 377EB8, 4DAF4A, 984EA3, FF7F00, FFFF33, A65628, F781BF, 999999       # color cycle for plot lines
polaraxes.grid : true           # display grid on polar axes
axes3d.grid : true              # display grid on 3d axes


xtick.major.size : 4            # Major tick size (in points).
xtick.minor.size : 2            # Minor tick size (in points).
xtick.major.width : 1           # Major tick width (in points).
xtick.minor.width : 1           # Minor tick width (in points).
xtick.major.pad : 6             # Distance to major tick label (in points).
xtick.minor.pad : 6             # Distance to the minor tick label (in points).
xtick.color : 000000            # Tick label colors.
xtick.labelsize : 8             # Tick label font size (in points).
xtick.direction : in            # Tick direction

ytick.major.size : 4            # Major tick size (in points).
ytick.minor.size : 2            # Minor tick size (in points).
ytick.major.width : 1           # Major tick width (in points).
ytick.minor.width : 1           # Minor tick width (in points).
ytick.major.pad : 6             # Distance to major tick label (in points).
ytick.minor.pad : 6             # Distance to the minor tick label (in points).
ytick.color : 000000            # Tick label colors.
ytick.labelsize : 8             # Tick label font size (in points).
ytick.direction : in            # Tick direction


grid.color : 999999             # Grid color.
grid.linestyle : :              # Grid line style.
grid.linewidth : 0.5            # Grid line width (in points).
grid.alpha : 1.0                # Grid line transparency.


legend.fancybox : true      # if true, use a rounded box for the
legend.isaxes : true
legend.numpoints : 1            # the number of points in the legend line
legend.fontsize : 9
legend.borderpad : 0.5          # border whitespace in fontsize units
legend.markerscale : 1.0        # the relative size of legend markers vs. original
legend.labelspacing : 0.5       # the vertical space between the legend entries in fraction of fontsize
legend.handlelength : 2.        # the length of the legend lines in fraction of fontsize
legend.handleheight : 0.7       # the height of the legend handle in fraction of fontsize
legend.handletextpad : 0.8      # the space between the legend line and legend text in fraction of fontsize
legend.borderaxespad : 0.5      # the border between the axes and legend edge in fraction of fontsize
legend.columnspacing : 2.       # the border between the axes and legend edge in fraction of fontsize
legend.shadow : false
legend.frameon : true           # whether or not to draw a frame around legend
legend.scatterpoints : 1        # number of scatter points

如何让坐标轴的字体使用Helvetica sans字体?

更新:

我尝试使用以下代码:

plt.rcParams['font.family'] = 'Helvetica'

但是我遇到了这个错误:
/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Helvetica'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1246: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmb10.ttf
  UserWarning)
/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1246: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=large. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmb10.ttf
  UserWarning)

在matplotlibrc文件中,我注意到一件事情:font.sans-serif: Helvetica,Arial,Bitstream Vera Sans,...应该只有一个字体类型,即font.sans-serif: Helvetica - AGS
我相信如果字体不可用,它会循环浏览剩余的字体。 - Tom Kurushingal
你是否使用了其他的包(例如 seaborn 或者你自己导入的代码等),这些包可能会覆盖你默认的 matplotlibrc 设置? - Joe Kington
@JoeKington 不,我没有使用任何其他的包。 - Tom Kurushingal
这是一个与您之前提出的问题[(http://stackoverflow.com/questions/30834003/how-to-set-helvetica-font-in-figure-with-text-usetex-set-to-true-in-matplotlib)]不同的问题吗?我对这个主题一无所知,但听起来很相似... - Adam Smith
显示剩余3条评论
2个回答

2

1
将此内容添加到我的matplotlibrc文件中对我有效。
mathtext.fontset : custom
mathtext.it : Helvetica:italic

此外,我需要在/usr/local/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf目录中有Helvetica-Oblique.ttf。Olga Botvinnik在这里的回答和她的博客中有一些很好的说明。

请注意,您需要清除~/.matplotlib下的缓存才能刷新此内容。

Matplotlib表示不支持自定义字体集,这可能会在将来的Matplotlib更新中导致所有内容崩溃。


这实际上适用于 text.usetex:False(默认设置),即使用 matplotlib 的 mathtext 而不是 latex 进行渲染。 - herrlich10

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接