如何从matplotllib中的AxesSubplot对象获取BarContainer对象? [英] How to get a BarContainer Object from an AxesSubplot object in matplotllib?
问题描述
我对matplotlib相当陌生,但据我了解:
I'm fairly new with matplotlib, but as I understand:
fig,ax = plt.subplot()
ax.bar(x,y)
或
bar = ax.bar(x,y)
返回BarContainer类型的对象.由此我们可以 bar.axes
来获得绘制该轴的轴.
returns a BarContainer type object. From this we can bar.axes
to get the axes ax on which this is plotted.
但是如果我们有一个轴 ax
,并且没有将barplot显式存储在 bar
对象中该怎么办.现在,我们继续对轴 ax
进行大量更改,例如 ax.position ax.legend
等.现在我们有了一个 AxesSubplot
类型的对象 ax
....由此,我们如何提取 BarContainer
类型的对象?
but what if we have an axes ax
and we didn't explicitly store the barplot in a bar
object.
Now we go ahead and do a whole lot of changes on the axes ax
, as in ax.position ax.legend
etc etc.
Now we have an AxesSubplot
type object ax
....from this, how can we extract our BarContainer
type object?
我有一个接受可下标 BarContainer
对象的函数,到处理结束时,我只有一个 AxesSubplot
类型的对象...我不知道找出如何从另一个中获取一个..我们可以从 AxesSubplot
对象获取艺术家,标签,轴等,但是我看不到从中获取 Container
类型对象的方法.请协助,谢谢!
I have a function that accepts the subscriptable BarContainer
object, and by the end of my processing I only have an AxesSubplot
type object... I can't figure out how to get one from the other..
We can get artists, labels, axis etc etc from the AxesSubplot
object but I see no way to get the Container
type object from it. Please assist, Thanks!
推荐答案
您可以使用 ax.containers
fig, ax = plt.subplots()
ax.bar(x, y)
con = ax.containers
print(con)
# [<BarContainer object of 3 artists>]
这篇关于如何从matplotllib中的AxesSubplot对象获取BarContainer对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!