Strategy

Types of Matplotlib in Python

The data visualizations are the graphical representation of data which produces the images to map the relationships among the data values. There are many visualization tools in the market which can generate automated charts or graphs by collecting the data from various sources. Python is a very simple programming language which is widely used in the market for carrying the data science work to meet the business needs. Python has got its own packages for displaying the charts or graphs.

Matplotlib in Python is a package that is used for displaying the 2D graphics. The Matplotlib can be used in python scripts, shell, web application servers and other GUI toolkits. Python provides different types of plots such as Bar Graph, Histogram, Scatterplot, Area plot, Pie plot for viewing the data. Let us now discuss these types of Matplotlib in detail.

1. Bar Graph Using Matplotlib

The bar graphs are used in data comparison where we can measure the changes over a period of time. It can be represented horizontally or vertically. Longer the bar it has the greater the value it contains.

Example code:

Result:

We have generated the bar graphs for the plots {(0.25,30), (1.25,40), (2.25,10), (3.25,80), (4.25,20)}and {(0.75,50), (1.75,30), (2.75,20), (3.75,50), (4.75,60)}.We have assigned cyan colour for the label male and green colour for the label female in the bar function. The x-axis is labelled using xlabel = Days and y-axis is labelled using ylabel = Bed rest(hrs). In this graph the comparison is made between male and female patients over a period of 5 days.

2. Histogram Using Matplotlib

The histogram is used where the data is been distributed while bar graph is used in comparing the two entities. Histograms are preferred during the arrays or data containing the long list. Consider an example where we can plot the age of the population with respect to the bin. The bin refers to the range of values divided into a series of intervals. In the below example bins are created with an interval of 10 which contains the elements from 0 to 9, then 10 to 19 and so on.

Example code:

Result:

3. Scatter Plot Using Matplotlib

The scatter plots are preferred while comparing the data variables to determine the relationship between dependant and independent variables. The data is displayed as a collection of points, each having the value of one variable which determines the position on the horizontal axis and the value of other variable determines the position on the vertical axis. Below is an example of implementing two different scatter plots on a single graph.

Example code:

Result:

The above example has two scatter plots, the data is displayed as a collection of points having “high bp low heart rate” and “low bp high heart rate”. The scatter plot points of (x1,y1) are {(1,7),(1.5,8),(2,8.5),(2.5,9),(3,9.5),(3.5,10),(3.6,10.5)} and (x2,y2) are {(8,3),(8.5,3.5),(9,3.7),(9.5,4),(10,4.5),(10.5,5),(11,5.2)}.

4. Area Plot Using Matplotlib

The area plots were also called as stack plots. It is quite similar to the line plots. Area plots are used in tracking the changes over time for two or more related groups that make one whole category.

Example:

Result:

In the above example, we can see the time spent based on the categories of age and weight. Area plot or stack plot is used to show the trends over time among different attributes.

5. Pie Chart Using Matplotlib

A pie chart is a circular graph which is divided into segments or slices of pie. It is used to represent the percentage or proportional data where each slice of pie represents a category.

Example code:

Result:

In the above example, we have divided the circle into 4 segments which represent the “Prescription drugs”, ”clinical services”, ” hospital services”, ”other services”. The slices list contains the values of the categories which automatically performs the percentage which they hold per each slice.