【发布时间】:2018-10-16 04:54:10
【问题描述】:
我想知道是否可以将 plt.bar 函数的结果存储到数组中。像
a=[1,2,3]
b=[21321,5345,654457]
height=list(b)
plt.bar(a,height=height)
a_b=result_of_pltbar
【问题讨论】:
标签: python python-2.7 numpy matplotlib
我想知道是否可以将 plt.bar 函数的结果存储到数组中。像
a=[1,2,3]
b=[21321,5345,654457]
height=list(b)
plt.bar(a,height=height)
a_b=result_of_pltbar
【问题讨论】:
标签: python python-2.7 numpy matplotlib
我不确定您将结果存储到数组中是什么意思,但这里有一些想法。
假设plt 由from matplotlib import pyplot as plt 导入,结果图如下所示:
b
a
现在除了条形的“边缘”之外,您已经拥有了其他一切。要获取当前轴,您可以使用ax = plt.gca()。如果您调查vars(ax),您会发现ax.patches 看起来特别有趣。它们包含有关条形的数据。以下是您可以从ax.patches[0] 找到的内容:
In [18]: vars(ax.patches[0])
Out[18]:
{'_stale': True,
....
'_x0': 0.6,
'_y0': 0,
'_width': 0.8,
'_height': 21321,
'_x1': 1.4,
'_y1': 21321,
....
}
从这里很容易看出,这些都是第一根柱的几何属性和位置坐标。所以,如果你想收集条的左边缘,你会使用
left_edges = [bar._x0 for bar in ax.patches]
这将导致[0.6, 1.6, 2.6]。获取bar的其他属性的过程应该很清楚了。
【讨论】: