<p>可以使用以下命令将图例移动到图表中需要的位置。在</p>
<p>绘制条形图时需要添加标签。我改变了你描绘传奇的主线。在</p>
<p>我添加了一些伪标签,在您的代码中,您可以通过执行<code>labels = list(df)</code>来获得标签,为您提供数据帧中的列名列表。在</p>
<pre><code>import matplotlib.pyplot as plt
colorscheme = ['r','b','c','y']
fig1 = plt.figure()
accuracyFig = fig1.add_subplot(221)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
accuracyFig.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])
accuracyFig1 = fig1.add_subplot(223)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
accuracyFig1.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])
accuracyFig2 = fig1.add_subplot(222)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
accuracyFig2.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])
accuracyFig3 = fig1.add_subplot(224)
A =[1,2,3,4]
B = [4,3,2,1]
labels = ['perceptron','C2','C3','C4']
for i in range(0,len(A)):
accuracyFig3.bar(A[i],B[i], align = 'center',label = labels[i], color = colorscheme[i])
# Plot the legend:
# You don't want to plot to any particular axis, instead to a general plot.
plt.legend(loc = 'lower center',bbox_to_anchor = (0,-0.3,1,1),
bbox_transform = plt.gcf().transFigure)
plt.show()
</code></pre>
<p>图例图来源:</p>
<p><a href="https://stackoverflow.com/questions/18974928/how-to-create-custom-legend-in-matplotlib-based-on-the-value-of-the-barplot">How to create custom legend in matplotlib based on the value of the barplot?</a>
<a href="https://stackoverflow.com/questions/9834452/how-do-i-make-a-single-legend-for-many-subplots-with-matplotlib">how do I make a single legend for many subplots with matplotlib?</a>
<a href="https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/4701285#4701285">How to put the legend out of the plot</a></p>
<p><a href="https://i.stack.imgur.com/2W6r8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2W6r8.png" alt="enter image description here"/></a></p>
<p>更新:意外删除了我的评论:在<code>legend()</code>中添加<code>ncol = 2</code>将得到您想要的对称分割行为。在</p>