Mastering Matplotlib: Part 1. An Introduction and Basic Plotting Techniques
Unlocking Data Visualization: Your First Steps with Matplotlib

🔜 Next: Enhancing Plots with Labels, Titles, Legends, and Customizations
During my recent machine learning course at the university, I explored various essential tools and techniques used in data analysis and visualization. I’ve decided to share some of these insights in a series of blog posts on Medium. This post on Matplotlib is part of that series, offering a practical introduction to one of Python’s most powerful plotting libraries. For those interested in diving deeper, additional code examples can be found at my GitHub page: [GitHub].
Matplotlib is one of the most popular plotting libraries in Python, widely used for creating static, animated, and interactive visualizations. Whether you are a data scientist, engineer, or just someone who wants to visualize data, Matplotlib provides all the tools you need to create publication-quality plots. In this tutorial, we’ll start with the basics, setting a strong foundation for more advanced plotting in the series.
Installation and Setup
Before diving into plotting, you need to install Matplotlib. You can install it using pip:
pip install matplotlib
Once installed, you can import Matplotlib in your Python scripts or Jupyter notebooks:
import matplotlib.pyplot as pltpyplot is the Matplotlib submodule that provides a MATLAB-like interface, making it easier to create plots quickly.
Basic Structure of a Matplotlib Plot
Matplotlib plots typically consist of several components:
- Figure: The entire window or page where the plot appears.
- Axes: The area where the data is plotted, including the x and y-axis.
- Axis: Represents the data values, including ticks and labels.
- Plot Elements: These include lines, markers, text, etc., that are added to the Axes.
Here’s an example of a simple plot:
import matplotlib.pyplot as plt
# Create data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Create a plot
plt.figure(figsize=(8, 6))
plt.plot(x, y)
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
# Show the plot
plt.show()Output:

In this code:
plt.figure()initializes the figure.plt.plot()creates a line plot.plt.xlabel(),plt.ylabel(), andplt.title()add labels and a title.plt.show()displays the plot.
Overview of pyplot
pyplot is designed to provide a simple interface for creating common types of plots. It supports a variety of plotting functions like plot(), scatter(), bar(), hist(), and more. Each of these functions can be customized with arguments to adjust colors, styles, markers, and other properties.
Basic Plotting
Now that we’ve covered the basics, let’s explore how to create and customize plots in Matplotlib.
Plotting Line Graphs
Line graphs are the simplest and most common type of plot. You can plot a basic line graph using the plot() function:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Line Graph')
plt.show()Output:

This will generate a simple line graph, plotting y against x.
Customizing Line Styles and Colors
Matplotlib allows you to customize line styles and colors to make your plots more visually appealing. You can change the color, line style, and marker of the line using additional arguments in the plot() function:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
plt.plot(x, y, color='green', linestyle='--', linewidth=2)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Customized Line Graph')
plt.show()Output:

In this example:
color='green'changes the line color.linestyle='--'makes the line dashed.linewidth=2increases the line thickness.
Adding Markers
Markers are used to highlight individual data points on the line. You can add markers by specifying the marker argument:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
plt.plot(x, y, marker='o', color='blue', linestyle='-', linewidth=2)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Graph with Markers')
plt.show()Output:

Here, marker='o' adds circular markers at each data point.
Plotting Multiple Lines
You can plot multiple lines on the same graph by calling the plot() function multiple times before show():
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [1, 4, 2, 3]
y2 = [2, 3, 4, 5]
plt.plot(x, y1, marker='o', color='blue', label='Line 1')
plt.plot(x, y2, marker='s', color='red', label='Line 2')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Lines')
plt.legend()
plt.show()Output:

This code plots two lines with different styles and adds a legend to distinguish between them.
Conclusion
In this first part of the “Mastering Matplotlib” series, we introduced you to the basics of Matplotlib, a powerful Python library for data visualization. We covered what Matplotlib is, how to install and set it up, and explored the fundamental structure of a Matplotlib plot. You learned how to create basic line graphs, customize line styles and colors, add markers, and plot multiple lines on a single graph.
These foundational skills are crucial as we move forward into more advanced topics. In the next part of this tutorial series (Part — 2), we’ll focus on enhancing your plots with detailed labels, titles, and legends. We’ll also dive into plot customization techniques, such as adjusting dimensions, changing axis limits, customizing ticks, and adding annotations.
If you like the article and would like to support me make sure to:
👏 Clap for the story (as much as you liked it 😊) and follow me 👉 📰 View more content on my medium profile 🔔 Follow Me: LinkedIn | Medium | GitHub | Twitter
Feel free to share your thoughts and questions in the comments below!




