Top Posts
Intranet Application Case Studies
Electronic E-commerce and the Trade Cycle
Types of JDBC drivers
C++ Program to implements Constructor Overloading
C++ Program to implements Constructor
C++ Program to calculate the area using classes
C++ Class Program to Store and Display Employee...
Operator Overloading of Decrement — Operator
Postfix Increment ++ Operator Overloading
Top 8 Programming Languages That Will Rule in...
TECHARGE
  • HOME
  • BLOGS
  • TUTORIALS
    • ALL TUTORIALS
    • PROGRAMMING TUTORIALS
      • JAVA TUTORIALS
      • C++ TUTORIAL
      • C PROGRAMMING TUTORIALS
      • PYTHON TUTORIAL
      • KNOWLEDGE MANAGEMENT TUTORIALS
      • DATA STRUCTURE AND ALGORITHM TUTORIALS
      • PROGRAMMING EXAMPLES
        • CPP EXAMPLES
        • JAVA EXAMPLES
        • C++ GRAPHICS PROGRAM
    • PROJECTS
      • PYTHON PROJECTS
      • SWIFT PROJECT
    • PPROGRAMMING QUIZ
    • DBMS TUTORIALS
    • COMPUTER NETWORK TUTORIALS
    • COMPUTER NETWORK SECURITY TUTORIALS
    • E COMMERCE TUTORIALS 
    • AWS TUTORIAL
    • INTERNET OF THINGS
    • CHEATSHEET
  • MORE
    • JOBS AND INTERNSHIPS
    • INTERVIEW PREPARATION
    • TECH BOOK
    • TECH NEWS
    • INSTAGRAM GALLERY
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • WEB STORIES
    • CONTACT US
  • WRITE +
  • ABOUT US
Matplotlib Library

Python | Introduction to Matplotlib Library Tutorial

by Prateek Kashyap November 26, 2022
written by Prateek Kashyap 1 comment
Python | Introduction to Matplotlib Library Tutorial
Python | Introduction to Matplotlib Library Tutorial
2.7K

Table of Contents

  • What Is Matplotlib used for?
  • Is Matplotlib Included in Python?
      • Installation:
  •   Python Matplotlib: Types of Plots
    • 1.Python Matplotlib: Bar Graph
    • Output:
    • 2. Python Matplotlib: Histogram
    • Output:
    • 3. Python Matplotlib: Scatter plot
    • Output:
    • 4. Python Matplotlib: Area plot
    • Output:
    • 5. Python Matplotlib: Pie Chart
    • Output:

What Is Python Matplotlib?

Matplotlib.pyplot is a plotting library used in the python programming language for 2D graphics. It can be used in python scripts, shells, servers for web applications, and other toolkits for graphical user interfaces.

What Is Matplotlib used for?

Matplotlib is Python Library used for plotting, this python library provides and objected-oriented APIs for integrating plots into applications.

Is Matplotlib Included in Python?

Matplotlib is not part of the default libraries that are installed by default. Some of them are independent downloads, others can be shipped but have additional dependencies with the source code of matplotlib.

Installation:

  • Download Anaconda Navigator(anaconda3)
  • Otherwise open CDM and type:
python -m pip install -U matplotlib

  Python Matplotlib: Types of Plots

  1. Bar Graph
  2. Histogram
  3. Scatter Plot
  4. Area Plot
  5. Pie Chart

1.Python Matplotlib: Bar Graph

To compare data between various groups, a bar graph uses bars. When you want to calculate the changes over a period of time, it is well suited. Horizontally or vertically, it can be interpreted. The crucial thing to note too is that the higher the bar, the greater the value.
Now, using python matplotlib, let us implement it functionally.

import matplotlib.pyplot as plt
   
Country = ['USA','India','Germany','UK','France']
GDP_Per_Capita = [48000,38000,50000,45000,44000]

plt.bar(Country, GDP_Per_Capita)
plt.title('Country Vs GDP Per Capita')
plt.xlabel('Country')
plt.ylabel('GDP Per Capita')
plt.show()

Output:

Python | Introduction to Matplotlib Library Tutorial

2. Python Matplotlib: Histogram

A histogram is used to represent a distribution, while a bar chart is used to compare various entities. When you have arrays or a long list, histograms are useful. Now, using python matplotlib, let us implement it functionally.

import matplotlib.pyplot as plt
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]
bins=[0,10,20,30,40,50,60,70,80,90,100]
plt.hist(population_age,bins,histtype='bar',rwidth=0.8)
plt.xlabel('age groups')
plt.ylabel('Number of people')
plt.title('Histogram')
plt.show()
plt.show()

Output:

Histogram : Python Matplotlib
Histogram : Python Matplotlib

3. Python Matplotlib: Scatter plot

In order to compare variables, we typically need scatter plots, for instance, how much one variable is influenced by another variable to construct a relationship out of it. The data is shown as a set of points, each of which has the value of a single variable that determines the position on the horizontal axis, and the position on the vertical axis is determined by the value of another variable.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8]
y = [4,1,3,6,1,3,5,2]

plt.scatter(x,y)

plt.title('scatter plot')
plt.xlabel('x')
plt.ylabel('y')


plt.show()

 

Output:

Scatter plot : Python Matplotlib
Scatter plot : Python Matplotlib

4. Python Matplotlib: Area plot

Such plots are very similar to the line plot. They are also called as stack plots. For two or more similar classes that make up one whole category, these plots can be used to track changes over time.

import matplotlib.pyplot as plt

days = [1,2,3,4,5,6,7,8]
sleeping= [7,8,6,11,7,5,13,4]
eating=[2,3,4,3,2,2,4,5]
working=[7,8,7,2,2,5,6,8]
playing=[8,5,7,8,13,9,7,10]

plt.plot([],[],color='m', label='Sleeping',linewidth=5)
plt.plot([],[],color='c', label='Eating',linewidth=5)
plt.plot([],[],color='r', label='Programming',linewidth=5)
plt.plot([],[],color='k', label='Playing',linewidth=5)

plt.stackplot(days,sleeping,eating,working,playing,colors=['m','c','r','k'])

plt.title('Stack plot')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

Output:

Area plot : Python Matplotlib
Area plot : Python Matplotlib

5. Python Matplotlib: Pie Chart

A pie chart refers to a circular graph that is divided into parts, i.e. pie slices. Basically, it is used to display the percentage or relative data where a segment is represented by each slice of pie.

import matplotlib.pyplot as plt

days = [1,2,3,4,5]
sleeping= [7,8,6,11,7]
eating=[2,3,4,3,2]
working=[7,8,7,2,2]
playing=[8,5,7,8,13]
slices=[7,2,2,13]
activities=['Sleeping', 'Eating' , 'Playing',  'Programming']
cols=['c', 'm', 'r', 'b']


plt.pie(slices,labels=activities,colors=cols,startangle=90,
              shadow=True,explode=(0,0.1,0,0),autopct='%1.1f%%')

plt.title('Pie plot')
plt.show()

Output:

Python | Introduction to Matplotlib Library Tutorial

Would love to know your feedback, Thank You for reading.

Matplotlibmatplotlib area plotmatplotlib bar graphmatplotlib colorsmatplotlib documentationmatplotlib histogrammatplotlib in pythonmatplotlib inlinematplotlib legendmatplotlib plotmatplotlib pythonmatplotlib scattermatplotlib scatter plotmatplotlib subplotmatplotlib tutorialplot a graphplot graphplotting graphs
Share 16 FacebookTwitterLinkedinRedditWhatsappTelegramEmail
Prateek Kashyap

Studies Computer Science Engineering at Bundelkhand institute of engineering and technology, Jhansi. love programming, specialization in C, C++ and Python programming languages.

previous post
What is Data Communication?
next post
Components of Data Communication

You may also like

Python | GUI Widgets, Date Tick labels, Polar Plots, and XKCD-style sketch...

Python: Plots, Images, Contour and Pseudocolor in Matplotlib

Python : PathPatch ,3D plotting & StreamPlot in Mathplotlib

Python | Ellipse, Pie Charts, Tables and Scatter Plot in Matplotlib

Python | Pyplot in Matplotlib Tutorial

Python | Decay , Bayes Update ,Double Pendulum problem and Oscilloscope in...

1 comment

Anup Kumar MauryA January 7, 2021 - 2:14 pm

Great work Prateek!

Comments are closed.

PYTHON Tutorial

  • Online Python Compiler
  • Getting Started with Python
  • String in Python
  • Python Data Types
  • Python Operators
  • Python Keywords and Identifiers
  • Python Input, Output, and Import
  • Python if else
  • Python List
  • Python Dictionary
  • Python Libraries
  • Matplotib Tutorial
    • Python | Introduction to Matplotlib Library Tutorial
    • Python | Pyplot in Matplotlib Tutorial
    • Python | Ellipse, Pie Charts, Tables and Scatter Plot in Matplotlib
    • Python : PathPatch ,3D plotting & StreamPlot in Mathplotlib
    • Python: Plots, Images, Contour and Pseudocolor in Matplotlib

Keep in touch

Facebook Twitter Instagram Pinterest

Recent Posts

  • Intranet Application Case Studies

    March 21, 2023
  • Electronic E-commerce and the Trade Cycle

    March 21, 2023
  • Types of JDBC drivers

    December 28, 2022
  • C++ Program to implements Constructor Overloading

    December 27, 2022

EDUCATIONAL

  • Top 8 Programming Languages That Will Rule in 2023

  • Difference between Google Cloud Platform, AWS and Azure

  • Google Apps You Should Be Using in 2022

  • Top Sites From Where You Can Learn

  • PyScript: Python in the Browser

  • Best Fake Email Generators (Free Temporary Email Address)

  • How to Find Out Who Owns a Domain Name

  • Mobile phone brands by country of origin

  • How to start a new YouTube Channel in 2022

  • Best way to use google search you won’t believe exist

CHEATSHEET

  • Git and Github 2022 Cheat Sheet

  • ReactJs Cheatsheet

  • Linux Commands Cheat Sheet

  • C Programming language Cheatsheet

  • Scala Cheatsheet

  • MySQL Cheatsheet

  • Javascript Cheatsheet

PROJECTS

  • Print emojis using python without any module

  • Country Date and Time using Python

  • Covid-19 Tracker Application Using Python

  • Python | GUI Calendar using Tkinter

  • Shutdown Computer with Voice Using Python

  • Python GUI Calculator using Tkinter

  • Convert an Image to ASCII art using Python

  • Python YouTube Downloader with Pytube

  • Tic-Tac-Toe using Python

  • Draw Indian Flag using Python

  • Drawing Pikachu with the Python turtle library

  • Word Dictionary using Tkinter

TECH NEWS

  • 5+ Best Humanoid Robots In The World

  • Reliance Jio launches streaming platform JioGamesWatch

  • Microsoft Teams down for thousands of users

  • Carbon: Google programming language as a C++ successor

JOBS AND INTERNSHIPS

  • Accenture Off Campus Hiring Drive | Associate Job | Program Project Management | 2019-2022 Batch| Apply Now

    September 1, 2022

@2019-21 - All Right Reserved. Designed and Developed by Techarge

TECHARGE
  • HOME
  • BLOGS
  • TUTORIALS
    • ALL TUTORIALS
    • PROGRAMMING TUTORIALS
      • JAVA TUTORIALS
      • C++ TUTORIAL
      • C PROGRAMMING TUTORIALS
      • PYTHON TUTORIAL
      • KNOWLEDGE MANAGEMENT TUTORIALS
      • DATA STRUCTURE AND ALGORITHM TUTORIALS
      • PROGRAMMING EXAMPLES
        • CPP EXAMPLES
        • JAVA EXAMPLES
        • C++ GRAPHICS PROGRAM
    • PROJECTS
      • PYTHON PROJECTS
      • SWIFT PROJECT
    • PPROGRAMMING QUIZ
    • DBMS TUTORIALS
    • COMPUTER NETWORK TUTORIALS
    • COMPUTER NETWORK SECURITY TUTORIALS
    • E COMMERCE TUTORIALS 
    • AWS TUTORIAL
    • INTERNET OF THINGS
    • CHEATSHEET
  • MORE
    • JOBS AND INTERNSHIPS
    • INTERVIEW PREPARATION
    • TECH BOOK
    • TECH NEWS
    • INSTAGRAM GALLERY
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • WEB STORIES
    • CONTACT US
  • WRITE +
  • ABOUT US