首页 > 解决方案 > 执行图例函数时出现“名称'scatter'未定义”错误

问题描述

执行此程序时出现“散点词未定义错误”

fig, ax= plt.subplots()
ax.scatter(x=data['Odometer (KM)'],
           y=data['Price'],
           c=data['Doors'])
ax.set(title='Car sales',
       xlabel='Odometer',
       ylabel='Price')
#legend function
ax.legend(*scatter.legend_elements(), title='Doors');

标签: pythonmatplotlib

解决方案


问题是您没有定义实例分散。你为什么不试试这个?

'```

fig, ax= plt.subplots()

scatter = ax.scatter(x=data['Odometer (KM)'], y=data['Price'], c=data['Doors'])

ax.set(title='Car sales', xlabel='Odometer', ylabel='Price')

#legend function
ax.legend(*scatter.legend_elements(), title='Doors');

推荐阅读