3
Lesson 3
Simple plots
The plot() function is one of the most commonly used functions in Matplotlib for creating basic line plots. It takes data points (typically for x and y) and connects them with lines.
Syntax:
Syntax:
plt.plot(x, y, options)
- x : Data for the x-axis.
- y : Data for the y-axis.
- options : Various optional arguments to customize the appearance of the plot (e.g., color, line style, markers).
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a simple line plot plt.plot(x, y) # Display the plot plt.show()
In this example, a basic line plot is created with the plot() function, connecting the points (1, 2), (2, 4), and so on.
Adding titles, legends, and labels
A good plot includes clear labels for the axes, a title to describe the plot, and a legend to explain multiple lines or data points. Matplotlib provides several functions to make your plots informative.
a. Adding a title:
The title() function is used to add a title to the plot.
plt.title('Sample Plot')
b. Adding labels:
To label the x-axis and y-axis, use xlabel() and ylabel().
plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label')
c. Adding a legend:
The legend() function is used to add a legend to the plot, typically when plotting multiple lines. You need to specify labels for each plot using the label parameter in plot().
# Plot multiple lines plt.plot(x, y, label='Line 1') # Add a legend plt.legend()
Example with titles, labels, and legend:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [2, 3, 5, 7, 11] # Create two line plots plt.plot(x, y1, label='Quadratic') plt.plot(x, y2, label='Prime') # Add title and labels plt.title('Comparison of Two Lines') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Add legend plt.legend() # Show the plot plt.show()
In this example:
- Two lines are plotted, one representing y1 (quadratic values) and the other y2 (prime numbers).
- We added a title, x-axis and y-axis labels, and a legend that describes each line.
Modifying line styles, colors, and markers
Matplotlib allows for a high degree of customization, including changing the style of the lines, the color, and adding markers at each data point.
Line styles:
You can modify the line style using the linestyle argument. Some common options include:
- - : Solid line (default)
- -- : Dashed line
- -. : Dash-dot line
- : : Dotted line
plt.plot(x, y, linestyle='--') # Dashed line
a. Line color:
The color of the line can be changed using the color argument. You can specify a color name (blue, green, etc.), a hex code (#FF5733), or a shorthand like r for red, g for green, and so on.
plt.plot(x, y, color='green') # Green line
b. Markers:
Markers are symbols that appear at each data point. You can set the marker type using the marker argument. Some common markers include:
- o : Circle
- s : Square
- ^ : Triangle
- x : Cross
plt.plot(x, y, marker='o') # Circle markers
c. Combining styles:
You can combine these options to create a customized plot.
Example with combined customizations:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [2, 3, 5, 7, 11] # Customize the first line plt.plot(x, y1, color='blue', linestyle='-', marker='o', label='Quadratic') # Customize the second line plt.plot(x, y2, color='red', linestyle='--', marker='s', label='Prime') # Add title and labels plt.title('Customized Line Styles and Markers') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Add legend plt.legend() # Show the plot plt.show()
In this example:
- The first line (quadratic) uses a solid blue line with circle markers.
- The second line (prime numbers) uses a dashed red line with square markers.
Conclusion
In this lesson, we learned how to create simple plots using the plot() function. We explored how to add titles, legends, and labels to make our plots informative, and how to modify the appearance of plots using line styles, colors, and markers. In the next lesson, we will dive deeper into customizing plots, including modifying the axis limits, adding grids, and more advanced formatting.