作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想从从 pandas.Series.hist 返回的 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
获取直方图值,但我想使用 pandas hist 方法。
最佳答案
正如 xnx
在评论中指出的那样,这并不像您使用 plt.hist
那样容易访问。但是,如果您真的想使用 pandas hist
函数,您可以从添加到 的
patches
中获取此信息>histAxesSubplot
当你调用 serie.hist
时。
这是一个循环遍历补丁并返回 bin 边缘和直方图计数的函数:
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
这是要检查的直方图:
关于python - 从 matplotlib AxesSubplot 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888973/
我是一名优秀的程序员,十分优秀!