create legend manually in matplotlib

笑着哭i 提交于 2019-12-23 05:15:14

问题


I have the following code to plot a bar graph - there are 6 species but the code groups them into three coloured groupings.

I then want to make a legend of the three groups with three colours. However, the legend only features the first grouping (which is colored correctly!) and then ignores the other groupings (see image)

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

#create data frame
G = pd.DataFrame(np.random.normal(100,20,size=(30, 1)), columns=list('G'))
T = []
for i in range(1,7):
    for j in range(5):
        T.append('Species'+repr(i))
T = pd.DataFrame(np.array(T).reshape(30,1), columns = list("T"))
cols = ['g','b','k']
C=[]
for i in range(3):
    for j in range(10):
        C.append(cols[i])
C = pd.DataFrame(np.array(C).reshape(30,1), columns = list("C"))
dat = pd.concat([G, C, T], axis = 1)
dat.columns = ['growth', 'cols', 'sp']

#begin code to make plot
grps = sorted(list(set(list(dat['sp']))))
meanGrs=[]
sems=[]
cols=[]
y_pos = np.arange(len(grps))
for grp in grps:
    qw = 'sp == "' + grp + '"'
    meanGrs.append(dat.query(qw)['growth'].mean())
    sems.append(dat.query(qw)['growth'].sem())
    cols.append(str(dat.query(qw)['cols'].max()))

plt.bar(y_pos, meanGrs, yerr=sems, align = 'center', alpha = 0.5, color = cols, label = cols)
plt.legend(('group 1', 'group 2', 'group 3'))
plt.xticks(y_pos, grps, ha = 'right', rotation = 45)
plt.show()

Any help much appreciated


回答1:


You can try this way :

for _ in dat['sp']: #looping here keeps the order
if _ not in grps:
    grps.append(_)

i=0
for grp in grps:
    dat.loc[dat['sp']==grp,'meanGrs']= dat.loc[dat['sp']==grp,'growth'].mean()
    dat.loc[dat['sp']==grp,'sems']= dat.loc[dat['sp']==grp,'growth'].sem()
    plt.bar(i, dat.loc[dat['sp']==grp,'meanGrs'], 
              yerr=dat.loc[dat['sp']==grp,'sems'], align = 'center', 
              alpha = 0.5, color = dat.loc[dat['sp']==grp,'cols'])
    i=i+1
    plt.legend(('group 1', 'group 2', 'group 3', 'group 4'))
    plt.show()



回答2:


This does the job:

import matplotlib.patches as mpatches

l1 = mpatches.Patch(color='k', label='group 1')
l2 = mpatches.Patch(color='b', label='group 2')
l3 = mpatches.Patch(color='g', label='group 3')
plt.legend(handles=[l1, l2, l3)



回答3:


Put the argument "label" in the plt.bar:

plt.bar(y_pos, meanGrs, yerr=sems, align = 'center', alpha = 0.5, color = cols, label=cols)



回答4:


I'm not quite sure what would determine which group is which in this case. But if that is arbitrary you can just create the groups on the fly.

legenditems = [(plt.Rectangle((0,0),1,1, color=c, alpha=0.5), "group "+str(i+1))
                 for i,c in enumerate(np.unique(cols))]
plt.legend(*zip(*legenditems))



来源:https://stackoverflow.com/questions/50830680/create-legend-manually-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!