简体   繁体   English

Matplotlib:刻度标签与字体设置不一致(LaTeX文本示例)

[英]Matplotlib: tick labels are inconsist with font setting (LaTeX text example)

I have the following simple python code: 我有以下简单的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()

This results in the following figure: 这导致下图: 刻度标签中的字体粗细错误

As you can see, the tick-labels have a too thin font compared with the axes-labels (or the axes-labels are too thick). 如您所见,与轴标签(或轴标签太厚)相比,刻度标签的字体太细。 I have found out that this is due to activating the LaTeX text rendering (see comment in the code), but I have no clue how to change this as I do not want to switch the LaTeX text rendering off. 我发现这是由于激活了LaTeX文本渲染(请参阅代码中的注释),但我不知道如何更改它,因为我不想关闭LaTeX文本渲染。

Any idea why the font-thickness (what is the plural of thickness?) is inconsistent and how to change that? 知道为什么字体厚度(厚度的复数是多少?)是不一致的以及如何改变它?

Update 1 : Following the suggestion from llap42 , a hack would be to do 更新1 :根据llap42的建议,黑客将会这样做

plt.xticks([2, 4, 6, 8, 10], ['2', '4', '8', '10' ])

But that is only a hack and there has to be a better solution. 但这只是一个黑客,必须有一个更好的解决方案。

As said in the comments, this is rather an issue of ticklabels not obeying the font setting when used with latex. 如评论中所述,这与使用乳胶时不符合字体设置的ticklabels相关。

This issue seems to only occur when using a ScalarFormatter (which is the default formatter for axes). 只有在使用ScalarFormatter (它是轴的默认格式化程序)时才会出现此问题。 I've posted an issue about this on GitHub. 我在GitHub上发布了一个关于此的问题

A workaround may be to use a different Formatter. 解决方法可能是使用不同的Formatter。 Eg a 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()

在此输入图像描述

An alternative solution is to set the font latex uses to a sans-serif font. 另一种解决方案是将字体latex使用设置为sans-serif字体。 How to achieve this is explained over on tex.stackexchange . tex.stackexchange上解释了如何实现这一点。

One solution would be to use the sfmath latex package. 一种解决方案是使用sfmath乳胶包。 To add it to the preamble, use 要将其添加到序言中,请使用

    plt.rc('text.latex', preamble=r'\usepackage[cm]{sfmath}')

This also works for logarithmic scales, where the other proposed solution fails. 这也适用于对数标度, 其他提议的解决方案失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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