gpt4 book ai didi

python - 从 matplotlib AxesSubplot 获取值

转载 作者:太空狗 更新时间:2023-10-30 02:29:10 24 4
gpt4 key购买 nike

我想从从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com