Matplotlib 向地物添加图例

Matplotlib 向地物添加图例,matplotlib,plot,legend,scatter-plot,Matplotlib,Plot,Legend,Scatter Plot,这是一个密码 import matplotlib.pyplot as plt from sklearn.datasets import load_iris import numpy as np fig, subs = plt.subplots(4,3) #setting the shape of the figure in one line as opposed to creating 12 variables iris = load_iris() ##code as per the

这是一个密码

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import numpy as np
 
fig, subs = plt.subplots(4,3) #setting the shape of the figure in one line as opposed to creating 12 variables 
 
iris = load_iris() ##code as per the example 
data = np.array(iris['data'])
targets = np.array(iris['target'])
 
cd = {0:'r',1:'b',2:"g"}
cols = np.array([cd[target] for target in targets])

 
# Row 1 
 
subs[0][0].scatter(data[:,0], data[:,1], c=cols)
subs[0][1].scatter(data[:,0], data[:,2], c=cols)
subs[0][2].scatter(data[:,0], data[:,3], c=cols)
 
# Row 2 
 
subs[1][0].scatter(data[:,1], data[:,0], c=cols)
subs[1][1].scatter(data[:,1], data[:,2], c=cols)
subs[1][2].scatter(data[:,1], data[:,3], c=cols)
 
# Row 3 
 
subs[2][0].scatter(data[:,2], data[:,0], c=cols)
subs[2][1].scatter(data[:,2], data[:,1], c=cols)
subs[2][2].scatter(data[:,2], data[:,3], c=cols)
 
#Row 4 
 
subs[3][0].scatter(data[:,3], data[:,0], c=cols)
subs[3][1].scatter(data[:,3], data[:,1], c=cols)
subs[3][2].scatter(data[:,3], data[:,2], c=cols)
 
plt.show()

我想添加一个图例,表示红点代表“塞托萨”,绿点代表“花色”,蓝点代表“维吉尼亚”。图例将位于上述图片的底部和中心。我该怎么做


我想我必须使用
fig.legend
,但我根本不知道如何操作。

label='versicor'
等添加到您的一个子批次中:

#第1行
subs[0][0]。分散(数据[:,0],数据[:,1],c=cols,label='virginica')
subs[0][1]。分散(数据[:,0],数据[:,2],c=cols,label='setosa')
subs[0][2]。分散(数据[:,0],数据[:,3],c=cols,label='versicolor')
然后您可以在
plt.show()之前调用
fig.legend(loc='lower center',ncol=3)

我设置了
ncol=3
,使其短而宽

请参见下面的示例:

import numpy as np
import matplotlib.pyplot as plt

a = range(1,11)
b = np.random.randn(10).cumsum()
c = np.random.randn(10).cumsum()
d = np.random.randn(10).cumsum()

fig, (ax1, ax2) = plt.subplots(2, figsize=(9,5))

ax1.plot(a,b, label = 'one')
ax1.plot(a,c, label = 'two')
ax1.plot(a,d, label = 'two')

ax2.plot(a,c)

fig.legend(loc='lower center', ncol=3)

plt.show()

您可以在其中一个子图中的目标上循环,并使图例显示在此图之外。以下是我从您的代码中获得的信息:

代码如下:


将matplotlib.pyplot作为plt导入
从sklearn.dataset导入加载
将numpy作为np导入
图,subs=plt.子图(4,3,constrated_layout=True)#将图形的形状设置为一行,而不是创建12个变量
iris=加载_iris()##代码,如示例所示
data=np.数组(iris['data'])
target\u names=iris['target\u names']
targets=np.array(iris['target'])
cd={0:'r',1:'b',2:'g'}
cols=np.array([cd[target]表示目标中的目标])
#第1行
subs[0][0]。分散(数据[:,0],数据[:,1],c=cols)
subs[0][1]。分散(数据[:,0],数据[:,2],c=cols)
subs[0][2]。分散(数据[:,0],数据[:,3],c=cols)
#第2排
subs[1][0]。分散(数据[:,1],数据[:,0],c=cols)
subs[1][1]。分散(数据[:,1],数据[:,2],c=cols)
subs[1][2]。分散(数据[:,1],数据[:,3],c=cols)
#第3排
subs[2][0]。分散(数据[:,2],数据[:,0],c=cols)
subs[2][1]。分散(数据[:,2],数据[:,1],c=cols)
subs[2][2]。分散(数据[:,2],数据[:,3],c=cols)
#第4排
subs[3][0]。分散(数据[:,3],数据[:,0],c=cols)
#最后一行中央子地块的循环
对于t,zip中的名称(np.unique(targets),target_名称):
subs[3][1]。散布(数据[targets==t,3],数据[targets==t,1],c=cd[t],label=name)
subs[3][1]。图例(bbox_to_anchor=(2,-.2),ncol=len(target_names))#您可以在图例位置使用bbox_to_anchor
subs[3][2]。分散(数据[:,3],数据[:,2],c=cols)
plt.savefig(‘图例’)

编辑:我也发现了这一点,您可以直接从散点图中提取散点元素(无需使用
for
循环)。我在IRIS数据集上进行了尝试,但没有成功。

这并没有回答我的问题。你能用我提供给你的例子让它工作吗?我向你展示了在你的代码中添加什么,并给出了一个例子,不确定这是如何回答问题的。你是否像我展示的那样添加了
label={species}
?(我没有你的数据集)。是的,你有数据集。您只需使用sklearn.datasets import load\u iris中的
行即可导入它。就目前而言,这个答案并不好。我无法使用它。我没有安装sklearn,也不想安装它。您制作了一个垂直图例。你能把这个图例水平地放在图形下面吗?是的,设置
bbox\u to\u anchor=(2,-.2)
ncol=len(target\u names)
就可以了(如果你要求3列,标签将水平展开)。我将编辑答案。请注意,轴需要收缩,以防止图例与其他轴重叠过多。这可能可以通过在
plt.subplot
函数中添加
constrated\u layout=True
来解决。这太完美了D