[英]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 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()
I think you need to pass all the parameters in plt.legend()
at once.我认为您需要一次传递plt.legend()
中的所有参数。 Try it like this:试试这样:
plt.legend(labels=["A", "B", "C", "D", "E"], ncol=4, loc='center', bbox_to_anchor=(0.5, 1.06))
instead of supplying labels and other params separately.而不是单独提供标签和其他参数。
And as pointed by @BigBen in comments, your code would readily give expected result if you do not use transpose()
正如@BigBen 在评论中指出的那样,如果您不使用transpose()
,您的代码很容易给出预期的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.