【问题标题】:How to get data from matplotlib bar chart如何从 matplotlib 条形图中获取数据
【发布时间】:2020-12-03 20:38:03
【问题描述】:

如何以编程方式从 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)

但是,对于条形图,没有线条,我不清楚等效对象是什么:

plt.bar([1,2,3], [4,5,6])
axis = plt.gca()
???

FWIW,这里有一些相关的帖子(不进入条形图):

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:
    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)
    

    【讨论】:

    • 作为后续,有没有办法从绘图对象中获取 BarContainer,而不是调用“bar”的返回?
    • 没关系,找到答案了:ax = plot.gca() con = ax.containers print(con) from stackoverflow.com/questions/62189677/…
    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多