You can add a legend above a curve in a matplotlib plot in python by doing the following:
label
parameter to the plt.plot()
method:main.py66 chars3 lines
plt.legend()
method.main.py13 chars2 lines
loc
parameter of plt.legend()
method to specify the location of the legend box.main.py66 chars2 lines
Here, the loc
parameter is set to upper center
, bbox_to_anchor
parameter is used to set the position of the legend box relative to the axes (in this case, it is positioned slightly above the top-center point of the plot) and finally ncol
parameter is set to 2
, to split the legend items into 2 columns. You can adjust these parameters to change the location and position of the legend box.
Here's the complete code:
main.py333 chars17 lines
This will display the plot with the legend box at the top center of the plot, as shown below:
gistlibby LogSnag