简体   繁体   English

轴中科学记数法的字体大小不一致

[英]Inconsistent font size for scientific notation in axis

I'm trying to make a plot for which the x axis will show in scientific notation.我正在尝试绘制一个 x 轴将以科学记数法显示的图。 The way I found how to do so is to use the ticklabel_format function.我找到的方法是使用ticklabel_format函数。 Unfortunately this does not respect the font size I assign to the numbers shown in the axis, see image below:不幸的是,这不尊重我分配给轴中显示的数字的字体大小,请参见下图:

在此处输入图片说明

The 1e-12 and 1e4 are displayed in a different font size even though I set equal label sizes.即使我设置了相同的标签大小, 1e-121e4也会以不同的字体大小显示。

How could I fix this?我怎么能解决这个问题? (A MWE is below) (下面是MWE

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 10000.0, 10.)
s = np.sin(np.pi*t)*np.exp(-t*0.0001)

fig, ax = plt.subplots()

ax.tick_params(axis='both', which='major', labelsize=7)
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0), labelsize=7)
plt.plot(t,s)

plt.show()

According to ticklabel_format documentation , the function does not accept labelsize parameter.根据ticklabel_format文档,该函数不接受labelsize参数。

You can change the font size using matplotlib.rc :您可以使用matplotlib.rc更改字体大小:

plt.rc('font', size=7)

在此处输入图片说明

I guess it's because that scientific representation is not treated as tick label, you can use:我想这是因为该科学表示不被视为刻度标签,您可以使用:

import matplotlib
matplotlib.rc('font', size=7)

or或者

matplotlib.rcParams['font.size']=7

and remove labelsize=7 in ax.tick_params和删除labelsize=7ax.tick_params

我认为这会对您有所帮助,而无需更改全局设置:

ax.yaxis.get_offset_text().set_fontsize(size)

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

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