【问题标题】:'numpy.ndarray' object has no attribute 'xaxis' - not sure why\'numpy.ndarray\' 对象没有属性 \'xaxis\' - 不知道为什么
【发布时间】:2022-12-03 09:43:03
【问题描述】:

我有以下代码。

我在尝试着循环遍历数据框“out”并创建一个单独的子图每个组和级别.

共有35组5级,制作共175地块.

因此我想创造5 位数每个都有35 个子图(7 行和 5 列)。

但是,当我尝试将特定图分配给不同的轴时,出现错误:'numpy.ndarray' 对象没有属性 'xaxis'

我将非常感谢您的帮助!

我在下面附上了一些示例数据。

for j in range(0,len(individualoutliers)):
    fig = plt.figure(figsize=(50,50))
    fig,axes = plt.subplots(7,5)
    for i in range(0,len(individualoutliers[j])):
        individualoutliersnew = individualoutliers[j]
        out = individualoutliersnew.loc[:, ["newID", "x", "y","level"]].apply(lambda x: pd.Series(x).explode())
        for k,g in out.groupby("newID"):
            globals()['interestingvariable'] = g 
            newframe = interestingvariable 
            sns.lineplot(data=newframe,x='x',y='y',ax=axes[i])
            axes[i].set_xlabel('x-coordinate',labelpad = 40,fontsize=70,weight='bold')
            axes[i].set_ylabel('y-coordinate',labelpad = 40,fontsize=70,weight='bold')
            plt.xticks(weight='bold',fontsize=60,rotation = 30)
            plt.yticks(weight='bold',fontsize=60)
            title = (newframe.iloc[0,0]+' '+'level'+' '+str(newframe.iloc[i,3]))
            axes[i].set_title(title,fontsize=70,pad=40,weight='bold') 
        dir_name = "/Users/macbook/Desktop/"
        plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname(dir_name))
        plt.savefig(newframe.iloc[0,0]+' '+'level'+' '+str(newframe.iloc[i,3])+'individualoutlierplot')
        plt.show()
out.head(10)

newID   x   y   level
24  610020  55  60  1
24  610020  55  60  1
24  610020  55  60  1
24  610020  60  60  1
24  610020  60  65  1
24  610020  60  65  1
24  610020  65  70  1
24  610020  70  70  1
24  610020  70  75  1
24  610020  75  75  1
newframe.head(10)

newID   x   y   level
3313    5d254d  55  60  1
3313    5d254d  55  60  1
3313    5d254d  55  60  1
3313    5d254d  60  60  1
3313    5d254d  60  65  1
3313    5d254d  60  65  1
3313    5d254d  65  65  1
3313    5d254d  65  70  1
3313    5d254d  70  75  1
3313    5d254d  75  75  1

【问题讨论】:

  • 错误消息指向代码中的哪一行?
  • sns.lineplot(data=newframe,x='x',y='y',ax=axes[i]) :)
  • 也许你可以试试fig,axesmatrix = plt.subplots(7,5),然后是axes = axesmatrix.flatten()
  • 非常感谢您的建议 - 但遗憾的是这似乎不起作用:(

标签: python pandas loops matplotlib jupyter-notebook


【解决方案1】:

fig,axes = plt.subplots(7,5) 中,axes 是一个二维轴数组(实际上是 x、y 轴对)。

sns.lineplot(data=newframe,x='x',y='y',ax=axes[i])中,您传递的是一维数组axes[i],而不是lineplot可能期望的单轴(对)。

【讨论】:

    猜你喜欢
    • 2023-01-05
    • 2020-09-22
    • 2020-02-21
    • 2020-12-08
    • 2019-12-10
    • 2017-07-10
    • 2020-05-10
    • 2019-11-04
    • 2017-05-24
    相关资源
    最近更新 更多