Matplotlib is a powerful library for creating visualizations in Python. One of the most common types of plots is the line plot.
Here’s a simple example to get you started with creating a line plot using Matplotlib. Now, here’s a basic example of how to create a line plot:
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Create the plot
plt.plot(x, y)
# Add title and labels
plt.title("Simple Line Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
# Show the plot
plt.show()
In this example:
x
andy
are lists of coordinates for the data points.plt.plot(x, y)
creates the line plot.plt.title()
,plt.xlabel()
, andplt.ylabel()
add a title and labels to the plot.plt.show()
displays the plot.
You can customize the appearance of the plot by adding more arguments to plt.plot()
, such as line color, style, and markers. Here’s an example with some customizations:
plt.plot(x, y, color='red', linestyle='--', marker='o', markerfacecolor='blue')