Complete the code to add the first legend to the plot.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line 1') plt.plot([3, 2, 1], label='Line 2') plt.[1]() plt.show()
title() instead of legend().The legend() function adds a legend to the plot showing labels.
Complete the code to create a second legend for the scatter points.
import matplotlib.pyplot as plt line1, = plt.plot([1, 2, 3], label='Line 1') line2, = plt.plot([3, 2, 1], label='Line 2') scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points') plt.legend(handles=[line1, line2]) plt.[1](handles=[scatter], loc='upper right') plt.show()
title() or other functions instead of legend().To add a second legend, call plt.legend() again with the new handles.
Fix the error in the code to correctly add two legends to the plot.
import matplotlib.pyplot as plt line1, = plt.plot([1, 2, 3], label='Line 1') line2, = plt.plot([3, 2, 1], label='Line 2') scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points') leg1 = plt.legend(handles=[line1, line2], loc='upper left') plt.add_artist([1]) plt.legend(handles=[scatter], loc='upper right') plt.show()
When adding multiple legends, the first legend must be added as an artist using plt.gca().add_artist() or plt.add_artist() with the legend object.
Fill both blanks to create two legends and add the first legend as an artist.
import matplotlib.pyplot as plt line1, = plt.plot([1, 2, 3], label='Line 1') line2, = plt.plot([3, 2, 1], label='Line 2') scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points') leg1 = plt.[1](handles=[line1, line2], loc='upper left') plt.[2](leg1) plt.legend(handles=[scatter], loc='upper right') plt.show()
title() or xlabel() instead of add_artist().First, create the legend with plt.legend(). Then add it as an artist with plt.add_artist() before adding the second legend.
Fill all three blanks to create two legends with different locations and add the first legend as an artist.
import matplotlib.pyplot as plt line1, = plt.plot([1, 2, 3], label='Line 1') line2, = plt.plot([3, 2, 1], label='Line 2') scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points') leg1 = plt.[1](handles=[line1, line2], loc=[2]) plt.[3](leg1) plt.legend(handles=[scatter], loc='upper right') plt.show()
Create the first legend with plt.legend() at 'upper left'. Then add it as an artist with plt.add_artist() before adding the second legend at 'upper right'.