python - 如何从 matplotlib 条形图中获取数据
问题描述
如何以编程方式从 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,这里有几个相关的帖子(不进入条形图):
解决方案
matplotlib.pyplot.bar
返回BarContainer
对象 的 APImatplotlib.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)
推荐阅读
- django - 有没有办法在 Django Rest Framework 和 Flutter 应用程序之间传输图像?
- android - 我无法在适配器中使用 textview 显示整数值
- android - Gradle Daemon Android Studio“无法启动守护进程”?- 离子 V4
- python - 使用 Django 通道向前端发送数据
- javascript - @Inject 不能在带有打字稿的 vue 中工作
- c++ - 在基于范围的循环与标准循环中擦除矢量元素
- python - 如何遍历不同的 sci-kit 学习分类器
- javascript - 为什么将参数传递给单击事件侦听器函数会导致奇怪的结果?
- python-3.x - 如何打印对象方法的所有值?
- node.js - 快速会话不起作用,只是一个简单的代码