Introduction to Matplotlib
Matplotlib is a Python library for creating charts and visualizations — it turns raw lists of numbers into clear line plots, bar charts, histograms, and more that you can save, share, and explore.
Learn Introduction to Matplotlib in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this first lesson you'll install Matplotlib, import it the standard way, and render your very first chart.
Matplotlib is the most widely used plotting library in Python. It lets you transform data — numbers in lists, arrays, or DataFrames — into visual charts that humans can actually understand at a glance.
Almost every Python data tool — pandas, seaborn, scikit-learn — builds on top of Matplotlib, so learning it gives you a foundation for the entire data-visualization world.
Matplotlib isn't built into Python, so install it once from your terminal:
Then, in your Python file, import the pyplot module. Almost everyone uses the alias plt :
What you'll see: a window opens with a blue line rising from 100 up to 240, with the five month numbers along the bottom (x-axis) and the sales values up the side (y-axis). That's a complete chart in just three lines of real code.
There are two ways to get your chart out of Python: plt.show() opens an interactive window, while plt.savefig() writes the chart to an image file on disk.
What you'll see: a smooth upward curve (a parabola, because y is x squared). A file named squares.png is also written to your working folder — open it and you'll see the exact same chart as an image.
Replace each ___ to plot a small dataset and display it.
❌ ModuleNotFoundError: No module named 'matplotlib'
You haven't installed it. Run pip install matplotlib in your terminal first.
You forgot plt.show() . In a plain script the window only opens when you call it.
Plot a week of daily temperatures and save the chart to a file.
Lesson 1 complete — you've drawn your first chart!
You installed Matplotlib, imported pyplot the standard way, and rendered and saved a real chart with just a few lines of code.
🚀 Up next: The Figure & Axes — understand the two objects that hold every plot.
Practice quiz
What kind of library is Matplotlib?
- A web framework
- A plotting and visualization library
- A database driver
- A unit-testing tool
Answer: A plotting and visualization library. Matplotlib is Python's most widely used library for creating charts and visualizations.
Which command installs Matplotlib?
- import matplotlib
- python matplotlib
- pip install matplotlib
- plt install
Answer: pip install matplotlib. Matplotlib is not in the standard library, so you install it with pip install matplotlib.
What is the standard import line for pyplot?
- import matplotlib.pyplot as plt
- import pyplot as plt
- from matplotlib import plt
- import matplotlib as plt
Answer: import matplotlib.pyplot as plt. The conventional alias is plt: import matplotlib.pyplot as plt.
Which call draws a line connecting points from two lists?
- plt.draw(x, y)
- plt.line(x, y)
- plt.show(x, y)
- plt.plot(x, y)
Answer: plt.plot(x, y). plt.plot(x, y) connects the (x, y) points with a line.
Which call opens an interactive window showing the chart?
- plt.show()
- plt.display()
- plt.render()
- plt.open()
Answer: plt.show(). plt.show() opens the figure window in a plain script.
Which call writes the chart to an image file?
- plt.export()
- plt.write()
- plt.savefig('chart.png')
- plt.tofile()
Answer: plt.savefig('chart.png'). plt.savefig('name.png') saves the figure to disk as an image.
Why should savefig() be called before show()?
- savefig is faster than show
- show requires a filename
- Order does not matter at all
- show() clears the figure, so a later savefig would be blank
Answer: show() clears the figure, so a later savefig would be blank. After show() closes the window the figure is cleared, so call savefig() first.
If you call plt.plot but see nothing in a script, what is the likely cause?
- You used the wrong color
- You forgot plt.show()
- The data was too small
- You must call plt.draw twice
Answer: You forgot plt.show(). In a plain script the window only appears when you call plt.show().
What error means Matplotlib is not installed?
- SyntaxError
- ValueError
- ModuleNotFoundError: No module named 'matplotlib'
- ImportWarning
Answer: ModuleNotFoundError: No module named 'matplotlib'. ModuleNotFoundError appears when the package has not been installed with pip.
Which tools commonly build on top of Matplotlib?
- pandas, seaborn, and scikit-learn
- numpy only
- the os and sys modules
- requests and flask
Answer: pandas, seaborn, and scikit-learn. Many data tools like pandas, seaborn, and scikit-learn build their plotting on Matplotlib.