Nothing Special   »   [go: up one dir, main page]

Mat Plot Lib

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

MATPLOTLIB

Matplotlib is a data visualization library in Python. It provides a high-level interface for


drawing attractive and informative statistical graphics. With its wide variety of plotting
options, it is used by data scientists and analysts to create visualizations to support data-
driven decision making. Matplotlib can be used in various environments, such as Jupyter
notebooks, standalone scripts, or web application servers, and supports different output
formats, including PNG, PDF, SVG, and JPG.

Import matplotlib and some necessary libraries: -


We can import matplotlib and some necessary using the import command
import matplotlib as mp
import matplotlib.pyplot as plt

If we want to plot within an IPython notebook we can use ‘%matplotlib’. It comes with two
different commands –
1. %matplotlib notebook for interactive plots.
2. %matplotlib inline for static plots.

Some Basic Commands: -


plt.plot(point1,point2) : To make plot between two points.
plt.show() : To show the graph.
plt.title(‘titlename’) : To give a particular title to the graph.
plt.legend(loc = ‘loc value’) : To give the graph a label. The labels should be
defined in the plot function. The loc value can be 'best', 'upper right', 'upper left', 'lower
left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'.

plt.colorbar() : To get a colorbar in which we can see the color scale and value
comparison i.e. which color is for which value.
Generating sin and cos graph: -
We can plot sin and cos graph using numpy sin method.

More number of intervals means


more smooth the graph

If we want to increase the number of waves then we can multiply pi with 2,3,4… and increase the pi
value –
Add label to the graphs: -
We can add labels to the graphs using the legend( ) method. We have to define the labels in
the plot function.

Change the line dash: -


We can change the line dashes by specifying it inside the plot function.
Change the style of the graph: -
We can check what are the available styles using the ‘plt.style.available’ .

The available styles are –

We can apply any theme by using the plt.style.use(‘style name’ )


Change the figure size: -
We can change the size of the figure using the figure function
plt.figure(figsize = (length,height))

Saving the plots in our local machine: -


We can save the figure in our local machine by using the ‘savefig’ function.
variable = plt.figure()
---Plotting statements---
variable.savefig(‘imagename.extension’)
Change the line colour: -
We can change the colour of any line using the ‘color’ attribute inside the plot function.
plt.plot(point1,point2,color = ‘colorname’)

Change the line width: -


We can change the width of any line using the ‘lienwidth attribute inside the plot function.
plt.plot(point1,point2,linewidth = width value)
Different Types of Plots: -

• Line Plot: - In a line plot the x-axis represents one variable and the y-axis
represents another variable. The data points are connected by lines to
create a plot that shows how the values of the two variables change with
respect to each other. Line plots are often used to visualize time-series
data, where the x-axis represents time, and the y-axis represents a
measurement.

• Multiple Line Plot: - We can also plot multiple lines using the plot
function.
Real Life Datasets: -
We can import real life datasets from sklearn.datasets package. Ex – california housing.

We can plot a graph from the ‘target’ key values of the dataset –
Change limit of x-axis and y-axis: -
We can change the limit of x-axis and y-axis by using the ‘xlim’ and ‘ylim’ function.
plt.xlim([start,end])
plt.ylim([start,end])

*** To avoid adjusting the graph we can use ‘plt.axis(‘tight’)’ or ‘plt.axis(‘equal’)’.

set method: -
The set method is used to combine all the fields like xlim, ylim, xlabel, ylabel, title etc.
fig = plt.axes()
fig.set(xlim,ylim,xlabel,ylabel,title…)
Scatter Plot: -
A scatter plot is a type of data visualization in which individual data points are represented
as points in a two-dimensional plane. The x-coordinate of each data point represents one
feature of the data, and the y-coordinate represents another feature. Scatter plots are
commonly used to visualize the relationship between two variables and to identify patterns
or outliers in the data.
In Matplotlib, scatter plots can be created using the scatter function. The scatter function
accepts two arrays of values, representing the x-coordinates and y-coordinates of the data
points. You can also specify additional arguments to customize the appearance of the
scatter plot, such as the size and color of the markers, the transparency of the markers, and
the marker shape.

plt.scatter(x,y)

There are many parameters in the scatter function –


• c – color
• alpha – transparency
• s – size
• label – label for the graph
• marker – by default the scatter plot elements are round (‘o’). We can
change them as – ‘.’ , ‘,’ , ‘x’ , ‘+’, ‘v’, ‘^’, ‘<’, ‘>’,
‘s’, ‘d’
• cmap – color map
• edgecolor – color of the border of elements
Scatter Plot in a Real life dataset: -
In a real-life dataset like iris we have some data about 4 features – sepal length, sepal
width, petal length, petal width. So we will make a scatter plot between the sepal length
and sepal width.

Same thing we can do between the petal length and petal width
Scatter Plot using random points, random colors and random sizes: -
We can make a scatter plot in a way that the x and y point will be based on random
numbers. The size of the scatters and the color of scatters will also be based on random
numbers and will be different for every scatter.

Histograms: -
A histogram is a plot that displays the distribution of a set of continuous or discrete data.
The data is divided into a set of intervals or bins, and the height of each bar in the plot
represents the number of data points that fall within that bin.
plt.hist(data)
We can explicitly change the number of bins of the histogram by the ‘bins’ field.

More number of random numbers means more uniform the histogram.

We can change the ‘histtype’ field inside hist( ) to change the type of the histogram.

*** There are other histtypes like – ‘step’ , ‘end’, ‘bar’, ‘barstacked’
Multiple Histograms: -
We can plot multiple histograms in the same plot. To do that we have to specify multiple
points and plot them separately using hist( ) functions.
2D Histograms: -
plt.hist2d is a function in matplotlib that generates a 2D histogram. A 2D histogram is a
representation of the distribution of data over two variables. In contrast to a regular
histogram which shows the distribution of a single variable, a 2D histogram shows the
relationship between two variables. The function creates a heatmap that shows the density
of data points in a 2D space.
plt.hist2d(point1,point2)

Here we also have the same attributes as – cmap, bins etc. And we can also add colorbar to
it.
Pie Charts: -
Pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical
proportion.
plt.pie(data,labels = ‘labels list’)

We can explode the value outwards by specifying the ‘explode’ field inside the pie function.

We can also explode only a specific one by setting all others explode value = 0.
We can add shadow to each partition by setting the ‘shadow = True’

We can also change the edge color of the partitions by using the edgecolor attribute.

We can add percentage to each partition by using the ‘autopct’ attribute.

The ‘%.2f%%’ means 2 place after decimal and at the last we are appending a ‘%’.
There are some fields that we can add like ‘edgecolor’, ‘linewidth’ etc. For that we have an
attribute called wedgeprops inside which we use key value pairs for defining the other
properties.
In the example we are changing the edgecolor, linewidth and antialising the diagram i.e.
making the diagram clearer.
Multiple Subplots Index Based: -
Multiple subplots refer to dividing a figure into multiple subplots or axes, each of which can
contain a separate plot. This allows for multiple plots to be displayed in a single figure. In
matplotlib, you can create multiple subplots using the subplot function.

fig,ax = plt.subplots(row,column)
ax[0,0].plot()
ax[1,1].plot()
ax[1,0].plot()
ax[1,1].plot()
Multiple Subplots with loops: -
We can also make multiple subplots using loops using the ‘subplot’ function inside a loop
and the subplots_adjust function to adjust the space between the subplots.
Multiple subplots without indexing: -
We can also make multiple subplots without indexing by specifying different axes names at
the time of subplotting.

fig,(ax1,ax2,ax3,…) = plt.subplots(row,column)
ax1.plot()
ax2.plot()
ax3.plot()…
3D Plots: -
Matplotlib provides a toolkit for 3d plotting named mplot3d which provides functionality
to create 3d plots.
ax = plt.axes(projection = ‘3d’)
ax.plot3D(x,y,z,‘color’)

Scatter Graph in 3D Plots: -

Here ‘111’ means 1 row, 1 column and 1 subplot.

You might also like