【问题标题】:Get values from matplotlib AxesSubplot从 matplotlib AxesSubplot 获取值
【发布时间】:2015-11-24 08:44:06
【问题描述】:

我想从matplotlib.axes.AxesSubplot 获取值,该值是从pandas.Series.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()
# I want to get values of hist variable.

我知道我可以使用np.histogram 获取直方图值,但我想使用 pandas hist 方法。

【问题讨论】:

  • 我不确定这是否可能:Pandas plotting.py source 似乎丢弃了 matplotlib 返回给它的分箱数据、分箱边缘和补丁对象。为什么不直接用plt.hist 绘图?

标签: python pandas matplotlib


【解决方案1】:

正如 xnx 在 cmets 中指出的那样,这不像您使用 plt.hist 那样容易访问。但是,如果您真的想使用 pandas 的 hist 函数,您可以从您调用 serie.hist 时添加到 hist AxesSubplotpatches 获取此信息.

这是一个循环遍历补丁的函数,并返回 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

这是要检查的直方图:

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 2022-01-17
    • 2020-12-16
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多