正如我在评论中提到的,有两个可能的问题。一是你很困惑为什么只有一些标签的大小发生了变化。 Matplotlib 区分 major and minor ticks,您的方法仅修改主要的 y 刻度。这很容易通过使用ax.tick_params() 访问轴对象来解决:
import matplotlib.pyplot as plt
def make_histogram(listOfAllComplexities):
plt.hist(listOfAllComplexities,ec='black',color='orange',log=True, density=True,bins=5)
plt.xlabel(r'$\tilde{K}(x)$')
plt.ylabel('Frequency',labelpad=20)
ax = plt.gca()
ax.tick_params(axis="both", which="both", labelsize=8)
plt.title('Hist_{0}chars_{1}_{2}'.format(" A ", " B ", " C "))
plt.show()
make_histogram([7.0, 8.1, 7.0, 7.0, 9.3, 7.0, 8.1, 9.3, 7.0, 7.0, 7.0, 5.8, 7.0, 8.1, 9.3, 7.0, 8.1, 7.0, 5.8, 9.3, 5.8, 7.0, 7.0, 8.1, 8.1, 7.0, 8.1, 2.3, 7.0, 5.8, 8.1, 2.3])
示例输出:
至于10^-1 的格式,我认为这是matplotlib 团队有意识的决定,因此可以清楚地看到主要刻度的几十年。但是,我们可以构建自己的 FuncFormatter 来模仿用于次要刻度的样式:
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import math
def numfmt(x, pos):
sign_string = ""
if x<0:
sign_string = "-"
x = math.fabs(x)
if x == 0:
return r'$\mathdefault{0}$'
base = 10
exponent = math.floor(math.log10(x))
coeff = round(x / (base ** exponent))
return r'$\mathdefault{%s%g\times%s^{%d}}$' % (sign_string, coeff, base, exponent)
myfmt = tkr.FuncFormatter(numfmt)
def make_histogram(listOfAllComplexities):
plt.hist(listOfAllComplexities,ec='black',color='orange',log=True, density=True,bins=5)
plt.xlabel(r'$\tilde{K}(x)$')
plt.ylabel('Frequency',labelpad=20)
ax = plt.gca()
ax.tick_params(axis="both", which="both", labelsize=8)
ax.yaxis.set_major_formatter(myfmt)
plt.title('Hist_{0}chars_{1}_{2}'.format(" A ", " B ", " C "))
plt.show()
make_histogram([7.0, 8.1, 7.0, 7.0, 9.3, 7.0, 8.1, 9.3, 7.0, 7.0, 7.0, 5.8, 7.0, 8.1, 9.3, 7.0, 8.1, 7.0, 5.8, 9.3, 5.8, 7.0, 7.0, 8.1, 8.1, 7.0, 8.1, 2.3, 7.0, 5.8, 8.1, 2.3])
示例输出:
FuncFormatter 函数 numfmt() 是最重要的,因为我只是回顾性地注意到我们不需要它来处理次要的刻度(让 matplotlib 处理它们)并且您的直方图频率将始终为正。哦,好吧。