2016-11-13 76 views
0

我正在使用熊猫绘图函数绘制直方图子图。熊猫绘图函数返回np.array - 如何让它返回AxesSubplot

df.plot.hist(subplots = True) 

我想为每个图添加一条垂直线。我知道你可以使用

plt.axvlines 

在matplotlib阴谋做到这一点,但我怎么访问由大熊猫产生的次要情节个人? 我看着:Is there a way to pass different vertical lines to each subplot when using pandas histogram with "by=somevar"? 但有人能给我一个如何做到这一点的实际例子?
谢谢!

回答

0

DataFrame.hist()返回Axes对象的列表。你只需要遍历每个轴并添加垂直线

df = pd.DataFrame(np.random.random((100,5))) 
axs = df.plot.hist(subplots = True) 
for ax in axs: 
    ax.axvline(0.5) 
+0

我得到这个错误与您的解决方案:AttributeError的:“numpy.ndarray”对象有没有属性“axvline” –

+0

我在这里看到的 - HTTP:// pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.plot.hist.html - 该df.plot.hist生成Axes对象或一个numpy数组。这显然是给我一个错误。任何想法,而不是获得轴列表? –

+0

你必须提供一个示例DataFrame。在我的测试中,我从来没有能够返回任何其他的东西,除了1个'Axes'对象,或者'Axes'列表。你尝试过使用'DataFrame.hist()'而不是'DataFrame.plot.hist()'吗? –