简体   繁体   中英

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()

在此处输入图像描述

I think you need to pass all the parameters in plt.legend() at once. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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