我试图使用数据框来创建散点图。以下是数据框的示例:
----------------------------------------------
| Index | x | y | color | name |
----------------------------------------------
| 0 | 4.3 | 2.2 | 'b' | 'First'|
----------------------------------------------
| 1 | 2.3 | 3.2 | 'c' | 'Secd' |
----------------------------------------------
我用来绘制的代码如下所示:
plt.scatter(dframe['x'], dframe['y'], color=dframe['color'], label=dframe['name'])
plt.title('Title')
plt.xlabel('X label')
plt.ylabel('Y label')
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.legend(scatterpoints=1, loc='lower left', fontsize=10)
plt.show()
无论出于何种原因,这会在图例中添加1个项目,该项目的标签就是整个'名称'专栏重复2次。
如何让图例在名称列中单独显示每个项目,并仅显示该列一次?
谢谢!
作为信息,我尝试了以下内容无济于事:
plt.legend(dframe['name'], loc = 'lower....
for i in range(len(dframe['name'])):
plt.legend(dframe['name'][i], loc = ..
plt.legend([dframe['name']], loc = 'lower....
答案 0 :(得分:0)
plt.scatter
每次调用只能获得一个图例标签。如果您想为每个点添加标签,则必须执行以下操作:
for index, row in dframe.iterrows():
plt.scatter(row['x'], row['y'], color=row['color'], label=row['name'])
plt.title('Title')
plt.xlabel('X label')
plt.ylabel('Y label')
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.legend(scatterpoints=1, loc='lower left', fontsize=10)
plt.show()