【发布时间】:2021-10-09 11:59:09
【问题描述】:
我想做一个由两部分组合而成的图例,第一个是标签的名称,第二个是标签对应的编号,例如: 0:50; 1:50; 2:50
现在我的代码是这样的,我厌倦了通过 Counter 函数添加它的数量,但是,它显示错误: 只能将元组(不是“str”)连接到元组
import matplotlib.pyplot as plt
from sklearn import datasets
from collection import Counter
iris = datasets.load_iris()
X = iris.data
y = iris.target
df = pd.DataFrame(X, columns = iris.feature_names)
fig, ax = plt.subplots(figsize=(12,8))
points = ax.scatter(df.values[:,0],
df.values[:,1],
c = y)
legend1 = ax.legend(*points.legend_elements() +':' + list(Counter(y).values()), loc = "lower left",title = 'clusters')
#ax.add_artist(legend1)
#handles, labels = points.legend_elements(prop = 'sizes')
#legend2 = ax.legend(handles, labels, loc='upper right')
plt.show()
【问题讨论】:
标签: python matplotlib scatter-plot