gpt4 book ai didi

python - 是什么导致此 NameError : name 'ax' is not defined in my Python code?

转载 作者:行者123 更新时间:2023-12-04 02:46:22 25 4
gpt4 key购买 nike

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

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:名称“ax”未定义

谁能告诉我是什么导致了这个问题?我尝试使用其他人,但 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)

关于python - 是什么导致此 NameError : name 'ax' is not defined in my Python code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57079159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com