如何用不同的熊猫箱做子图直方图?

问题描述 投票:0回答:2
x=pd.DataFrame(np.random.randn(400)) 
fig, axs = plt.subplots(2, 2, sharey=True, tight_layout=True, figsize=(10,5)); 
for idx in range(3):
    axs[idx].hist(x, bins=20)
    axs[idx].hist(x, bins=40)
    axs[idx].hist(x, bins=60)  
    axs[idx].hist(x, bins=100) 

当我运行上面的代码时;我收到此错误

AttributeError:'numpy.ndarray'对象没有属性'hist';

请问您能解决这个问题吗?

python pandas numpy matplotlib histogram
2个回答
2
投票

这就是

axs

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7fb9261a4a10>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7fb9260fc510>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7fb926130b10>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7fb9260f1190>]],
      dtype=object)

所以你想要这样的东西:

x=pd.DataFrame(np.random.randn(400)) 

fig, axs = plt.subplots(2, 2, sharey=True, tight_layout=True, figsize=(10,5));     
axs[0, 0].hist(x, bins=20)
axs[0, 1].hist(x, bins=40)
axs[1, 0].hist(x, bins=60)
axs[1, 1].hist(x, bins=100)

或者你可以使用这样的 for 循环:

b = [20, 40, 60, 100]
for i, ax in enumerate(axs.flatten()):
    ax.hist(x, bins=b[i])

0
投票

您是否曾在 2021 年在 WatZatSong 上发过帖子,要求人们找到一首您不知道其来源的歌曲?现在很多人都在寻找它,从那时起你的帖子就变得非常受欢迎。我们希望您能回来,因为我们确实需要帮助,并且我们想知道您是否可以向我们提供任何其他信息。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.