
Python Matplotlib Basic Tutorial
What is Matplotlib?
Matplotlib is a popular Python library used for creating data visualizations on a large set of data. It enables you to turn numbers into charts and graphs with just a few lines of code. In this short guide, you will learn the basics and see how to get started with some actual plots.
Steps To Use Matplotlib for Data Visualization:
1) Installing Matplotlib And Importing It In Terminal
The Matplotlib Python library and any of its dependencies must be installed on your local system before any execution of code in Python. You need to type these commands in your Linux/ Windows terminal (or command prompt):
If this command does not work, you can try using “python3 -m pip install matplotlib.”
Alternatively, you can use this command as the first line in each of your plot script,s as shown in the examples.
2) Creating a Basic Line Plot
As an example of plotting a line graph using Matplotlib, let’s see below a simple line chart.
Step by step:
- Prepare the data — lists or sequences of values
- Call the plot function to get lines
- Add title and labels
- Call plt.show() to display
Here’s sample code:
Below is an example output of this code which opens automatically due to command plt.show() used in the last line. You can play around and see what all variants of these graphs you can make.
3) Create a Bar Chart
Bar charts are useful when you want to compare categories.
To create a bar chart in Matplotlib, you can prepare your data in a file or directly add it in and run the below command:
Visualization of the data as bar graph is shown below-
4) Scatter Plot for Relationship Visualization
When you want to see how one variable relates to another, you can use code similar to the following:
An example of the output is given below-
5) Customize Plot Styles
Matplotlib lets you tweak line styles or colors. You can also use numpy to perform algebraic or geometric functions on data and plot it using Matplotlib, as shown below:
Output of the new visualization format is shown below as a sigmoidal curve from the given data-
6) Save Your Graph Image
After building a useful visualization, you might want to save it.
So, keeping the earlier code intact you can add this command plt.savefig("my_plot.png") before plt.show():
You can check the file saved as .png format in your current working directory.
Explore Other Demanding Courses
No courses available for the selected domain.
7) Multiple Plots Together (as Subplots)
You can show more than one type of chart side by side using the key Matplotlib command fig, axes = plt.subplots(1, 2):
Below is an example of two side-by-side plots from the given data-
8) Few Tips To Become Better in Python and Matplotlib-Based Data Visualization.
- Always label your axes with proper terms. This surely makes the graphs easier to read
- Legends will help you when you have multiple lines or plots, so plan to add them using codes on the graphs
- Apart from coding on your terminal, you can also try interactive notebooks like Jupyter to see the plot inline
- For 3D plot,s there are extra tools you can enable within Matplotlib. You can learn more about this in our Python Data Analytics Course at SevenMentor which teaches you various techniques and tools to make you data effective.
Wrapping Up
In just several steps, you’ve learned how to install Matplotlib, plot basic graphs, customize them, and save your work. This foundation training in matplotlib lets you build charts for data reports, quick exploration, or dashboards when needed. You can explore more advanced visuals once you’ve practiced these basics. Also, check out our free demo lecture courses to learn more, and feel free to contact us for the Data Visualization tricks and tips inthe future. The Python training courses at Sevenmentor Institute focus on practical learning, clear concepts, and industry-oriented examples, which makes the learning process smoother for beginners as well as working professionals.
Also, explore our YouTube Channel: SevenMentor