使用matplotlib绘制图例:错误

时间:2017-04-05 22:55:58

标签: python numpy matplotlib plot legend

我正在尝试在matplotlib中为图表添加图例。而不是创建一个图例,它将所有mylabels的完整列表放在图例中。

我的图表如下所示: enter image description here

传说被切断,我看不到更多,我认为由于它的大小。

这是我的代码:

features2 = ["Number of Sides"]
features3 = ["Largest Angle"]
header2 = ["Label"]

data_df = pd.DataFrame.from_csv("AllMixedShapes2.csv")
X1 = np.array(data_df[features2].values)
y1 = np.array(data_df[features3].values)
l = np.array(data_df[header2].values)

plt.scatter(X1[:, 0],y1, c=y, cmap=plt.cm.Paired, label=l)
plt.axis([0, 17, 0, 200])
plt.ylabel("Maximum Angle (Degrees)")
plt.xlabel("Number Of Sides")
plt.title('Original 450 Test Shapes')

plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()

AllMixedShapes2.csv如下所示:

enter image description here

我是python和机器学习的新手,我尝试过其他例子,但我无法工作。

1 个答案:

答案 0 :(得分:0)

Matplotlib的label参数是一个标记整个数据集的单个字符串,而不是数据集中各点的单个标签数组。如果您希望传递将聚合到图例中的逐点标签数组,则最佳选项可能是Seaborn库。 Seaborn为matplotlib提供了一个包装器,可以更方便地进行统计可视化。

这应该与您希望对数据做什么大致相同:

import seaborn
seaborn.lmplot('Number of Sides', 'Largest Angle', hue='Label',
               data=data_df, fit_reg=False)

我建议您查看seaborn example gallery以获取更多想法。