Matplotlib: Exercises


2. Customizing a Line Plot. In Matplotlib, you can customize a line plot by adding labels, titles, legends, and changing line styles. You can use functions like plt.xlabel(), plt.ylabel(), plt.title(), and plt.legend().

For example, you can add labels and a legend to a line plot like this: plt.xlabel('X-axis'); plt.ylabel('Y-axis'); plt.legend(['Line']).

In the exercise below, you will add labels and a legend to a line plot.


# Import the Matplotlib library import matplotlib.pyplot as plt # Create x and y values for a line plot x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a line plot with customizations plt.plot(x, y, label='Line') # Add labels and legend plt.xlabel(___) plt.ylabel(___) plt.legend(___) # Display the plot plt.show() # Import the Matplotlib library import matplotlib.pyplot as plt # Create x and y values for a line plot x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a line plot with customizations plt.plot(x, y, label='Line') # Add labels and legend plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend(['Line']) # Display the plot plt.show() test_object("x") test_object("y") success_msg("Correct!")
To customize a line plot, use functions like plt.xlabel(), plt.ylabel(), and plt.legend().

Previous Exercise Next Exercise