DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Feature Engineering Transforming Predictive Models
  • Comprehensive Guide to Data Analysis and Visualization With Pandas and Matplotlib
  • Python in Urban Planning

Trending

  • How to Practice TDD With Kotlin
  • DGS GraphQL and Spring Boot
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Ethical AI in Agile
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Types of Matplotlib in Python

Types of Matplotlib in Python

In this article, take a look at some types of Matplotlib in Python.

By 
Sai Manikanth Thiyari user avatar
Sai Manikanth Thiyari
·
Aug. 04, 20 · Opinion
Likes (2)
Comment
Save
Tweet
Share
60.9K Views

Join the DZone community and get the full member experience.

Join For Free

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:

Python
 




xxxxxxxxxx
1
10


 
1
from matplotlib import pyplot as plt
2
plt.bar([0.25,1.25,2.25,3.25,4.25],[30,40,10,80,20],
3
label=”Male”,color=’c’,width=.5)
4
plt.bar([0.75,1.75,2.75,3.75,4.75],[50,30,20,50,60],
5
label=”Female”, color=’g’,width=.5)
6
plt.legend()
7
plt.xlabel(‘Days’)
8
plt.ylabel(‘Bed rest(hrs)’)
9
plt.title(‘Information’)
10
plt.show()


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:

Python
 




xxxxxxxxxx
1
15
9


 
1
import matplotlib.pyplot as plt
2
population_age = [22,55,62,45,21,22,34,42,42,4,2,102,95,85,55,110,120,70,65,55,111,115,80,75,65,54,44,43,42,48]
3
bins = [0,10,20,30,40,50,60,70,80,90,100]
4
plt.hist(population_age, bins, histtype=’bar’, rwidth=0.8)
5
plt.xlabel(‘age groups’)
6
plt.ylabel(‘Number of people’)
7
plt.title(‘Histogram’)
8
plt.show()
9
plt.xlabel(‘age groups’)


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:

Python
 




xxxxxxxxxx
1
23


 
1
import matplotlib.pyplot as plt
2
x1 = [1,1.5,2,2.5,3,3.5,3.6]
3
y1 = [7.5,8,8.5,9,9.5,10,10.5]
4
x2=[8,8.5,9,9.5,10,10.5,11]
5
y2=[3,3.5,3.7,4,4.5,5,5.2]
6
plt.scatter(x1,y1, label=’high bp low heartrate’,color=’r’)
7
plt.scatter(x2,y2,label=’low bp high heartrate’,color=’b’)
8
plt.title(‘Scatter Plot’)
9
plt.xlabel(‘x’)
10
plt.ylabel(‘y’)
11
plt.legend()
12
plt.show()
10

          


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:

Python
 




xxxxxxxxxx
1
23


 
1
import matplotlib.pyplot as plt
2
days = [1,2,3,4,5]
3
age =[72,82,61,11,27]
4
weight =[17,28,72,52,32]
5
plt.plot([],[],color=’c’, label=’age’, linewidth=5)
6
plt.plot([],[],color=’g’, label=’weight’, linewidth=5)
7
plt.stackplot(days,age,weight,colors=[‘c’,’g’])
8
plt.xlabel(‘x’)
9
plt.ylabel(‘y’)
10
plt.title(‘Area Plot’)
11
plt.legend()
12
plt.show()
5
age =[72,82,61,11,27]


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:

Python
 




xxxxxxxxxx
1
25


 
1
import matplotlib.pyplot as plt
2

          
3
slices = [12,25,50,36]
4
activities = [‘Prescription drugs’,’clinical services’,’hospital services’,’other services’]
5
cols = [‘c’,’m’,’r’,’g’]
6
plt.pie(slices,
7
labels=activities,
8
colors=cols,
9
startangle=90,
10
shadow= True,
11
explode=(0,0.1,0,0),
12
autopct=’%1.1f%%’)
13
plt.title(‘Pie Plot’)
14
plt.show()
9
plt.pie(slices,


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.

Python (language) Matplotlib Data science

Opinions expressed by DZone contributors are their own.

Related

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Feature Engineering Transforming Predictive Models
  • Comprehensive Guide to Data Analysis and Visualization With Pandas and Matplotlib
  • Python in Urban Planning

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: