+1 vote
in Programming Languages by (60.0k points)

I am trying to set the xlabel of a subplot to some value. But the code returns an error: "AttributeError: 'AxesSubplot' object has no attribute 'xlabel'"

Here is the code:

fig, axs = plt.subplots(len(cols), len(cols), figsize=(12, 12))

axs[i, j].scatter(x, y, c=colors)

axs[i, j].xlabel(cols[i])

The line that gives the error has been highlighted. How can I fix it?

1 Answer

+2 votes
by (72.7k points)
selected by
 
Best answer

It is clear from the error the subplot does not have the 'xlabel' attribute. You need to use 'set_xlabel()' instead. 

Make the following change in your code, and it should fix the error.

fig, axs = plt.subplots(len(cols), len(cols), figsize=(12, 12))

axs[i, j].scatter(x, y, c=colors)

axs[i, j].set_xlabel(cols[i])


...