Matplotlib Python 3 tutorial : In this tutorial, we will understand the matplotlib in detail with the installation and the plotting in detail. The visualization of data is an important factor which is done by the matplotlib. It is a powerful plotting library used for working with numpy and python. The matplotlib is used as pyplot which is used to provide the interface like MATLAB and uses python because it is open source. It is powerful plotting library used in the python programming language. By using the matplotlib we can create the plots and get solutions on complex work. The matplotlib library is used for the plotting and enables us to visualize the data in graphical representation. Also plots the 2d graph and these graphs are drawn through the python script also called as the pyplot. Using the pyplot we can create the plotting easily and control the lines and formatting processes.
The use of python language is the data analysis and done by the data scientist to visualize the data.
To start the matplotlib we have to start by downloading the matplotlib.org and get started.
The matplotlib is used for plotting the library in python and create any plot.
They will create the charts as well as the graphs.
The plotting knowledge is extracted from the MATLAB programming for purpose of the graphics.
Parameters |
X: - It is the array/sequence of arrays. When we take input value it is not required of the same length.
|
1. Backend layer:-
It is the layer placed at the bottom and consists of implementation of various functions that are needed for the plotting.
There are classes from the backend layer as,
2. Artist layer:-
It is called the second layer in architecture and responsible for plotting the functions like axis having the coordinates to use renderer on figure canvas.
3. Scripting layer:-
The layer is placed at the topmost layer on which the code is run.
The methods are automatically taken and leave the care of the current state.
The data visualization tool is important in analytics and gives the idea of complex data.
Python language is important for visualization of tools.
There are two languages used for visualization as seaborn and matplotlib.
The seaborn library is based on matplotlib.
The comparison between the two libraries is as follows:-
1 .Functionality:-
Matplotlib:-
It is deployed for basic plotting of the graph.
The visualization consists of the bars, lines, scatter plots, etc.
Seaborn:-
It will provide a variety of visualization patterns. Also use syntax and has interest in the default themes.
The visualization is used if we have to summarize data and show the distribution in data.
Matplotlib Python :-
It has multiple figures opened but closed explicitly.
The plt.close () will close the current figure and plt.close (‘all’) will close em all.
Seaborn:-
It automatically generates multiple figures and leads to OOM i.e. out of memory issues.
3 .visualization:-
Matplotlib:-
It is the graphics package for data visualization. And well integrated with pandas and numpy.
The pyplot module has MATLAB plotting and the commands closely.
The MATLAB user easily transmits plotting with the python programming.
Seaborn:-
It is integrated for working with the data frames and extends the matplotlib for creating graphics using set methods.
Matplotlib Python :-
It works with data frames and arrays.
They have different stateful API for plotting.
The plot() calls without parameters, without having to manage the parameters.
Seaborn:-
Seaborn will work with the dataset and is more intuitive than matplotlib.
The replot () is API parameter to specify the type of plot which is line, bar, etc.
The plot () is used to pass the object.
5. Flexibility:-
Matplotlib Python :-
It is highly powerful and customizable.
Seaborn:-
It allows the boilerplate and provides the themes which are used.
6. Use case:-
matplotlib:-
The pandas use matplotlib and it is a neat wrapper around matplotlib.
seaborn:-
Seaborn uses the cases and it is matplotlib under hood.
Also called as the statistical plotting.
Which we should use:-
The seaborn and matplotlib visualization tools are used in python and have a difference in the use of scalability and other things.
The visualization tool should be selected for a particular work.
If anybody is doing statistics the seaborn is of good choice because it has a lot of things for the statistical tasks.
Features of python plot are,
Tasks performed by visualization:-
A. Analysis:-
The data analysis id used for transforming, inspecting and modeling the data to derive information.
Taking decisions means analyzing the past.
The analysis is used for better decision of the organization.
B.Visualize:-
First, analyze the data and make it complex and usable.
The data is represented in the tabular form and used where the user look with specific measurement.
C.Transform data set:-
The standard of the data used to decide for effectively.
D.Document insight:-
It is the process useful for the data which is organized in document format.
E.Python Matplotlib Terminology:-
The python programming has the terminologies as the,
The data visualization has considerations as:-
Sr.no |
Function and Description |
1 |
Imread:-Reading the image from the file in array. |
2 |
Imsave:-Save the array in image file. |
3 |
Imshow:-Display an image on the given axis |
Sr.no |
Function and Description |
1 |
‘-’ it is the solid line style |
2 |
‘-.’Dash dot line style |
3 |
‘--’ Dashed line style |
4 |
‘:’ It is the dotted style |
5 |
‘,’ pixel marker |
6 |
‘.’point marker |
7 |
‘o’ circle marks |
8 |
‘<’ triangle left |
9 |
‘>’triangle right |
10 |
‘1’tri down marking |
11 |
‘2’tri up |
12 |
‘+’used as the plus marker |
The color abbreviation is as:-
character |
color |
g |
green |
b |
blue |
c |
cyan |
r |
red |
y |
yellow |
m |
magenta |
w |
white |
k |
black |
It will come with plot that will help to understand the pattern and make correlations.
Example:-
import matplotlib.pyplot as plt
import numpy as np
data=np.arrange (100,201)
plt.plot (data)
plt.show ()
Output:-
There are list of the information’s about the sales and advertisement and use the list for making line plot in python language.
First step is the importing of matplotlib,
Example:-
import matplotlib.pyplot as plt
y= [1, 4, 9, 16, 25, 36, 49, 64]
x1= [1, 16.30, 42, 55, 68, 77, 88]
x2= [1, 6.12, 18, 28, 40, 52, 65]
After loading the matplotlib some data is used for making the line plot by using simple code,
plt.plot (tv.sales)
plt.show ()
Output:-
Example:-
from matplotlib import pyplot as plt
x= [5, 2, 9, 4, 7]
y= [10, 5, 8, 4, 2]
plt.plot(x, y)
plt.show ()
Output:-
These bars are plotted horizontally or vertically.
It wills show the comparison among discrete categories as one axis shows the specific category and the other axis will represent the measured value.
Matplotlib will provide API function to be used in the MATLAB style and the signature is used with axes of the objects as,
Ax.bar(x, height, width, bottom, align)
Parameters:- |
X:-sequence of the scalars |
return |
Container: |
Other parameters |
Color:- |
Example:-
from matplotlib Python import pyplot as plt
x= [5, 2, 9, 4, 7]
y= [10, 5, 8, 4, 2]
plt.bar(x, y)
plt.show ()
It is used for the graphical representation of probability distribution.
Also called the bar chart so by using the bar chart and matplotlib we can easily create the histogram chart.
Advantages:-
Example:-
from matplotlib import pyplot as plt y= [10.5, 8, 4, 2]
plt.hist(y)
plt.show ()
Output:-
The scatter plot is used to plot the data points on the vertical and horizontal axis.It is used to plot the graph and use the data as a collection of points.The data table is represented by x and y axis. It will depend on the two-dimensional values used in the matplotlib.
Parameters:-
Parameters:- |
x, y: - is the scalar and has data positions.
|
Advantages:-
frommatplotlib import pyplot as plt
x= [5, 2, 9, 4, 7]
y= [10, 5, 8, 4, 2]
plt.scatter(x,y)
plt.show()
Output:-
Example:-
import numpy as np
import matplotlib.pyplot as plt
n=60
g1= (0.6+0.6*np.random.rand (N), np.random.rand (N))
g1= (0.4+0.3*np.random.rand (N), 0.5*np.random.rand (N))
g1= (0.3*np.random.rand (N), 0.3*np.random.rand (N))
data= (g1, g2, g3)
Colours= (“red”,”green”,”blue”)
groups= (“coffee”,”tea”,”water”)
fig=plt.figure ()
ax=fig.add_subplot (1, 1, 1, axisbg=”1.0”)
for data, color, group in zip (data, colors, groups):
ax.scatter (x, y, alpha=0.8, color, edgecolors=’none’,s=30,label=group)
plt.title (‘matplot scatter plot’)
plt.legend (loc=2)
plt.show ()
Output:-
The pie chart will display the series of data and show the size of items in a series.
API of pie () will generate the pie diagram for the representation of the data.
The area of each wages is given by x/sum(x).
If the condition as, sum(x) <1 the value of x will give the fractional area and array will not normalized.
Parameters:- |
X:-it is like array wedge sizes. |
Advantages:-
Output:-
The area plot is the same as the line plot also known as stack plots.
It represents the numerical variable and follows another variable.
The stack plots are working on the stacked areas.
The plots are used to track the changes for two or more groups.
Example:-
import matplotlib.pyplot as plt
days= [1, 2, 3, 4, 5]
sleeping= [7, 8, 6, 11, 7]
eating= [2, 3, 4, 3, 2]
working= [7, 8, 7, 2, 2]
playing= [8, 5, 7, 8, 13]
plt.plot ([], [], color=’m’, label=’sleeping’, linewidth=5)
plt.plot ([], [], color=’c’, label=’eating’, linewidth=5)
plt.plot ([], [], color=’r’, label=’working’, linewidth=5)
plt.plot ([], [], color=’k’, label=’playing’, linewidth=5)
plt.stackplot (days, sleeping, eating, working, playing, colors= [‘m’,’c’,’r’,’k’])
plt.xlabel (‘x’)
plt.xlabel (‘y’)
plt.title (‘stack plot’)
plt.legand ()
plt.show ()
Output:-
The bin plot is created using the dataframe plot function and useful if the scatter plot is small.
It will help to bin spatial area of the chart and the color that hexagon can interpret.
Also called lattice and divided into number of the grids.
They are based on the data points of distributions of the variables.
They are used when the number of points in the distribution is large.
Example:-
df=pd.Dataframe (np.random.random (1000, 2), columns= [‘a’,’b’])
df[‘b’] =df [b‘] +np.arange (1000)
df.plot (kind=’hexbin’, x=’a’, y=’b’, gridsize=25)
Output:-
The matplotlib is used in various enviourement as follows,