0

I have a simple dataframe of group name, x and y data. Each row has a unique group name but repetitve. My code is given below:

from colour import Color
clr_list = list(mcd.XKCD_COLORS.values())[::10][:len(strng_list)] # unique color list
ex = [['group1',[1,2,3,4],[1,2,3,4]],['group2',[1,2,3,4],[3,2,1,6]],
      ['group1',[1,2,3,4],[3,9,6,5]],['group2',[1,2,3,4],[4,6,9,0]]]
exdf = pd.DataFrame(columns=['name','xdata','ydata'],data=ex)
print(exdf)
#     name         xdata         ydata
#0  group1  [1, 2, 3, 4]  [1, 2, 3, 4]
#1  group2  [1, 2, 3, 4]  [3, 2, 1, 6]
#2  group1  [1, 2, 3, 4]  [3, 9, 6, 5]
#3  group2  [1, 2, 3, 4]  [4, 6, 9, 0]

name_list = exdf['name'].unique().tolist()
for i in name_list: 
    auxdf = exdf[exdf['name']== i]
    auxdf.reset_index(drop=True,inplace=True)
    idx = name_list.index(i)
    for ses in range(0,len(auxdf)):
        plt.plot(auxdf['xdata'][ses],auxdf['ydata'][ses],
                 '-',color=clr_list[idx],label =auxdf['name'][ses])
plt.ylabel("ydata")
plt.legend(loc='center left',fontsize = 8,ncol=2)
plt.xlabel("xdata")
plt.subplots_adjust(left=0.14, right=0.67, top=0.95, bottom=0.15)
plt.show()

My present output is given below:

enter image description here

I hope you understood the problem from the picture legend. Yes! I want to produce legend with no repetitive names and colors. How to avoid it.

Mainland
  • 4,110
  • 3
  • 25
  • 56

2 Answers2

1

You can specify to add label only the first time:

from colour import Color
import matplotlib._color_data as mcd
import pandas as pd
import matplotlib.pyplot as plt

clr_list = list(mcd.XKCD_COLORS.values())[::10][:2] # unique color list
ex = [['group1',[1,2,3,4],[1,2,3,4]],['group2',[1,2,3,4],[3,2,1,6]],
      ['group1',[1,2,3,4],[3,9,6,5]],['group2',[1,2,3,4],[4,6,9,0]]]
exdf = pd.DataFrame(columns=['name','xdata','ydata'],data=ex)
print(exdf)
#     name         xdata         ydata
#0  group1  [1, 2, 3, 4]  [1, 2, 3, 4]
#1  group2  [1, 2, 3, 4]  [3, 2, 1, 6]
#2  group1  [1, 2, 3, 4]  [3, 9, 6, 5]
#3  group2  [1, 2, 3, 4]  [4, 6, 9, 0]

name_list = exdf['name'].unique().tolist()
for i in name_list: 
    auxdf = exdf[exdf['name']== i]
    auxdf.reset_index(drop=True,inplace=True)
    idx = name_list.index(i)
    for ses in range(0,len(auxdf)):
        plt.plot(auxdf['xdata'][ses],auxdf['ydata'][ses],
                 '-',color=clr_list[idx],label =auxdf['name'][ses] if ses == 0 else "")
plt.ylabel("ydata")
plt.legend(loc='center left',fontsize = 8,ncol=2)
plt.xlabel("xdata")
plt.subplots_adjust(left=0.14, right=0.67, top=0.95, bottom=0.15)
plt.show()

adding label =auxdf['name'][ses] if ses == 0 else "" will do the trick

sau
  • 1,316
  • 4
  • 16
  • 37
0

Inspired from the answer here

following is the response:

ex = [['group1',[1,2,3,4],[1,2,3,4]],['group2',[1,2,3,4],[3,2,1,6]],
      ['group1',[1,2,3,4],[3,9,6,5]],['group2',[1,2,3,4],[4,6,9,0]]]
exdf = pd.DataFrame(columns=['name','xdata','ydata'],data=ex)
name_list = exdf['name'].unique().tolist()
for i in name_list: 
    auxdf = exdf[exdf['name']== i]
    auxdf.reset_index(drop=True,inplace=True)
    idx = name_list.index(i)
    for ses in range(0,len(auxdf)):
        plt.plot(auxdf['xdata'][ses],auxdf['ydata'][ses],
                 '-',color=clr_list[idx],label =auxdf['name'][ses])
plt.ylabel("ydata")
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())
plt.xlabel("xdata")
plt.subplots_adjust(left=0.14, right=0.97, top=0.95, bottom=0.15)
plt.show()

Present output:

enter image description here

Mainland
  • 4,110
  • 3
  • 25
  • 56