A brief introduction to MATPLOTLIB
Matplotlib is a plotting library available for the python programming language and is widely used to making graphs, charts, histograms, animations and much more things.
In this post we will mainly focus on the matplotlib’s pyplot function. We would be using pyplot as it is one of the most vast feature of matplotlib.
Importing Matplotlib
Matplotlib’s pyplot can be simply imported from python using the following code.
import matplotlib.pyplot as plt
%matplotlib inline OR %matplotlib notebook
we would also prefer using the magic of jupyter notebooks to use inline / notebook feature. Inline feature would create new graphs and plots on new line whereas the notebook function would update the existing graph instead of making new one.
To plot new figure
To plot new figure we need to write certain code in order to tell the machine that we need a new figure instead of updating the existing one.
plt.figure()
Plotting Some points
We can plot points on the graph using the plot function . Parameters include the points in ( X-cord , Y-cord ) form along with the shape of point like a circle (o) or cross (x) . We will also set the axis length using the gca ( get current axis ) function.
plt.figure()
ax=plt.gca()
ax.axis([0,5,0,5]) // format - [x_min,x_max,y_min,y_max])
plt.plot(3,2,'o')
Scatter-Plots
A scatterplot is a type of data display that shows the relationship between two numerical variables. It can be simply thought as using many plot functions. It can be implemented using the following code-
import numpy as npx = np.array([1,2,3,4,5])y = xplt.figure()plt.scatter(x,y)
Making the Graph Colorful !
The graph can be made colorful by passing the color as an argument to the plot function . In the example we will make starting 4 points of green color and final point as red color.
import numpy as npx = np.array([1,2,3,4,5])y = xcolour = ['green']*(len(x)-1)colour.append('red')plt.figure()plt.scatter(x,y,c=colour)
Adding labels and legend
Labels are usually used to describe the the x and y coordinate and legend is used to describe the points incase points are of different colors.
plt.xlabel('The description of x coordinate')
plt.ylabel('The description of y coordinate')
plt.title('Title')
plt.legend(loc=2,title='legend',frameon=True)
Filling color between the graph
Color between the graph could make it look even better . It could be used to clearly signify details that we want to show. We would implement it using the fill_between code of the gca class. The code is given below
plt.gca().fill_between(range(len(X_axis)),below_line,above_line,facecolor='blue',alpha=0.25)
Bar Chart
Bar graph are used in variety of places to compare one set of datas to another . Here we will look at plotting some bar graphs. Parameters include the data and also use the width parameter used to set the width of the bar column.
plt.figure()
xvals = range(len(data))
plt.bar(xvals,data,width=0.3)
Plotting 2 simultaneous Bar graphs
Here we will use the fact that we can change the width of the bar column. We will implement this using the same bar function of the plt function
new_val=[]
for item in val:
new_val.append(item+0.3)
plt.bar(new_val,data)
Error line in bar graph
Error lines can be drawn in the graph using the yerr parameter in the bar function. The code is given below
from random import randint
linear_error =[randint(0,15) for x in range(xvals)
plt.bar(xvals,data , width=0.3 ,yerr=linear_error)
Stacked and Horizontal bar graphs
There are various type of bar graphs one of them is stacked bar graphs in this section we will how easily we can implement them.We will use the parameter bottom of bar function which sets the bottom height of the bar graph
plt.figure()
xvals=range(len(data))
plt.bar(xvals,data,width=0.3,c='b')
plt.bar(xvals,data2,bottom=data,c='r')
Simmilarly horizontal bar charts can be made using the plt.barh function.The given image is just for illustration.
plt.barh(xvals,data,height=0.3,c='b')
plt.barh(xvals,data2,h=0.3,left=data,c='r')
Subplots
Subplots can be made if we want to make 2 or more graph side-by-side to compare them . They are very useful for direct comparison.We will make them using the subplot function
plt.figure()
plt.subplot(1,2,1)
plt.plot(data,'-o')
plt.subplot(1,2,2)
plt.plot(data2,'x')
The parameter in subplot is defined as (nb_of_rows,nb_of_columns,which_subplot_to_refer). The given diagram will make it more clear.
Histogram
Histograms are one of the most useful graphs for comparison of any features. We will have a look on how to plot them.
fig,((ax1,ax2),(ax3,ax4)) = plt.subplot(2,2,sharex=True)
axs=[ax1,ax2,ax3,ax4]
for n in range(len(axs)):
no_of_sample = 10*(n-1)
sample = np.random.normal(loc=0,scale=1,size=sample)
ax[n].hist(sample)
The given image is just for illustration . Do try to implement the above code.
Heatmaps
Heatmaps have various application to find the density of sample distribution. Here we will try to implement them.
plt.figure()
Y=np.random.normal(loc=0,scale=1,size=1000)
X=np.random.random(size=1000)
_ = plt.hist2d(X,Y,bins=25)
plt.colorbar() // A type of Legend
Summary of when to use which plot!
That’s all from my side.
Hope you have learned something new from here.
Do leave a clap if you liked it.
Keep Learning,
Shrey Pandit