How to create visualizations with matplotlib in Python
In this tutorial, you will get started with Matplotlib visualization in Python. You will learn the object-oriented approach with Matplotlib, this makes it less confusing with the cost of only one more line of code.
In this tutorial, you will:
- Get started with Matplotlib: Learn the basics of Matplotlib, a popular data visualization library in Python.
- Object-Oriented Approach: Explore the object-oriented approach in Matplotlib, which simplifies the process with minimal additional code.
- Clear and Concise Visualization: Create visually appealing and informative plots using Matplotlib’s object-oriented approach.
By the end of the tutorial, you will have a solid foundation in using Matplotlib for data visualization. The object-oriented approach will streamline your workflow and help you create clear and concise visualizations with ease.
Plot a list of numbers with Matplotlib in Python
Given a list of numbers, how can you make a connected line?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3, 4])
Which results in the following output.
The numbers do not need to be in a straight line. But the line will be connected.
Make a Colored Scatter Plot with Matplotlib in Python
Now you need tree lists.
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6, 4] y = [2, 3, 2, 1, 6, 10, 3] c = [1, 1, 2, 2, 3, 4, 4] fig, ax = plt.subplots() ax.scatter(x, y, c=c) ax.set_title("Title") ax.set_xlabel("X label") ax.set_ylabel("Y label")
This results in the following plot.
Notice that we also added titles and labels to the axis.
This could also be done in the connected line plot above.
Make a Histogram with Matplotlib
You can make a histogram as follows.
import matplotlib.pyplot as plt data = [1, 1, 2, 2, 1, 2, 3, 3, 2, 3, 1, 3, 2] fig, ax = plt.subplots() ax.hist(data, bins=4) ax.set_title("Title") ax.set_xlabel("X label") ax.set_ylabel("Y label")
This results in the following plot.
Pie charts are a very powerful way to represent data.
- Pie charts are easy to understand.
- Pie charts are simple to show how data is divided.
Be sure to check this Pie Chart tutorial for matplotlib in Python.
Want to learn more?
In the next project you will learn NumPy Basics and Linear Regression with Python
If this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.
The course is structured with the following resources to improve your learning experience.
- 17 video lessons teaching you everything you need to know to get started with Python.
- 34 Jupyter Notebooks with lesson code and projects.
- A FREE eBook to support your Python learning.
See the full FREE course page here.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.