Saturday, May 21, 2022
HomeMachinelearningMatplotlib for Machine Learning

Matplotlib for Machine Learning

Machine Learning : matplotlib

Matplotlib, a library in python helps us to draw the graph, it well known as a plotting library for python. As we know our mind can easily understand the diagram/graph instead of text. As the name plotting library, so it helps in the control of every aspect of a figure/plot. It is an open-source library so we can easily and freely use it.

How to use matplotlib?

Well, if python and PIP is present on your system then to install the matplotlib is quite easy.

Just check out the path as shown below and you are ready to go.

C:UsersmyName>pip install matplotlib or

Conda install matplotlib.

If the above command fails, then you can go through python distribution which already has matplotlib, for example, Anaconda etc.

If you are using GoogleColab, just import it and you are good to go.

import matplotlib.pyplot as plt

How to check the version matplotlib?

You can easily check the version by just specifying the underscore two times before and after the keyword “version”. Here is the example,

import matplotlib as plt
print(plt.__version__)

How many types of graphs are plotted using matplotlib?

Well, I recommend you to go through the official website. Where you can see the various option like Line plot, Pie chart, Bar charts etc.

How to display plot using jupyter notebook?

You have to specify this line of code,

%matplolib inline, using it allows you to see the plots you have created inside the jupyter notebook. If you are using other than Jupyter then you need to use the SHOW().

How to draw a plot using matplotlib?

To draw the plot/graph we required two libraries if you are using array, 1) matplotlib 2) numpy

import matplotlib.pyplot as plt

import numpy as np

How many ways to create the plot?

There are two ways to create the plot. 1) Functional method 2) Object-Oriented method.

In this blog post we will discuss about the first one which is Functional method.

Functional method

xpoints = np.array([0,1,2,3,4,5])

ypoints = np.array([50,40,30,20,10,0])

plt.plot(xpoints, ypoints)
plt.show()

Plot, takes two argument. The first parameter works as an X-axis and second parameter works as a Y-axis. By default, it will display the line. If you want to plot without the line pass the third argument/parameter ‘o’, which means rings or marker.

How to add label and title to our plot?

We have to method named xlabel and ylabel to display the label in our plot.

plt.xlabel(‘Test X’)
plt.ylabel(‘Test Y’)
plt.title(‘Test Title’)

How to draw multi plot on the same canvas?

In order to draw multi plot need to use the “subplot”. Which required some argument like, Number of rows, number of column and plot number you are referring to in this case we just use 1.

plt.subplot(1,2,1)
plt.plot(xpoints,ypoints,’r’)
plt.subplot(1,2,2)
plt.plot(ypoints,xpoints,’b’)
plt.show()

How to add title to entire figure?

plt.subplot(1,2,1)
plt.plot(xpoints,ypoints,’r’)
plt.title(“Test1”)
plt.subplot(1,2,2)
plt.plot(ypoints,xpoints,’b’)
plt.title(“Test2”)
plt.suptitle(“Main Title”)
plt.show()

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments