As I have shown in the figure:
I don't want to show the line in the legend but label, if it is possible please help to fix.
Have tried to minimise the present-legend's line and label and overwrite only the new-label but legend brings both back.
legend = ax.legend(loc=0, shadow=False)
for label in legend.get_lines():
for label in legend.get_texts():
ax.legend(loc=0, title='New Title')
At that point, it's arguably easier to just use
import numpy as np import matplotlib.pyplot as plt data = np.random.normal(0, 1, 1000).cumsum() fig, ax = plt.subplots() ax.plot(data) ax.annotate('Label', xy=(-12, -12), xycoords='axes points', size=14, ha='right', va='top', bbox=dict(boxstyle='round', fc='w')) plt.show()
However, if you did want to use
legend, here's how you'd do it. You'll need to explicitly hide the legend handles in addition to setting their size to 0 and removing their padding.
import numpy as np import matplotlib.pyplot as plt data = np.random.normal(0, 1, 1000).cumsum() fig, ax = plt.subplots() ax.plot(data, label='Label') leg = ax.legend(handlelength=0, handletextpad=0, fancybox=True) for item in leg.legendHandles: item.set_visible(False) plt.show()