【问题标题】:Call function on yaxis of matplotlib histogrammatplotlib直方图y轴上的调用函数
【发布时间】:2018-03-20 10:03:20
【问题描述】:

我想用函数格式化直方图的 y 轴:

def convertCountToKm2(x):
    return x * 25.0 * 1e-6

因为这会将直方图的 y 轴从细胞计数转换为以 km2 为单位的面积。直方图由以下人员创建:

bins = numpy.array(list(range(0,7000,1000)))
plt.hist(Anonzero,bins)

下图中的结果:

我尝试使用以下代码调用该函数:

yFormat = tkr.FuncFormatter(convertCountToKm2)
plt.yaxis.set_major_formatter(yFormat)

返回错误:模块'matplotlib.pyplot' has no attribute 'yaxis'

How do I format axis number format to thousands with a comma in matplotlib? 似乎也有一些关于格式化轴的提示,但这并不是针对这种情况的。我无法用它来回答我的问题。

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    错误告诉你plt(我想是pyplot)没有yaxis,这是正确的。 yaxis 是坐标区的一个属性。使用

    plt.gca().yaxis.set_major_formatter(...)
    

    解决这个问题你可能会遇到另一个问题,即FuncFormatter 的函数需要接受两个参数x, pos。你可以忽略pos,但它必须在签名中

    def convertCountToKm2(x, pos=None):
        return x * 25.0 * 1e-6
    
    yFormat = FuncFormatter(convertCountToKm2)
    plt.gca().yaxis.set_major_formatter(yFormat)
    

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 2017-11-17
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 2014-03-17
      相关资源
      最近更新 更多