我有以下简单的python代码:
导入numpy为np将matplotlib.pyplot导入为plt
plt.rc(‘font’,size = 20,family =“Times”)#使用带衬线的字体
#以下行触发…
如评论中所述,这与使用乳胶时不符合字体设置的ticklabels相关。
这个问题似乎只发生在使用a时 ScalarFormatter (这是轴的默认格式化程序)。我发布了 一个问题 关于这个在GitHub上。
ScalarFormatter
解决方法可能是使用不同的Formatter。例如 StrMethodFormatter :
StrMethodFormatter
import matplotlib.pyplot as plt import matplotlib.ticker plt.rc( 'text', usetex=True ) plt.rc('font',family = 'sans-serif', size=20) fig , ax = plt.subplots(figsize=(5,3)) ax.set_xlabel( r'\textit{x} in a.u.' ) ax.set_ylabel( r'\textit{y} in a.u.' ) fmt = matplotlib.ticker.StrMethodFormatter("{x}") ax.xaxis.set_major_formatter(fmt) ax.yaxis.set_major_formatter(fmt) plt.tight_layout() plt.show()