Python ProgrammingPython Programming

Put the legend at various position of the plot with Matplotlib?

Put the legend at various position of the plot:

import matplotlib.pyplot as plt

#Plot a line graph
plt.plot([5, 15], label='Rice')
plt.plot([3, 6], label='Oil')
plt.plot([8.0010, 14.2], label='Wheat')
plt.plot([1.95412, 6.98547, 5.41411, 5.99, 7.9999], label='Coffee')

# Add labels and title
plt.title("Interactive Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.legend(bbox_to_anchor=(1.1, 1.05))

plt.show()


Put the legend at various position of the plot
plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.02),
          ncol=3, fancybox=True, shadow=True)


Put the legend at various position of the plot
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))


Put the legend at various position of the plot
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
           fancybox=True, shadow=True, ncol=5)


Put the legend at various position of the plot