Reputation: 225
I just want to delete the Label from the Legend:
example (i just copy pasted the plot from an other stackoverflow problem)Plot example
The question is, is there a way to delete the color Labels in the legend?
So that I'll just see "first" , "second" , "third" without the blue,green,red color label.
My problem is, i just want to use 1 Value in the Legend ( float ) and i dont want to have a label. The Value should be outside of the plot like in this example. I use the Legend with this code:
PC= np.pearsonr(x,y)
plt.scatter(x,y,color="#3F5D7D",label='PK: %.3f'%(PC[0]))
plt.legend(prop={'size':12},bbox_to_anchor=(1.1, 1.1))
Upvotes: 1
Views: 1897
Reputation: 131
If you want to remove one label from an existing plot you can try this:
label_to_remove='remove_me'
h,l=ax.get_legend_handles_labels()
idx_keep=[k[0] for k in enumerate(l) if l[k[0]] != label_to_remove]
handles=[]
labels=[]
for i in idx_keep:
handles.append(h[i])
labels.append(l[i])
print(handles, labels)
ax.legend(handles, labels, loc='upper right')
Upvotes: 0
Reputation: 1123
Your title seems a little misleading as it states you want to delete the Label (text part) from the Legend, however the text of your question states you want to just see the text without the red, green and blue (Legend Key).
If you want to keep the Label(text), you can substitute the default key for a blank one by creating an empty rectangle as below. (creating a proxy artist for the legend)
import matplotlib.pyplot as plt
import matplotlib
import random
import scipy.stats as ss
pop = range(100)
x = random.sample(pop, 10)
y = random.sample(pop, 10)
PC = ss.pearsonr(x, y)
plt.scatter(x,y,color="#3F5D7D")
blank = matplotlib.patches.Rectangle((0,0), 0, 0, fill=False, edgecolor='none', visible=False)
plt.legend([blank], ['PK: %.3f'%(PC[0])], prop={'size':12},bbox_to_anchor=(1.1, 1.1))
plt.show()
Upvotes: 1