change text in legend matplotlib 1

change text in legend matplotlib

plt.plot(range(10), label='Some very long label')
plt.plot(range(1,11), label='Short label')
L=plt.legend()
L.get_texts()[0].set_text('make it short')
plt.savefig('temp.png')

Here is what the above code is Doing:
1. We create a figure and axes object.
2. We plot two lines on the axes.
3. We create a legend object.
4. We get the list of text objects associated with the legend.
5. We set the text of the first text object to be ‘make it short’.
6. We save the figure.

Similar Posts