简体   繁体   English

使用matplotlib绘制图例:错误

[英]Plotting a legend with matplotlib: error

I am trying to add a legend to my graph in matplotlib. 我正在尝试在matplotlib中为我的图表添加一个图例。 instead of creating a legend it puts the full list of all mylabels in the legend. 而不是创建一个图例,它将所有mylabels的完整列表放在图例中。

My graph looks like this: 我的图表看起来像这样: 在此输入图像描述

The legend is cut off and i cant see more than that, i assume due to its size. 传说是切断的,我不能看到更多,我假设由于它的大小。

This is my code: 这是我的代码:

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()

And AllMixedShapes2.csv looks like this: AllMixedShapes2.csv看起来像这样:

在此输入图像描述

I'm quite new to python and machine learning and ive tried other examples but i cant get anything to work. 我是python和机器学习的新手,我尝试了其他的例子,但我无法得到任何工作。

Matplotlib's label argument is meant to be a single string that labels the entire dataset, rather than an array of individual labels for the points within the dataset. Matplotlib的label参数是一个标记整个数据集的单个字符串,而不是数据集中各点的单个标签数组。 If you wish to pass an array of point-by-point labels that will be aggregated into a legend, the best option is probably the Seaborn library. 如果您希望传递将聚合到图例中的逐点标签数组,最佳选择可能是Seaborn库。 Seaborn provides a wrapper around matplotlib for more convenient statistical visualization. Seaborn为matplotlib提供了一个包装器,可以更方便地进行统计可视化。

This should do approximately what you wish to do with your data: 这应该与您希望对数据做的大致相同:

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

I'd suggest checking out the seaborn example gallery for more ideas. 我建议查看seaborn示例库以获取更多想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM