繁体   English   中英

访问 matplotlib 直方图轴中的数据

[英]Access data in matplotlib histogram axes

绘制直方图的 matplotlib ax object 中存储的数据在哪里?

我的场景:我写了一个 function,它使用 matplotlib 绘制了一个自定义直方图。 我正在编写单元测试并想测试绘制的数据是否

理想行为:

import matplotlib.pyplot as plt

f, ax = plt.subplots()
ax.hist(some_data)
data_i_want = ax.plotted_data

我不确定您到底想达到什么目标,但 plt.hist(...) function 返回直方图的数据:

histinfo = plt.hist(data)
histinfo[0] #This is the information about the # of instances
histinfo[1] #This is the information about the position of the bins

如果您想不惜一切代价从 plot 本身获取信息(假设您有条形图):

container = ax.containers[0] #https://matplotlib.org/3.1.1/api/container_api.html
for rect in container: #https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.patches.Rectangle.html#matplotlib.patches.Rectangle
    print(rect.xy)

您可以获取容器,这些容器将包含有关已在注释 URL 中绘制的条形(矩形)的信息,您可以找到有关此的所有信息。

Ps.:您可能必须针对特定实例调整代码,但这是从 plot 获取一些信息的一种方式。 (可能有更好的方法来做到这一点,这是我所知道的最好的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM