如何将plt.bar的结果存储到数组中

时间:2022-02-01 01:48:53

I want to know of it's possible to store the result of the plt.bar function into an array. Something like

我想知道可以将plt.bar函数的结果存储到数组中。就像是

a=[1,2,3]
b=[21321,5345,654457]

height=list(b)

plt.bar(a,height=height)

a_b=result_of_pltbar

1 个解决方案

#1


0  

I am not sure what you mean by storing the result into an array, but here are some thoughts.

我不确定将结果存储到数组中是什么意思,但这里有一些想法。

Assuming that the plt is imported by from matplotlib import pyplot as plt the resulting figure looks like this:

假设plt是从matplotlib import pyplot导入为plt,结果图如下所示:

如何将plt.bar的结果存储到数组中

  • The heights of the bars are in b
  • 条形的高度在b中

  • The x-positions of the bars are stored in a
  • 条形的x位置存储在a中

Now you have everyting else but the "edges" of the bars. To get the current axis you may use ax = plt.gca(). If you investigate vars(ax) you'll see that ax.patches looks particularly interesting. They contain data about the bars. Here is what you will find from the ax.patches[0]:

现在你除了酒吧的“边缘”之外还有其他所有东西。要获得当前轴,您可以使用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,
....
}

From here it is easy to see that these are all the geometric properties and position coordinates for the first bar. So, if you would like to collect the left edges of the bars, you would use

从这里可以很容易地看出,这些是第一个柱的所有几何属性和位置坐标。所以,如果你想收集条的左边缘,你会使用

left_edges = [bar._x0 for bar in ax.patches]

which will result into [0.6, 1.6, 2.6]. The process of getting other properties of the bars should be clear.

这将导致[0.6,1.6,2.6]。获得酒吧的其他属性的过程应该是清楚的。

#1


0  

I am not sure what you mean by storing the result into an array, but here are some thoughts.

我不确定将结果存储到数组中是什么意思,但这里有一些想法。

Assuming that the plt is imported by from matplotlib import pyplot as plt the resulting figure looks like this:

假设plt是从matplotlib import pyplot导入为plt,结果图如下所示:

如何将plt.bar的结果存储到数组中

  • The heights of the bars are in b
  • 条形的高度在b中

  • The x-positions of the bars are stored in a
  • 条形的x位置存储在a中

Now you have everyting else but the "edges" of the bars. To get the current axis you may use ax = plt.gca(). If you investigate vars(ax) you'll see that ax.patches looks particularly interesting. They contain data about the bars. Here is what you will find from the ax.patches[0]:

现在你除了酒吧的“边缘”之外还有其他所有东西。要获得当前轴,您可以使用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,
....
}

From here it is easy to see that these are all the geometric properties and position coordinates for the first bar. So, if you would like to collect the left edges of the bars, you would use

从这里可以很容易地看出,这些是第一个柱的所有几何属性和位置坐标。所以,如果你想收集条的左边缘,你会使用

left_edges = [bar._x0 for bar in ax.patches]

which will result into [0.6, 1.6, 2.6]. The process of getting other properties of the bars should be clear.

这将导致[0.6,1.6,2.6]。获得酒吧的其他属性的过程应该是清楚的。