Matplotlib in python

The Matplotlib library helps you create static and dynamic visualisations. Dynamic visualizations that are animated and interactive. This library makes it easy to plot data and create graphs.

The Matplotlib library helps you create static and dynamic visualizations. Dynamic visualizations that are animated and interactive. This library makes it easy to plot data and create graphs.

install and Import Library

Python
R
Python
Python
R
import matplotlib
# install
Install.packages("ggplot2")

# Load package
library("ggplot2")
Python

Import specific method from matplotlib

Python
Python
Python
import matplotlib.pyplot as plt

Line plot

Let’s use the plot function, for which we provide two columns from datasets.

Python
R
Python
Python
R
import matplotlib.pyplot as plt
# import data preparation library
import numpy as np
# data handling library
import pandas as pd


bill=[25,30,40,50,70,85,95]
tips=[4,6,6.5,7,8.5,9.3,10.8]

print(len(bill),len(tips))
plt.plot(bill,tips)
plt.title('Bill To Tip Ratio')
plt.ylabel('Tips')
plt.xlabel('Bills')
bill
# Load package
library("ggplot2")

df<- data.frame(bill=c(25,30,40,50,70,85,95),tips=(4,6,6.5,7,8.5,9.3,10.8))

library(ggplot2)
# Line plot
ggplot(data=df, aes(x=bill, y=tips, group=1)) + geom_line() + geom_point()
image 3

Bar Plot

Bar plot represent data similar to rectangular bars. That are Normalized by a factor to be represented in a graph. Each bar represents a individual value or variable provided.

Python
R
Python
Python
R
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import numpy as np

figure,axes=plt.subplots(1,1)
employees=np.array([20,50,8,100])
axes.hist(employees,bins='auto')
axes.set_title('Histogram')
axes.set_xlabel('Count')
axes.set_ylabel('Employees')
plt.show()
bill3
library("ggplot2")
df<-data.frame(Department=c('Finance','Manager','Executive'),Employees=c(20,50,8))
ggplot(data=df,aes(x=Department,y=Employees))+geom_bar(stat='identity')
image 5

Histogram

Histograms are similar to graphs. They group variables or classes to represent data in a rectangular bar. These bars are also called as bin.

Python
R
Python
Python
R
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import numpy as np

figure,axes=plt.subplots(1,1)
employees=np.array([20,50,8,100])
axes.hist(employees,bins='auto')
axes.set_title('Histogram')
axes.set_xlabel('Count')
axes.set_ylabel('Employees')
plt.show()
bill3
library("ggplot2")
df<-c(20,50,8,100)
hist(df,xlab="Employee count",col="green",border="black")
image 6

Scatter plot

Scatter plot points out the exact placement of a data point. Using scatter plots, we can find clustered data. We can also use a third variable to change the size of the circle. It will add a third dimension to the graph.

Python
R
Python
Python
R
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import numpy as np

Department=['Finance','Manager','Executive','Worker']
employees=[20,50,8,100]
salary=[75,165,469,25]
figure=plt.figure()
axes=figure.add_axes([0,0,1,1])

axes.scatter(Department,employees,color='b')
axes.set_xlabel('Department')
axes.set_ylabel('Employees')
axes.set_title('Scatter_plot')
plt.show()
bill4 normal
library("ggplot2")
df<-data.frame(Department=c('Finance','Manager','Executive','Worker'),Employees=c(20,50,8,100),salary=c(75,165,469,25))
ggplot(df,aes(x=Department,y=Employees))+geom_point(size=2,shape=46)
image 9

In the following code, we used the third variable, salary, to differentiate data points from each other.

Python
R
Python
Python
R
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import numpy as np

Department=['Finance','Manager','Executive','Worker']
employees=[20,50,8,100]
salary=[75,165,469,25]
figure=plt.figure()
axes=figure.add_axes([0,0,1,1])

axes.scatter(Department,employees,salary,color='b')
axes.set_xlabel('Department')
axes.set_ylabel('Employees')
axes.set_title('Scatter_plot')
plt.show()
bill4
library("ggplot2")
df<-data.frame(Department=c('Finance','Manager','Executive'),Employees=c(20,50,8),salary=c(75,165,469,25))


ggplot(df, aes(x=Employees, y=salary, shape=salary, color=Department)) +
  geom_point()
image 8

Sub Plots

There is a concept of subplots in Matplotlib where we can create a grid of plots. The grid can organise plots.

Python
R
Python
Python
R
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import numpy as np

Department=['Finance','Manager','Executive','Worker']
employees=[20,50,8,100]
salary=[75,165,469,25]

plt.subplot(1,2,2)

plt.bar(Department,salary,width=0.5)
plt.xlabel("Department")
plt.ylabel("Salary")

plt.subplot(1,2,2)
figure=plt.figure()
axes=figure.add_axes([0,0,1,1])

axes.scatter(Department,employees,salary,color='b')
axes.set_xlabel('Department')
axes.set_ylabel('Employees')
axes.set_title('Scatter_plot')

plt.show()
bill6
bill5
library("ggplot2")
df<-data.frame(Department=c('Finance','Manager','Executive','Worker'),Employees=c(20,50,8,100),salary=c(75,165,469,25))

ggplot(data=df,aes(x=Department,y=Employees))+geom_bar(stat='identity')
ggplot(df, aes(x=Employees, y=salary, shape=salary, color=Department))+geom_point()
image 10

Time Series Data Plot

We have saved the stock data in the nested dictionaries. Here we plot accurate data points with plt.plot() and plt.scatter(). By using these, we create connected scatter plots. Using the following method, you can mix multiple types of plots into a single graph.

Python
R
Python
Python
R
# load libraries
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import numpy as np

# create dataset
Stocks=pd.DataFrame({'Year':[2022,2021,2020,2019,2018],'Motors_Turnover':[47263.68,47031.47,43928.17,69202.76,58831.41],'Steel_Turnover':[129021.35,64869.00,60435.97,70610.92,59160.79]})

# plot multiple lines in one plot
plt.plot(Stocks['Year'],Stocks['Motors_Turnover'],label="Tata Motors",color='r')
plt.scatter(Stocks['Year'],Stocks['Motors_Turnover'], color='r')
plt.plot(Stocks['Year'],Stocks['Steel_Turnover'],label="Steel Motors",color='b')
plt.scatter(Stocks['Year'],Stocks['Steel_Turnover'],color='b')
plt.legend()
plt.xlabel('Years')
plt.ylabel('Net TurnOver (Crores)')
plt.title('Information')
plt.show()

matplot8

# Load library
library("ggplot2") 
# load dataset 
stock<-data.frame(Year=c(2022,2021,2020,2019,2018),Motors_Turnover=c(47263.68,47031.47,43928.17,69202.76,58831.410),Steel_Turnover=c(129021.35,64869.00,60435.97,70610.92,59160.79))
# Save two plots in stock_plot 
stock_plot<- ggplot(stock, aes(Year))+geom_line(aes(y=Motors_Turnover),color = "green") + geom_line(aes(y = Steel_Turnover), color = "blue")
stock_plot
image 12

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

  • ANCOVA: Analysis of Covariance with python

    ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.

  • Learn Python The Fun Way

    What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!

  • Meet the most efficient and intelligent AI assistant : NotebookLM

    Start using NotebookLM today and embark on a smarter, more efficient learning journey!

  • Break the ice

    This can be a super guide for you to start and excel in your data science career.

  • Hypothesis Testing: A Comprehensive Overview

    This article delves into the application of hypothesis testing across diverse domains

  • Versions of ANCOVA (Analysis Of Covariance) with python

    To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…

  • Python Variables

    How useful was this post? Click on a star to rate it! Submit Rating Average rating 0 / 5. Vote count: 0 No votes so far! Be the first to rate this post.

  • A/B Testing Quiz

    Complete the code by dragging and dropping the correct functions

  • Python Functions

    Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…

  • Python Indexing: A Guide for Data Science Beginners

    Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.

  • Diffusion Models: Making AI Creativity

    Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…

  • Quiz Challenge: Basics with Python [Questions]

    Solve These Questions in Following Challange

  • Introducing Plethora of Stable Diffusion models: Part 1

    Generate AI images as good as DALL-E completely offline.

One response to “Matplotlib in python”

  1. […] Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. Gain further knowledge from our following article. […]

Leave a Reply

Points You Earned

Untitled design 6
0 distinction_points
Untitled design 5
python_points 0
0 Solver points
Instagram
WhatsApp
error: Content is protected !!