【问题标题】:How to Stop Matplotlib Auto-Formatting of ytick?如何停止 ytick 的 Matplotlib 自动格式化?
【发布时间】:2022-01-08 15:45:38
【问题描述】:

我正在制作几组不同数据的直方图,但在这个复杂性列表的数据集中,我的 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 样式不统一:

【问题讨论】:

  • 您认为这里的“不统一”是什么?文本大小的差异或几十年在“10^^-1”之前没有系数“1 x”?还是两者兼而有之?

标签: python matplotlib histogram


【解决方案1】:

正如我在评论中提到的,有两个可能的问题。一是你很困惑为什么只有一些标签的大小发生了变化。 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 处理它们)并且您的直方图频率将始终为正。哦,好吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2021-04-23
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2020-03-26
    • 2016-03-16
    相关资源
    最近更新 更多