Getting Started with Matplotlib: What You Should Know

R ChanR Chan
4 min read

Hello there!

In this blog, we will learn about Matplotlib library which is one of the most popular libraries for data visualization in Python. It is widely adopted in the data science and analytics community.

Matplotlib is an open-source Python library for visualizing data graphically. In this post, I’ll cover the core concepts and features of Matplotlib that you'll find yourself using most often.

1. Installation

  • We can install the library using pip package manager but it can also be installed via conda (if you’re using Anaconda or Miniconda).
$ pip install matplotlib
# or
$ conda install matplotlib
  • We can check the version using the Python interpreter.
$ python
Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 14.2.1 20250110 (Red Hat 14.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> print(matplotlib.__version__)
3.10.3
>>>
  • Or, without the Python interpreter.
$ python3 -c "import matplotlib; print(matplotlib.version)"

2. Importing the Library

pyplot is the submodule within Matplotlib that provides an interface for creating visualizations. It is usually aliased as plt, which is the standard naming convention. It gives you simple functions to create plots, add titles, labels, etc.

import matplotlib.pyplot as plt

3. Creating a Basic Line Plot

x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]

plt.plot(x, y)
plt.show()

  • plot(x, y): Draws a line connecting the points (x, y).

  • show(): Displays the plot in a window or notebook.

4. Adding Titles and Labels

Use these to make your plots readable:

plt.plot(x, y)
plt.title("Line Graph")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

  • title(): Sets the title of the graph.

  • xlabel() and ylabel(): Label the x-axis and y-axis to indicate what the data represents on those axes.

5. Customizing Line Style, Color, and Markers

You can change the color, line style, and add markers to your plots:

plt.plot(x, y, color='green', linestyle='--', marker='o')
plt.title('Customized Line Plot')
plt.show()

Here are some common options that we can choose for each attribute while plotting a graph:

  • color: 'red', 'blue', 'green', 'black', etc.

  • linestyle: '-' (solid), '--' (dashed), ':' (dotted)

  • marker: 'o' (circle), 's' (square), '^' (triangle), etc.

6. Adding a Legend

When you plot multiple lines, use legend() to explain what each line represents.

plt.plot(x, y, label='Data 1')
plt.plot(x, [i*0.5 for i in y], label='Data 2')
plt.legend()
plt.show()

7. Adjusting the Figure Size

plt.figure(figsize=(8, 4))  # width=8, height=4 (in inches)
plt.plot(x, y)
plt.show()

8. Creating Subplots

Use subplots() to show multiple plots in one figure.

fig, axs = plt.subplots(1, 2, figsize=(10, 4))

axs[0].plot(x, y)
axs[0].set_title("Line Plot")

axs[1].bar(x, y)
axs[1].set_title("Bar Chart")

plt.tight_layout()
plt.show()

  • subplots() creates a figure (fig) and an array of subplots (axs) where axs[i] lets you customize that specific subplot.

  • 1, 2 means: 1 row, 2 columns → two plots side by side.

  • figsize=(10, 4) sets the overall figure size in inches.

  • plt.tight_layout() automatically adjusts spacing to prevent overlap.

NOTE: plt.title(...) sets the title on the current active Axes. If we use here (let’s say, after axs[1].set_title("Bar Chart")) then it would update the title for the 2nd plot. Use fig.suptitle() to title the whole figure.

9. Saving the Plot

You can save your plot as an image file:

plt.plot(x, y)
plt.title('Save Me!')
plt.savefig("plot.png", dpi=300, bbox_inches='tight')
  • dpi=300: High resolution for printing or reports.

  • bbox_inches='tight': Removes extra white space.

10. Other Common Plot Types

Bar Chart

plt.bar(x, y)

Scatter Plot

plt.scatter(x,y)

Histogram

plt.hist([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])

These basics cover most of what you'll need for plotting with Matplotlib. Once you're comfortable, explore advanced features or try higher-level libraries like Seaborn or Plotly for more advanced plotting! Try combining it with libraries like Pandas or Seaborn for even more powerful visualizations.

If you enjoyed this post, please consider hitting the clap button 👏 below or leaving a comment 💬 or both 🤗. Feel free to subscribe! This will encourage me to write more short, simple, and easy-to-understand blogs!

🍀Happy Learning!🍀

0
Subscribe to my newsletter

Read articles from R Chan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

R Chan
R Chan

Passionate about learning technical stuff 💻. Using this platform to share what I learn each day as a software developer 😇.