Matplotlib in Python

Nishant Bhushan
DevMins
Published in
4 min readDec 15, 2018

What is Matplotlib?

Matplotlib is a plotting library for the Python, Pyplot is a matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source.

What does Matplotlib Pyplot do?

Matplotlib is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

Let us look at how it works :

First, we will import the important library needed for this

# import matplotlib
import matplotlib.pyplot as plt
import numpy as np

1. Line chart

It is a chart in which a series of data are plotted by straight lines, in which we can use line chart (straight lines) to compare related features i.e (x and y). We can explicitly define the grid, the x and y-axis scale and labels, title and display options.

a= range(1,16)
b = np.array(a)**2
#Now by just appliying plot command and the below chart will appear
plt.plot(a,b)

we can change the color of the line using following code

plt.plot(a,b,color='red')

Plotting the line chart from panda DataFrame

delhi_sale = [45,34,76,65,73,40]
bangalore_sale = [51,14,36,95,33,45]
pune_sale = [39,85,34,12,55,8]
sales = pd.DataFrame({'Delhi':delhi_sale,'Bangalore':bangalore_sale,'Pune':pune_sale})

Lets plot line chart and xtricks and ytricks are used to specify the significant range of the axis

sales.plot(xticks=range(1,6),yticks=range(0,100,20))

2. Bar Chart

Bar Chart is used to analyze the group of data, A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally.

Let us look at how it works

a= range(1,16)
b = np.array(a)**2
plt.bar(a,b)

Plotting the Bar chart from panda DataFrame

#we can generate bar chart from pandas DataFrame 
sales.plot(kind='bar')

3. Pie Chart

Pie chart represents whole data as a circle. Different categories make a slice along the circle based on their proportion

a  = [3,4,5,8,15]
plt.pie(a,labels=['A','B','C','D','E'])

4. Histograms

Histogram allows us to determine the shape of continuous data. It is one of the plots which is used in statistics. Using this we can detect the distribution of data, outliers in the data and other useful properties to construct a histogram from continuous data, we need to create bins and put data in the appropriate bin, The bins parameter tells you the number of bins that your data will be divided into.

# For example, here we ask for 20 bins:
x = np.random.randn(100)
plt.hist(x, bins=20)

5. Scatter Plot

It is used to show the relationship between two set of data points. For example, any person weight and height.

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

6. Bow Plot

Bow plot is used to understand the variable spread. In Box plot, rectangle top boundary represents the third quantile, bottom boundary represents first quantile and line in the box indicates medium verticle line at the top indicates max value and vertical line at the bottom indicates the min value.

box_data = np.random.normal(56,10,50).astype(int)
plt.boxplot(box_data)

For detailed implementation and code please visit Github Profile.

Thanks for reading

--

--