python - 从 matplotlib AxesSubplot 获取值

标签 python pandas matplotlib

我想从从 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 中获取此信息>hist AxesSubplot 当你调用 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()

这是 nbins 的输出:

[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

这是要检查的直方图:

enter image description here

关于python - 从 matplotlib AxesSubplot 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888973/

相关文章:

python - Python 读取行 (os x) 中的 `set editing-mode vi`

python - 如何用上面值的平均值填充数据框中的 NaN 值?

python - 用来自不同数据帧的数据有条件地填充 pandas 列

python - 如何使时间列成为绘图的x轴?

python - 使用 os.popen3() 在 python 中提取视频的缩略图

Python 和 CGI​​ : Prevent resending form data upon refreshing

matplotlib - matplotlib 中对数极坐标图轴标签的定位

python - 使用 matplotlib 在 NFS 文件系统上保存图形

Python 解析子进程输出

python - 使用 Pandas 切片和连接?