Creating Your First Plot
You'll learn how to create and display a simple line plot using Matplotlib's plot() function.
Now that you understand Matplotlib's anatomy and the two interfaces available, it's time to actually create your first visualization. Think of this like writing "Hello, World!" in programming—it's your entry point into the world of data visualization.
The Basic Building Blocks
Every basic plot needs three essential elements:
- Data to visualize: Your x and y values
- The plot command:
plt.plot()to create the visualization - The display command:
plt.show()to actually see it
Making Your First Line Plot
The plot() function draws lines connecting data points. You provide x-coordinates and y-coordinates, and Matplotlib connects the dots:
import matplotlib.pyplot as plt
# Your data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create the plot
plt.plot(x, y)
# Display the plot
plt.show()
This creates a simple line plot showing how y changes as x increases. The x-values define positions along the horizontal axis, while y-values define heights on the vertical axis.
Understanding the Flow
plt.plot(x, y)tells Matplotlib what to draw but doesn't display anything yetplt.show()renders the figure and opens a window (or displays it inline in notebooks)- If you omit
plt.show(), you might not see your visualization
Think of plot() as sketching on canvas and show() as unveiling the finished artwork to your audience.
Key Takeaway: Use plt.plot(x, y) to create a line plot connecting your data points, then call plt.show() to display the visualization—these two commands are your foundation for all Matplotlib plotting.