This is our first tutorial in Python Matplotlib, in this tutorial we are going to learn about Python Matplotlib Introduction & Installation.
Also you can check more Python articles in the below links
1: Complete PyQt5 GUI Development Course
2: Python TKinter GUI Development
What is Matplotlib ?
Matplotlib is a Python module for plotting, and it is a component of the ScientifcPython modules
suite. Matplotlib allows you to easily prepare professional-grade figures with a comprehensive
API to customize every aspect of the figures. also Matplotlib produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits. matplotlib makes scientific plotting very straightforward. Matplotlib is not the first attempt at making the plotting of graphs easy. what Matplotlib brings is a modern solution to the balance between ease of use and power
Installation
You can simply install matplotlib by using pip.
1 |
pip install matplotlib |
or for more information you can check their documentation, Matplotlib Documentation
So now let’s create our first example in Matplotlib, basically we are going to just create a curve graph. now open your favorite IDE, iam using Pycharm IDE.
1 2 3 4 5 6 7 8 9 10 |
import matplotlib.pyplot as plt x = range(50) y = [number ** 4 for number in x] plt.plot(x,y) plt.show() |
OK in the above code you can see that first we have imported our Matplotlib library, to save on a bit of typing, we make the name plt equivalent to matplotlib.pyplot. This is a very common practice that you will see in Matplotlib code. After that we have created a list named x, with all the integer values from 0 up to 50. The range function is used to create consecutive numbers in Python. also we have created another list named y, with all the values from the list x multiplied by 4. now we need to plot the curve, where the x coordinates of the curve’s points are given in the list x, and the y coordinates of the curve’s points are given in the list y. Note that the names of the lists can be anything you like.
So now run the complete code and this will be the result.
So you can see that how easily we have created a curve graph using Matplotlib in Python Programming Language.
Now let’s create another example this time we are using NumPy. NumPy is not required to use matplotlib. however, many Matplotlib tricks, code samples,and examples use NumPy.
What is Numpy ?
NumPy is the fundamental package for array computing with Python. it provides :
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier transform, and random number capabilities
- and much more
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases. you can simple use pip to install numpy, pip install numpy.
So now in this example we have used NumPy with Matplotlib to create a sin curve.
1 2 3 4 5 6 7 8 |
import numpy as np import matplotlib.pyplot as plt X = np.linspace(0, 4 * np.pi, 50) Y = np.sin(X) plt.plot(X, Y) plt.show() |
Now if you run the code this will be the result
Subscribe and Get Free Video Courses & Articles in your Email