问题描述
我想从matplotlib.axes.AxesSubplot中获取值.html"rel =" nofollow> pandas.Series.hist 方法.有什么方法可以这样做吗?我在列表中找不到该属性.
I'd like to get values from matplotlib.axes.AxesSubplot
which is returned from pandas.Series.hist method.Is there any method to do so? I couldn't find the attribute in the list.
import pandas as pd
import matplotlib.pyplot as plt
serie = pd.Series([0.0,950.0,-70.0,812.0,0.0,-90.0,0.0,0.0,-90.0,0.0,-64.0,208.0,0.0,-90.0,0.0,-80.0,0.0,0.0,-80.0,-48.0,840.0,-100.0,190.0,130.0,-100.0,-100.0,0.0,-50.0,0.0,-100.0,-100.0,0.0,-90.0,0.0,-90.0,-90.0,63.0,-90.0,0.0,0.0,-90.0,-80.0,0.0,])
hist = serie.hist()
# I want to get values of hist variable.
我知道我可以使用np.histogram
获取直方图值,但是我想使用pandas hist方法.
I know I can get histogram values with np.histogram
, but I want to use pandas hist method.
推荐答案
正如xnx
在注释中指出的那样,这并不像使用plt.hist
那样容易访问.但是,如果您确实要使用熊猫hist
函数,则可以从调用serie.hist
时添加到hist
AxesSubplot
的patches
中获取此信息.
As xnx
pointed out in comments, this isn't as easily accessible as if you used plt.hist
. However, if you really want to use the pandas hist
function, you can get this information, from the patches
that are added to the hist
AxesSubplot
when you call serie.hist
.
这是一个遍历补丁的功能,并返回垃圾箱边缘和直方图计数:
Here's a function to loop through the patches, and return the bin edges and histogram counts:
import pandas as pd
import matplotlib.pyplot as plt
serie = pd.Series([0.0,950.0,-70.0,812.0,0.0,-90.0,0.0,0.0,-90.0,0.0,-64.0,208.0,0.0,-90.0,0.0,-80.0,0.0,0.0,-80.0,-48.0,840.0,-100.0,190.0,130.0,-100.0,-100.0,0.0,-50.0,0.0,-100.0,-100.0,0.0,-90.0,0.0,-90.0,-90.0,63.0,-90.0,0.0,0.0,-90.0,-80.0,0.0,])
hist = serie.hist()
def get_hist(ax):
n,bins = [],[]
for rect in ax.patches:
((x0, y0), (x1, y1)) = rect.get_bbox().get_points()
n.append(y1-y0)
bins.append(x0) # left edge of each bin
bins.append(x1) # also get right edge of last bin
return n,bins
n, bins = get_hist(hist)
print n
print bins
plt.show()
这是n
和bins
的输出:
[36.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 1.0] # n
[-100.0, 5.0, 110.0, 215.0, 320.0, 425.0, 530.0, 635.0, 740.0, 845.0, 950.0] # bins
这是要检查的直方图:
这篇关于从matplotlib AxesSubplot获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!