从matplotlib AxesSubplot获取值

时间:2015-11-24 08:44:07

标签: python pandas matplotlib

我想从matplotlib.axes.AxesSubplot方法获取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

我知道我可以使用base64获得直方图值,但我想使用pandas hist方法。

1 个答案:

答案 0 :(得分:5)

正如xnx在评论中指出的那样,这并不像您使用plt.hist那样容易访问。但是,如果您真的想使用pandas hist函数,那么可以从添加到patches {{1}的hist中获取此信息当你致电AxesSubplot

这是循环补丁的函数,并返回bin边和直方图计数:

serie.hist

以下是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

这是要检查的直方图:

enter image description here