I want to format the y-axis of my histogram with the function:
我想用函数来格式化直方图的y轴:
def convertCountToKm2(x):
return x * 25.0 * 1e-6
Because this converts the histogram y-axis from a cell count to an area in km2. The histogram is created by:
因为这将直方图y轴从细胞计数转换为km2中的面积。直方图由:
bins = numpy.array(list(range(0,7000,1000)))
plt.hist(Anonzero,bins)
Which results in this figure:
结果如下:
I have tried calling the function with the following code:
我尝试用以下代码调用函数:
yFormat = tkr.FuncFormatter(convertCountToKm2)
plt.yaxis.set_major_formatter(yFormat)
which returns an error: module 'matplotlib.pyplot' has no attribute 'yaxis'
返回一个错误:模块'matplotlib。pyplot没有属性“yaxis”
How do I format axis number format to thousands with a comma in matplotlib? seems too have some hints about formatting the axis, but that is not specific for this case. I am unable to use it to answer my question.
如何在matplotlib中使用逗号将轴号格式格式化为数千?似乎也有一些关于格式化轴的提示,但这并不是本例中所特有的。我不能用它来回答我的问题。
1 个解决方案
#1
2
The error tells you that plt
(which I suppose is pyplot
) has no yaxis
, which is correct. yaxis
is an attribute of the axes. Use
这个错误告诉您,plt(我认为是pyplot)没有yaxis,这是正确的。yaxis是坐标轴的一个属性。使用
plt.gca().yaxis.set_major_formatter(...)
Solving this you might run into another problem, that is that functions for the FuncFormatter
need to accept two arguments, x, pos
. You may ignore pos
, but it has to be in the signature
解决这个问题可能会遇到另一个问题,即FuncFormatter的函数需要接受两个参数x、pos
def convertCountToKm2(x, pos=None):
return x * 25.0 * 1e-6
yFormat = FuncFormatter(convertCountToKm2)
plt.gca().yaxis.set_major_formatter(yFormat)
#1
2
The error tells you that plt
(which I suppose is pyplot
) has no yaxis
, which is correct. yaxis
is an attribute of the axes. Use
这个错误告诉您,plt(我认为是pyplot)没有yaxis,这是正确的。yaxis是坐标轴的一个属性。使用
plt.gca().yaxis.set_major_formatter(...)
Solving this you might run into another problem, that is that functions for the FuncFormatter
need to accept two arguments, x, pos
. You may ignore pos
, but it has to be in the signature
解决这个问题可能会遇到另一个问题,即FuncFormatter的函数需要接受两个参数x、pos
def convertCountToKm2(x, pos=None):
return x * 25.0 * 1e-6
yFormat = FuncFormatter(convertCountToKm2)
plt.gca().yaxis.set_major_formatter(yFormat)