如何停止 ytick 的 Matplotlib 自动格式化?
How to Stop Matplotlib Auto-Formatting of ytick?
我正在制作几组不同数据的直方图,但在这个复杂列表的数据集中,我的 yticks 正在自动格式化。如何让我的 yticks 统一?
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)
plt.xticks(size = 8)
plt.yticks(size = 8)
plt.title('Hist_{0}chars_{1}_{2}'.format(windowSize,func.__name__,data_short_name))
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])
你可以看到下面的结果图。有问题的 ytick 是倒数第二个 ytick。
直方图 yticks 样式不统一:
正如我评论中提到的,有两个可能的问题。一是你不解为什么只有部分标签改变了大小。 Matplotlib 区分 major and minor ticks, and your approach only modifies the major y-ticks. This is easily resolved by accessing the axis object with 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 处理它们)并且您的直方图频率将始终为正。好吧。
我正在制作几组不同数据的直方图,但在这个复杂列表的数据集中,我的 yticks 正在自动格式化。如何让我的 yticks 统一?
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)
plt.xticks(size = 8)
plt.yticks(size = 8)
plt.title('Hist_{0}chars_{1}_{2}'.format(windowSize,func.__name__,data_short_name))
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])
你可以看到下面的结果图。有问题的 ytick 是倒数第二个 ytick。
直方图 yticks 样式不统一:
正如我评论中提到的,有两个可能的问题。一是你不解为什么只有部分标签改变了大小。 Matplotlib 区分 major and minor ticks, and your approach only modifies the major y-ticks. This is easily resolved by accessing the axis object with 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 处理它们)并且您的直方图频率将始终为正。好吧。