繁体   English   中英

如何在图例上显示数据 label

[英]how to show data label on legend

i am trying to plot bar plot but there is problem labels are not showing on legend, any one help how can i show legend there in my figure data label are showing like 1,2 instead of label

import pandas as pd
import matplotlib.pyplot as plt

a = [3,4, 15, 10, 12]
b = [2,13,4,19,1]
c = [12,3,7,8,4]
d = [12, 13,4,7,14]
df = pd.DataFrame([a,b,c,d], columns=["A", "B", "C", "D", "E"]).transpose()

df.plot(kind = "bar")
plt.legend(["A", "B", "C", "D", "E"])
plt.legend(ncol=4, loc='center', bbox_to_anchor=(0.5, 1.06))

plt.xticks(rotation=360)
plt.show()

在此处输入图像描述

我认为您需要一次传递plt.legend()中的所有参数。 试试这样:

plt.legend(labels=["A", "B", "C", "D", "E"], ncol=4, loc='center', bbox_to_anchor=(0.5, 1.06))

而不是单独提供标签和其他参数。

正如@BigBen 在评论中指出的那样,如果您不使用transpose() ,您的代码很容易给出预期的结果

暂无
暂无

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

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