【发布时间】:2017-04-06 07:21:42
【问题描述】:
我有以下简单的python代码:
import numpy as np
import matplotlib.pyplot as plt
plt.rc( 'font', size=20, family="Times" ) # use a font with serifs
# the following line triggers the problem
plt.rc( 'text', usetex=True ) # activate LaTeX text rendering
fig = plt.figure( figsize=(8,6) ) # (width,height) in inches
ax1 = fig.add_subplot( 1, 1, 1 ) # rows cols plotnumber
ax1.plot( np.linspace(1,10,10), np.linspace(1,10,10)**2 )
ax1.set_xlabel( r'\textit{x} in a.u.' )
ax1.set_ylabel( r'\textit{y} in a.u.' )
plt.show()
如您所见,与坐标轴标签相比,刻度标签的字体太细(或坐标轴标签太厚)。我发现这是由于激活了 LaTeX 文本渲染(参见代码中的注释),但我不知道如何更改它,因为我不想关闭 LaTeX 文本渲染。
知道为什么字体粗细(粗细的复数形式是什么?)不一致以及如何改变?
更新 1:根据 llap42 的建议,应该做一个 hack
plt.xticks([2, 4, 6, 8, 10], ['2', '4', '8', '10' ])
但这只是一种技巧,必须有更好的解决方案。
【问题讨论】:
-
看起来
usetex仅应用于标签。如果标签上有数字,它会更厚吗? (顺便说一句:厚度) -
@ElmarPeise 标签中的数字越来越粗,所以
usetex也适用于数字 -
对
plt.rc()的调用顺序是否重要?即您是否先尝试plt.rc('text', usetex=True),然后尝试plt.rc('font', size=20, family="Times")? -
问题是刻度标签不服从字体设置。尝试
plt.rc('font',family = 'sans-serif', size=20)时可以更好地看到这一点。正如can be seen,标签是无衬线的,但刻度标签不会改变。因此,与其询问“字体粗细”,不如将问题改写为为什么刻度标签不遵循字体设置。 -
我在 matplotlib 页面上提出了一个问题:github.com/matplotlib/matplotlib/issues/8436
标签: python matplotlib tex