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 | Pyplot in Matplotlib Tutorial

by Prateek Kashyap November 27, 2022
written by Prateek Kashyap 0 comment
1.6K

Table of Contents

  • Introduction to Pylot
      • Output:
  • Formatting the style of your plot in Matplotlib
      • Output:
  • Plotting with Keywords Strings in Matplotlib
      • Output:
  • Plotting with categorical variables in Matplotlib
      • Output:
  • Working with Text in Matplotlib
      • Example 1:
      • Output:
      • Example 2:
      • Output:

In this article, you’ll learn about pyplot in Matplotlib, Formatting the style of your plot in Matplotlib, Plotting with categorical variables in Matplotlib, and Working with Text in Matplotlib.

Introduction to Pylot

Matplotlib.pylot is a set of functions which, make matplotlib work like MATLAB. Each pyplot function modifies the figure somewhat for example, creates a figure, generates a plotting area in a figure, plots certain lines in the plotting area, decorates the plot with labels, etc.

Various states are maintained through function calls in matplotlib.pyplot. To keep track of things such as the current figure and plotting area and the plotting functions are aimed at the current axes (please note that “axes” here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[1,3,6,12,18])
plt.ylabel('some number')
plt.show()

Output:

Python | Pyplot in Matplotlib Tutorial

Formatting the style of your plot in Matplotlib

There is an optional third argument for every x,y pair of arguments, which is the format string that shows the plot colour and line type. The letters and symbols of the format string are from MATLAB, and you concatenate a colour string with a line type string. The default format string is ‘b-‘. which is a solid blue line For example, to map the above with red circles, you would issue.

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[1,3,6,12,18],'ro')
plt.ylabel('some numbers')
plt.show()

Output:

Python | Pyplot in Matplotlib Tutorial

Plotting with Keywords Strings in Matplotlib

There are several cases where you have data in a format that allows you with strings to access specific variables. For instance,. With pandas.DataFrame or numpy.recarray.
Matplotlib allows you to use the data keyword argument to have such an entity. You can generate plots with the strings corresponding to these variables, if given.

import matplotlib.pyplot as plt 
import numpy as np
 
data = {'a': np.arange(50), 
              'c': np.random.randint(0, 50, 50), 
              'd': np.random.randn(50)} 
data['b' ]=data['a'] + 10 * np.random.randn(50) 
data['d'] = np.abs(data['d']) * 100
 
plt.scatter('a' ,'b', c='c', s='d', data=data) 
plt.xlabel('Entry a') 
plt.ylabel( 'Entry b') 
plt.show()

Output:

Python | Pyplot in Matplotlib Tutorial

Plotting with categorical variables in Matplotlib

It is also possible to use categorical variables to construct the plot. You can transfer categorical variables directly to several plotting functions via Matplotlib. For example:

import matplotlib. pyplot as plt 
import numpy as np 

names = [ 'group a', 'group_b' ,  'group_c'  ] 
values = [1, 10, 100] 

plt.figure(figsize=(9, 3))
 
plt.subplot(131) 
plt.bar(names, values) 
plt.subplot(132) 
plt.scatter (names, values) 
plt.subplot(133)
plt.plot(names, values) 
plt.suptitle( 'Categorical Plotting') 
plt.show() 

Output:

Python | Pyplot in Matplotlib Tutorial

Working with Text in Matplotlib

You can use text to add text at an arbitrary location, and you can use xlabel, ylabel, and title to add text at the specified locations.

Example 1:

import matplotlib.pyplot as plt 
import numpy as np
mu, sigma=100,15
x= mu + sigma *np.random.randn(10000) 

 
n, bins, patches=plt.hist(x, 50, density=1, facecolor='g',alpha=0.75)
 
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ') 
plt.text(60, .025, r'$\mu=100, \ \sigma.15$') 
plt.axis([40, 160, 0, 0.03]) 
plt.grid(True) 
plt.show() 

Output:

Python | Pyplot in Matplotlib Tutorial

Example 2:

import matplotlib.pyplot as plt 
import numpy as np 
ax = plt.subplot(111)
 
t=np.arange(0.0, 5.0, 0.01) 
s=np.cos(2*np.pi*t) 
line = plt.plot(t, s, lw=2)
 
plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05)) 
plt.ylim(-2, 2) 
plt.show()

Output:

Python | Pyplot in Matplotlib Tutorial

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

import matplotlib.pyplot as pltMatplotlibmatplotlib documentationmatplotlib python 3matplotlib tutorialpyplot in matplotlib
Share 8 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
Components of Data Communication
next post
Data Representation

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 | Introduction to Matplotlib Library Tutorial

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

Categories

  • Amazon Web Services
  • C Examples
  • C Programming Tutorial
  • C# Programming Tutorials
  • C++ Graphics Program
  • C++ Programming Tutorials
  • C++ STL
  • Cheatsheet
  • Computer Fundamental And Office Automation
  • Computer Network
  • Computer Network Security
  • CPP Examples
  • Data Structure and Algorithm
  • Dbms Tutorials
  • Design
  • E-Commerce Tutorials
  • Economics
  • Educational
  • Events
  • Git and GitHub
  • Html Tutorials
  • Information System Analysis Design And Implementation
  • Internet
  • Interview Preparation
  • IoT Project
  • IoT-Internet of Things
  • Java Program Examples
  • Java Tutorial
  • JavaScript
  • JavaScript Example
  • JavaScript MCQs
  • Jobs and Internships
  • JSP
  • Knowledge Management
  • Linux
  • Matplotlib Library
  • Operating System Tutorial
  • Operator Overloading
  • Paper
  • Programming
  • Python Examples
  • Python Project
  • Python Tutorial
  • Searching Algorithm
  • Servlets
  • Sorting Algorithms
  • SQL Tutorial
  • SYSADMIN
  • Tech News
  • Technology
  • Web Development

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