簡體   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