The Story of Matplotlib:Part 1

The Story of Matplotlib:Part 1

Matplotlib is a Data Visualization library in Python.There are various types of graphs and charts available which help us understand data in a better fashion. An analogy would be "Matplotlib are the eyes of data science(ie they help visualize data)."

Plotting the most basic graph

Before I tell you anything else let me tell you how can you create the easiest graph. So first we import the matplotlib library.Then we pass 2 arrays to plot() function(ie x and y). To display the graph we use the show() function.

import matplotlib.pyplot as plt
X=[1,2,3]
y=[3,2,1]
plt.plot(X,y)
plt.show()

pic1.png

Adding labels,titles

Adding label to the x and y axis is necessary to determine the kind of data we wish to represent. This is done with the help of xlabel and ylabel in matploltlib. To add the title use the title function Make sure to write this code above the show() function.

plt.xlabel('Label X')
plt.ylabel('Label y')
plt.title('This is the title')

pic3.png

Scatter plots

Scatter plots are used to represent data in a point(or dot) like fashion. To plot scatter plots we use scatter() function.

The function has various other attributes like

marker(used to show the shape of the plotting point), color

size(represented by 's')

X=[1,2,3,4,5,6]
y=[2,4,6,8,10,12]
#colour is red with size of points as 100 and the shape of the point *
plt.scatter(X,y,color='red',marker='*',s=100)

pic5.png

Bar Charts

To plot th bar graph we use the bar() function. It has attributes like

Height-The height of the bar

Tick label-Labels given on the x axis to represent each bar

Width-The width of the bar

Colour-The colour of the bar

Alpha-The opacity of the graph

x = [1, 2, 3]   
height = [10,25,30] 

# labels for bars 
tick_label = ['A','B','C'] 

# plotting a bar chart 
plt.bar(x, height, tick_label = tick_label, 
        width = 0.5, color = 'green',alpha=0.6)

pic6.png

More on matplotlib in Part 2

Any feedback or constructive criticism is welcomed. You can either find me on Twitter @malavika1501 or email me