Getting Started with Matplotlib in Python
June 24, 2012
2 min read

- π Getting Started with Matplotlib in Python
- π¦ Installation
- π₯ Importing Matplotlib
- πΌοΈ Basic Plotting
- 1. Line Plot
- 2. Bar Chart
- 3. Scatter Plot
- 4. Histogram
- π¨ Customization Options
- 1. Colors and Line Styles
- 2. Grid, Legend, and Axis Limits
- 3. Subplots
- π§ Common Troubleshooting Tips
- β 1. ModuleNotFoundError: No module named 'matplotlib'
- β 2. Plots Not Showing Up
- β 3. Plot Overlaps or Looks Messy
- β 4. Wrong Plot Dimensions or Labels
- β 5. Plot Freezing or Crashing in IDEs
- π Additional Resources
- β Summary
π Getting Started with Matplotlib in Python
Matplotlib
is one of the most popular Python libraries for data visualization. Whether youβre building simple line plots or complex multi-axes figures, Matplotlib provides the flexibility and control you need.
In this post, youβll learn:
- How to install and import Matplotlib
- How to create basic plots
- How to customize your charts
- How to troubleshoot common Matplotlib issues
π¦ Installation
You can install Matplotlib using pip
or conda
:
pip install matplotlib
Or:
conda install matplotlib
π₯ Importing Matplotlib
The most common import pattern is:
import matplotlib.pyplot as plt
pyplot
is a collection of functions that mimic MATLAB-style plotting.
πΌοΈ Basic Plotting
1. Line Plot
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plt.plot(x, y)
plt.title("Line Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
2. Bar Chart
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 25]
plt.bar(categories, values)
plt.title("Bar Chart Example")
plt.show()
3. Scatter Plot
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y)
plt.title("Scatter Plot Example")
plt.show()
4. Histogram
data = [1, 1, 2, 3, 3, 3, 4, 5, 5, 6]
plt.hist(data, bins=5)
plt.title("Histogram Example")
plt.show()
π¨ Customization Options
1. Colors and Line Styles
plt.plot(x, y, color='red', linestyle='--', marker='o')
2. Grid, Legend, and Axis Limits
plt.plot(x, y, label='y = x^2')
plt.grid(True)
plt.legend()
plt.xlim(0, 5)
plt.ylim(0, 20)
3. Subplots
plt.subplot(1, 2, 1) # 1 row, 2 columns, 1st plot
plt.plot(x, y)
plt.subplot(1, 2, 2) # 2nd plot
plt.bar(categories, values)
plt.suptitle("Multiple Plots")
plt.tight_layout()
plt.show()
π§ Common Troubleshooting Tips
β 1. ModuleNotFoundError: No module named 'matplotlib'
Fix: You havenβt installed the library.
pip install matplotlib
If youβre using a Jupyter notebook, make sure you install it in the correct kernel.
β 2. Plots Not Showing Up
Fix:
- Ensure
plt.show()
is called after your plot. - If youβre using Jupyter Notebook, use:
%matplotlib inline
β 3. Plot Overlaps or Looks Messy
Fix: Use plt.tight_layout()
to adjust spacing between subplots.
plt.tight_layout()
β 4. Wrong Plot Dimensions or Labels
Fix:
- Double-check your data shapes:
x
andy
must be the same length. - Always label axes using
plt.xlabel()
andplt.ylabel()
.
β 5. Plot Freezing or Crashing in IDEs
Fix: Some IDEs (like Spyder or PyCharm) may require interactive mode:
plt.ion() # Turns on interactive mode
You can also try running your script from the terminal.
π Additional Resources
β Summary
Matplotlib is a versatile and powerful plotting library for Python. Youβve now learned how to:
- Create line, bar, scatter, and histogram plots
- Customize plots with colors, legends, and subplots
- Fix common issues like invisible plots or import errors
Ready to make your data speak visually? Start plotting with Matplotlib today!
Happy Plotting! π