how to show line label in matplotlib with plot()?
- User
how to show line label in matplotlib with plot()?
- Assistant
To show line labels in matplotlib using the
plot()function, you can use theplt.legend()function. Here's an example:pythonimport matplotlib.pyplot as plt # Create x and y data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [1, 8, 27, 64, 125] # Plot the lines plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') # Add legend plt.legend() # Show the plot plt.show()In this example,
plt.plot()is used to create two lines withxas the x-axis data andy1andy2as the y-axis data for the two lines. Thelabelparameter is used to assign a label to each line.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.