To add a legend box to a matplotlib plot, you can use the legend()
function. The legend function takes several arguments like the position of the legend and the labels to display.
Here is an example:
main.py182 chars8 lines
In this example, plt.legend(loc='upper right')
adds a legend to the upper right corner of the plot.
You can also explicitly set the labels for each line in the legend by passing a list of labels to the labels
parameter of legend()
.
main.py211 chars8 lines
This code will give a legend on the upper right
corner of the plot containing the labels provided in labels
list.
gistlibby LogSnag