Top Posts
Virtual Function in C++
Ceil and Floor functions in C++
Python Rock Paper Scissors Game
AKTU MCA 4 SEMESTER QUESTION PAPERS
Git & Github 2021 Cheat Sheet
Introduction to C Programming Language
Git & Github 2021 Cheat Sheet
10+ C Program to Print Patterns
LUCKNOW BCA 6 SEM QUESTION PAPERS
Currency Converter in Python
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
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • CONTACT US
  • WRITE +
  • ABOUT US
  • HIRE US
Matplotlib Library

Python | Introduction to Matplotlib Library Tutorial

by Prateek Kashyap March 8, 2022
written by Prateek Kashyap 1 comment 1953 views
Python | Introduction to Matplotlib Library Tutorial
Python | Introduction to Matplotlib Library Tutorial

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?

Table of Contents

  • What Is Matplotlib used for?
  • Is Matplotlib Included in Python?
      • Installation:
    •   Python Matplotlib: Types of Plots
    • Example:
    • Output:
    • 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:

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
Code language: Python (python)

  Python Matplotlib: Types of Plots

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

Example:

from matplotlib import pyplot as plt from matplotlib import style style.use('ggplot') x = [5,8,10] y = [12,16,6] x2 = [6,9,11] y2 = [6,15,7] plt.plot(x,y,'g',label='line one', linewidth=5) plt.plot(x2,y2,'c',label='line two',linewidth=5) plt.title('Epic Info') plt.ylabel('Y axis') plt.xlabel('X axis') plt.legend() plt.grid(True,color='k') plt.show()
Code language: Python (python)

Output:

Python | Introduction to Matplotlib Library Tutorial

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()
Code language: Python (python)

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()
Code language: Python (python)

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()
Code language: Python (python)

 

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()
Code language: Python (python)

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()
Code language: Python (python)

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
15
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
Best way to use google search you won’t believe exist
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

  • Virtual Function in C++

    May 19, 2022
  • Ceil and Floor functions in C++

    May 19, 2022
  • Python Rock Paper Scissors Game

    May 18, 2022
  • AKTU MCA 4 SEMESTER QUESTION PAPERS

    May 18, 2022

EDUCATIONAL

  • 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

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

  • 10 mostly asked questions related to WhatsApp

  • Top 8 Programming Languages That Will Rule in 2022

  • Google Cloud Platform

  • Best Online Code Editors For Web Developers

  • How to Write a Synopsis for Project Work

CHEATSHEET

  • Git and Github 2022 Cheat Sheet

  • ReactJs Cheatsheet

  • Linux Commands Cheat Sheet

  • C Programming language Cheatsheet

  • Scala Cheatsheet

  • MySQL Cheatsheet

  • Javascript Cheatsheet

PROJECTS

  • Python Rock Paper Scissors Game

  • Currency Converter in Python

  • Alarm clock GUI application with tkinter

  • Print emojis using python without any module

  • Country Date and Time using Python

  • Covid-19 Tracker Application Using Python

  • Python | GUI Calendar using Tkinter

  • Python: Shutdown Computer with Voice

  • Python GUI Calculator using Tkinter

  • Convert an Image to ASCII art using Python

  • Python YouTube Downloader with Pytube

  • Tic-Tac-Toe using Python

TUTORIALS

  • JAVA TUTORIAL
  • COMPUTER NETWORK
  • DBMS TUTORIAL
  • E-COMMERCE TUTORIAL
  • KNOWLEDGE MANAGEMENT
  • C++ PROGRAMMING
  • COMPUTER NETWORK SECURITY
  • AMAZON WEB SERVICES

TECH NEWS

  • PyScript: Python in the Browser

  • HalloApp is a secure alternative to WhatsApp, made by two early WhatsApp employees

  • 5+ Best Humanoid Robots In The World

  • Windows 11 Now Official, Brings Fresh UI, Centrally-Placed Start Menu

TERMS & POLICY

  • PRIVACY POLICY
  • TERMS AND CONDITIONS

COMPILERS

  • JAVA COMPILER
  • PYTHON COMPILER
  • JS COMPILER
  • C++ COMPILER
  • C COMPILER

JOBS AND INTERNSHIPS

  • TCS off-campus hiring 2022 for software engineers- 2019, 2020, & 2021 Batches

    February 27, 2022
  • Deloitte Recruitment For Any Graduates as Learning Operations Associate Analyst

    February 18, 2022
  • HP Recruitment For Tech Support Intern Position

    February 16, 2022
  • EY Hiring- PAS Global Immigration Advanced Analyst

    February 14, 2022
  • Amazon Recruitment Drive for Virtual Customer Support Associate Position

    February 12, 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
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • CONTACT US
  • WRITE +
  • ABOUT US
  • HIRE US