π Introduction
Data visualization is one of the most powerful ways to understand data patterns. In Python, Matplotlib is the most widely used library for plotting graphs and charts. Whether youβre analyzing sales trends π, building machine learning models π€, or just exploring data, Matplotlib helps you create clear, professional, and customizable graphs.
This article will walk you through how to plot graphs using Matplotlib step by step with examples.
βοΈ Step 1. Installing Matplotlib
Before using Matplotlib, you need to install it. Open your terminal or command prompt and run:
pip install matplotlib
If youβre using Jupyter Notebook, you can install and import it directly inside the notebook.
π¦ Step 2. Importing Matplotlib in Python
To get started, you usually import the pyplot module from Matplotlib:
import matplotlib.pyplot as plt
Here, plt
is the alias commonly used for pyplot
.
π Step 3. Creating a Simple Line Plot
A line plot is the most basic type of graph in Matplotlib.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.title("Simple Line Graph")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
β
This will display a straight line showing the relationship between x
and y
.
π΅ Step 4. Plotting a Scatter Plot
A scatter plot is useful to visualize individual data points.
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78]
plt.scatter(x, y, color="blue")
plt.title("Scatter Plot Example")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()
β
Each point will be plotted separately, useful for data analysis.
π Step 5. Creating a Bar Chart
Bar charts help in comparing categories.
x = ["Python", "Java", "C++", "JavaScript"]
y = [80, 65, 60, 75]
plt.bar(x, y, color="green")
plt.title("Programming Language Popularity")
plt.xlabel("Languages")
plt.ylabel("Popularity Score")
plt.show()
β
This shows a vertical bar chart comparing different categories.
π₯§ Step 6. Creating a Pie Chart
Pie charts are used to represent proportions.
sizes = [30, 25, 20, 25]
labels = ["Python", "Java", "C++", "JavaScript"]
plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=140)
plt.title("Market Share of Programming Languages")
plt.show()
β
This will display a pie chart with percentages.
π¨ Step 7. Customizing Graphs
Matplotlib allows customization of graphs:
Example
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
plt.plot(x, y1, label="Squares", color="blue", linestyle="--")
plt.plot(x, y2, label="Line", color="red", marker="o")
plt.title("Customized Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()
β
This displays two graphs with different styles and a legend.
π Step 8. Real-World Example β Sales Data Visualization
Letβs say you want to visualize monthly sales data.
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [200, 250, 300, 350, 400, 450]
plt.plot(months, sales, marker="o", color="purple")
plt.title("Monthly Sales Report")
plt.xlabel("Months")
plt.ylabel("Sales in $")
plt.grid(True)
plt.show()
β
This shows sales growth clearly with points and gridlines.
π Conclusion
Matplotlib is a powerful Python library for data visualization. With just a few lines of code, you can create line charts, scatter plots, bar charts, pie charts, and fully customized graphs. Whether youβre a beginner or working on ML projects, Matplotlib is an essential tool to present data visually and make insights clearer.
π Next steps: Once youβre comfortable with Matplotlib, explore Seaborn and Plotly for advanced visualizations.