Skip to content Skip to sidebar Skip to footer

What Causes This NameError: Name 'ax' Is Not Defined In My Python Code?

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'

Solution 1:

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)

Post a Comment for "What Causes This NameError: Name 'ax' Is Not Defined In My Python Code?"