Complete the code to add a legend to the plot.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line') plt.[1]()
The legend() function adds a legend to the plot using the labels defined in the plot commands.
Complete the code to place the legend in the upper left corner.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line') plt.legend(loc=[1]) plt.show()
The loc parameter controls the legend position. 'upper left' places it in the top-left corner.
Fix the error in the code to set the legend font size to 14.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line') plt.legend(fontsize=[1]) plt.show()
The fontsize parameter accepts an integer for font size in points, so 14 is correct.
Fill both blanks to place the legend at center left and set a shadow.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line') plt.legend(loc=[1], shadow=[2]) plt.show()
Setting loc='center left' places the legend at center left, and shadow=True adds a shadow effect.
Fill all three blanks to create a legend with a title, placed at the lower center, and with fancy box style.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line') plt.legend(title=[1], loc=[2], fancybox=[3]) plt.show()
The title parameter adds a title to the legend, loc='lower center' places it at the bottom center, and fancybox=True gives the legend a rounded box style.