[英]How to get data from matplotlib bar chart
如何以編程方式從 matplotlib 條形圖中檢索數據? 我可以為 matplotlib 折線圖做如下,所以也許我相當接近:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
axis = plt.gca()
line = axis.lines[0]
x_plot, y_plot = line.get_xydata().T
print("x_plot: ", x_plot)
print("y_plot: ", y_plot)
但是,對於條形圖,沒有線條,我不清楚等效的 object 是什么:
plt.bar([1,2,3], [4,5,6])
axis = plt.gca()
???
FWIW,這里有幾個相關的帖子(沒有 go 進入條形圖):
matplotlib.pyplot.bar
返回BarContainer
object
matplotlib.patches.Rectangle
提供了Patch
方法的完整說明。import matplotlib.pyplot as plt
rects = plt.bar([1,2,3], [4,5,6])
for rect in rects:
print(rect)
xy = rect.get_xy()
x = rect.get_x()
y = rect.get_y()
height = rect.get_height()
width = rect.get_width()
[out]:
Rectangle(xy=(0.6, 0), width=0.8, height=4, angle=0)
Rectangle(xy=(1.6, 0), width=0.8, height=5, angle=0)
Rectangle(xy=(2.6, 0), width=0.8, height=6, angle=0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.