是什么原因导致此NameError:我的Python代码中未定义名称"ax"? [英] What causes this NameError: name 'ax' is not defined in my Python code?

查看:205
本文介绍了是什么原因导致此NameError:我的Python代码中未定义名称"ax"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想用以下代码构建折线图:

So I want to build a line chart with this code:

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' 未定义.

But it only produces this error message: NameError: name 'ax' is not defined.

谁能告诉我是什么导致了这个问题?我尝试使用其他工具,但是在Python的数据可视化中, ax.plot 似乎很常见,所以我认为我需要弄清楚这一点.谢谢!

Anybody can tell me what can cause this problem? I tried using others, but it appears that ax.plot is very common in data visualization in Python, so I think I need to get this right. Thank you!

推荐答案

你需要在最后 3 行修复你的缩进,然后单独调用函数.

You need to fix your indentation on the last 3 lines, then call the function seperately.

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)

这篇关于是什么原因导致此NameError:我的Python代码中未定义名称"ax"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆