Mat Plot Lib
Mat Plot Lib
Mat Plot Lib
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.
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.
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.
• 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])
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)
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.
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.
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’)