What is Matplotlib ? || Describe the matplotlib ||
What is Matplotlib ? || Describe the matplotlib ||
Brief Introduction:
- While analyzing any data, instead of looking at raw data in the form of numbers, we get a much better understanding of it, if it is represented in the form of a graph.
- Matplotlib is a very popular open-source graph plotting library that is popularly used to visualize data.
- Matplotlib can be used to draw different types of graphs including, Line graph, Scatter graph, Bar chart, Histogram, pie chart, Violin chart, Contour chart, etc.
- Matplotlib can also be used to work with image and perform different operations on them.
- Graph drawn using matplotlib can either be saved then included in presentation or reports, or they can be embedded into application using GUI toolkits like TKinter, QT and GTK.
- It is possible to create static, animated and interactive data visualizations using Matplotlib library. It is possible to zoom, pan or update the interactive figures created by matplotlib.
- Matplotlib library was create by 'John D. Hunter ' in 2003. It is written in python.
Installation and Usage
- You can install Matplotlib and check its version as follows:
- Matplotlib has multiple versions. Your installed version can be checked as follows:
print ( matplotlib.__version__) # printed 3.5.2 in my case
- Instead of importing the exhaustive 'matplotlib' module, for plotting purposes 'matplotlib.pyplot' module is imported. Since plotting functions in matplotlib library expect the input data in the form of numpy arrays, most Matplotlib programs begin with following import statements:
import numpy as np
First Graph Using Matplotlib
- The following program shows how to use Matplotlib to draw a simple graph.
import matplotlib.pyplot as plt
import numpy as np
xvalues = np.linespace (0 , 2, 100 )
yvalues = np.linespace (0 , 2, 100 )
plt.plot ( xvalues , yvalues )
plt.title (" Simple plot " )
plt.xlabel ( " x values " )
plt.ylabel ( " y values " )
plt.show ( )
On execution, the program produces output shown in figure
image 1
- The call to linespace ( ) create 100 values in the range 0 to 2. Since we wanted to draw a linear graph, we kept xvalues and yvalues same.
- Plot ( ) function is responsible for plotting the values in xvalues and yvalues arrays and title ( ), xlabel ( ) and ylabel ( ) display the title of the graph, x-axis label and y-axis label respectively.
- Show ( ) function manages to display the plot.
- Note that once the graph is displayed, as we move the cursor within the graph, its current position in the bottom right corner of the figure.
- The other buttons in the toolbar at the bottom of the figure let you zoom or pan the figure or even save it to the disk.
Plotting Multiple Graphs
- The following program shows how to plot different growth-rates along with legend for each.
import matplotlib.pyplot as plt
import numpy as np
xvalues = np.linespace (1, 5, 100 )
ylin_values = np.linespace (1, 5, 100 )
yquad_values = xvalues * xvalues
ycubic_values = xvalues * xvalues * xvalues
ylog_values = np.ylog(xvalues)
ynlogn_values = xvalues *np.log(xvalues)
yexp_values = 2** xvalues
mpl.rcParams ['toolbar' ] =' None '
plt.plot(xvalues, ylin_values, label = ' Linear ' )
plt.plot(xvalues, yquad_values, label = ' Quadratic ' )
plt.plot(xvalues, ycubic_values, label = ' Cubic ' )
plt.plot(xvalues, ylog_values, label = ' Logarithmic ' )
plt.plot(xvalues, ynlogn_values, label = ' Log Linear ' )
plt.plot(xvalues, yexp_values, label = ' Exponential ' )
plt.legend ( )
plt.title ( " Rates of growth " )
plt.xlabel (" x values " )
plt.ylabel (y values " )
plt.show ( )
- On execution, the program produces output shown in figure 2
- Multiple graphs are drawn here for different growth rates of y values for x values present in the array xvalues.
- The legend keyword argument of plot ( ) function is used to specify the legend for each graph drawn. They get displayed when the legend ( ) function is called.
- Note that we do not have the toolbar at the bottom of the plot. This has been achieved through the statement.
mpl.rcParams[ 'toolbar ' ] =' None '
- Note that this statement should be executed before any plotting activity begins.
Two Ways to use Matplotlib
- There are 2 ways to use the matplotlib library:
- Using functions in matplotlib.pyplot module. This is the method that we have adopted in the above programs.
- Using the multiple objects present in the Matplotlib library.
- Which approach--function-based or object-based --should be used for plotting graph? Well, for simple graphs function-based approach is good enough. However, if we wish to have a finer control over each element of a graph, then we should take the object-based approach.
- In the rest of the chapter we would use the object-based approach to draw different graphs.
Using Object to Plot Graphs
- The following program shows how to draw the plots shown in figure 2 using object in the Matplotlib library :
import matplotlib.pyplot as plt
import numpy as np
xvalues = np.linespace (1, 5, 100 )
ylin_values = np.linespace (1, 5, 100 )
yquad_values = xvalues * xvalues
ycubic_values = xvalues * xvalues * xvalues
ylog_values = np.ylog(xvalues)
ynlogn_values = xvalues *np.log(xvalues)
yexp_values = 2** xvalues
mpl.rcParams ['toolbar' ] =' None '
(fig,ax) = plt.subplots ( ) # returns a tuple
ax.plot(xvalues, ylin_values, label = ' Linear ' )
ax.plot(xvalues, yquad_values, label = ' Quadratic ' )
ax.plot(xvalues, ycubic_values, label = ' Cubic ' )
ax.plot(xvalues, ylog_values, label = ' Logarithmic ' )
ax.plot(xvalues, ynlogn_values, label = ' Log Linear ' )
ax.plot(xvalues, yexp_values, label = ' Exponential ' )
ax.legend ( )
ax.set_title ( " Rates of growth " )
ax.set_xlabel (" x values " )
ax.set_ylabel (y values " )
fig.show ( )
- Here, we should first understanding the concept of Figure and Axes.
- Figure is an object that represents the whole figure, including the graph, title, label and legend.
- Axes represents the plotting area. So, Axes are contained with a figure. Usually, they are created together through the statement (fig,ax ) = plt.subplots ( ). It create a Figure with one Axes.
- If we wish can first create a Figure and then add Axes later as shown below:
ax =fig.subplots ( ) # add a single Axes to the FIgure
- Note that Axes represents multiple sub-plots in a figure and is not a plural of Axes that represents X-axis and y-axis.
What is Matplotlib ? || Describe the matplotlib ||
Reviewed by For Learnig
on
August 11, 2024
Rating:
No comments:
If you have any doubts, please tell me know