是什么原因导致此 NameError: name 'ax' is not Define in my Python code?

2023-12-06

所以我想用这段代码构建一个折线图:

x_data = df['Product Type']
y_data = df['Total Amount']

def lineplot(x_data, y_data, x_label="Product Type", y_label="Total Amount", title="Sales"):
    __, ax = plt.subplots()

    ax.plot(x_data, y_data, lw=3, color ='#539caf', alpha =1)

ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)

但它只会产生以下错误消息:NameError: name 'ax' is not defined.

任何人都可以告诉我什么会导致这个问题?我尝试使用其他人,但似乎ax.plot在 Python 的数据可视化中很常见,所以我认为我需要正确处理这一点。谢谢你!


您需要修复最后 3 行的缩进,然后单独调用该函数。

x_data = df['Product Type']
y_data = df['Total Amount']

def lineplot(x_data, y_data, x_label="Product Type", y_label="Total Amount", title="Sales"):
    __, ax = plt.subplots()

    ax.plot(x_data, y_data, lw=3, color ='#539caf', alpha =1)

    ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

lineplot(x_data, y_data)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是什么原因导致此 NameError: name 'ax' is not Define in my Python code? 的相关文章

随机推荐