我正在制作幾組不同資料的直方圖,但在這個復雜性串列的資料集中,我的 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 的風格不統一:

uj5u.com熱心網友回復:
正如我在評論中提到的,有兩個可能的問題。一是你很困惑為什么只有一些標簽的大小發生了變化。Matplotlib 區分
至于 . 的格式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 處理它們),并且您的直方圖頻率將始終為正。那好吧。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406716.html
標籤:
