Graphs
Matplotlib is a low level graph plotting library in
python that serves as a visualization utility.
Scatter Plot
import [Link] as plt
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y
= [99,86,87,88,111,86,103,87,94,78,77,85,86
]
[Link](x, y)
[Link]()
Compare
import [Link] as plt
import numpy as np
#day one, the age and speed of 13 cars:
x =
[Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y =
[Link]([99,86,87,88,111,86,103,87,94,78,7
7,85,86])
[Link](x, y)
#day two, the age and speed of 15 cars:
x =
[Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,1
2])
y =
[Link]([100,105,84,105,90,99,90,95,94,100
,79,112,91,80,85])
[Link](x, y)
Colors
import [Link] as plt
import numpy as np
x =
[Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y =
[Link]([99,86,87,88,111,86,103,87,94,78,7
7,85,86])
[Link](x, y, color = 'hotpink')
x =
[Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,1
2])
y =
[Link]([100,105,84,105,90,99,90,95,94,100
,79,112,91,80,85])
[Link](x, y, color = '#88c999')
[Link]()
Size
import [Link] as plt
import numpy as np
x =
[Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y =
[Link]([99,86,87,88,111,86,103,87,94,78,7
7,85,86])
sizes
= [Link]([20,50,100,200,500,1000,60,90,10
,300,600,800,75])
[Link](x, y, s=sizes)
[Link]()
Alpha
You can adjust the transparency of the dots with the alpha argument.
import [Link] as plt
import numpy as np
x =
[Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y =
[Link]([99,86,87,88,111,86,103,87,94,78,7
7,85,86])
sizes
= [Link]([20,50,100,200,500,1000,60,90,10
,300,600,800,75])
[Link](x, y, s=sizes, alpha=0.5)
[Link]()
Histogram
A histogram is a graph showing frequency distributions.
It is a graph showing the number of observations within each given interval.
import [Link] as plt
import numpy as np
x = [Link](170, 10, 250)
[Link](x)
[Link]()
import [Link] as plt
import numpy as np
# Generate random data
[Link](42)
# For reproducibility
data = [Link](1000)
# 1000 random values from a normal distribution
# Create histogram
[Link](data, bins=30, edgecolor='black', alpha=0.7)
# Add labels and title
[Link]('Value')
[Link]('Frequency')
[Link]('Histogram Example')
# Show plot
[Link]()
Bar chart
import [Link] as plt
import numpy as np
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x,y)
[Link]()
x = ["APPLES", "BANANAS"]
y = [400, 350]
[Link](x, y)
Horizontal Bars
import [Link] as plt
import numpy as np
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x, y)
[Link]()
Bar Color
import [Link] as plt
import numpy as np
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x, y, color = "red")
[Link]()
Color Names Supported by All Browsers
All modern browsers support the following 140
color names
Bar Width
import [Link] as plt
import numpy as np
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x, y, width = 0.1)
[Link]()
The default width value is 0.8
Bar Height
import [Link] as plt
import numpy as np
x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x, y, height = 0.1)
[Link]()
The default height value is 0.8
• Histograms visualize quantitative data or numerical
data,
whereas bar charts display categorical variables.
Creating Pie Charts
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
[Link](y)
[Link]()
Labels
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels = mylabels)
[Link]()
Explode
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
[Link](y, labels = mylabels, explode =
myexplode)
[Link]()
Shadow
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
[Link](y, labels = mylabels, explode =
myexplode, shadow = True)
[Link]()
Legend
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels = mylabels)
[Link]()
[Link]()
Legend With Header
import [Link] as plt
import numpy as np
y = [Link]([35, 25, 25, 15])
mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels = mylabels)
[Link](title = "Four Fruits:")
[Link]()
Line
import numpy as np
import [Link] as plt
x =
[Link]([80, 85, 90, 95, 100, 105, 110, 11
5, 120, 125])
y =
[Link]([240, 250, 260, 270, 280, 290, 300
, 310, 320, 330])
[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)
[Link](axis = 'x')
[Link]()
Linestyle
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, linestyle = 'dotted')
[Link]()
[Link](ypoints, linestyle = 'dashed')
[Link](ypoints, ls = ':')
Plotting Without Line
import [Link] as plt
import numpy as np
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])
[Link](xpoints, ypoints, 'o')
[Link]()
import [Link] as plt
import numpy as np
xpoints = [Link]([1, 2, 6, 8])
ypoints = [Link]([3, 8, 1, 10])
[Link](xpoints, ypoints)
[Link]()
• A heat map is a two-dimensional representation of data
in which various values are represented by colors. A
simple heat map provides an immediate visual
summary of information across two axes, allowing users
to quickly grasp the most important or relevant data
points.
• a heatmap is a graphical representation of data where
values are depicted using colors. The data is typically
arranged in a grid or matrix format, with each cell
assigned a color based on its value.
Basic Heatmap
import numpy as np
import seaborn as sn
import [Link] as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = [Link](low = 1, high = 100, size = (10, 10))
print("The data to be plotted:\n")
print(data)
# plotting the heatmap
hm = [Link](data = data)
# displaying the plotted heatmap
[Link]()
Anchoring the colormap
If we set the vmin value to 30 and the vmax value to 70, then only the cells with values
between 30 and 70 will be displayed. This is called anchoring the colormap.
# importing the modules
import numpy as np
import seaborn as sn
import [Link] as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = [Link](low=1,
high=100,
size=(10, 10))
# setting the parameter values
vmin = 30
vmax = 70
# plotting the heatmap
hm = [Link](data=data,
vmin=vmin,
vmax=vmax)
# displaying the plotted heatmap
Choosing the colormap
we’ll be using tab20.
# importing the modules
import numpy as np
import seaborn as sn
import [Link] as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = [Link](low=1,
high=100,
size=(10, 10))
# setting the parameter values
cmap = "tab20"
# plotting the heatmap
hm = [Link](data=data,
cmap=cmap)
# displaying the plotted heatmap
[Link]()
Displaying the cell values
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = [Link](low=1,
high=100,
size=(10, 10))
# setting the parameter values
annot = True
# plotting the heatmap
hm = [Link](data=data,
annot=annot)
# displaying the plotted heatmap
[Link]()
Crime rate in
city
import numpy as np
import [Link] as plt
import seaborn as sns
# Generate synthetic crime rate data (10x10 city grid)
[Link](42)
city_size = (10, 10) # Grid representing city blocks
crime_data = [Link](lam=5, size=city_size) # Poisson distribution for
crime occurrences
# Create the heatmap
[Link](figsize=(8, 6))
[Link](crime_data, cmap="Reds", annot=True, fmt="d", linewidths=0.5,
cbar=True)
# Labels and title
[Link]("Crime Rate Heatmap of a City")
[Link]("City Blocks (X-axis)")
[Link]("City Blocks (Y-axis)")
# Show the plot
[Link]()
Temperature variation
import numpy as np
import [Link] as plt
import seaborn as sns
# Generate synthetic temperature data (10x10 grid representing a region)
[Link](42)
region_size = (10, 10) # Grid representing different parts of the region
temperature_data = [Link](low=15, high=40, size=region_size) #
Temperatures in °C
# Create the heatmap
[Link](figsize=(8, 6))
[Link](temperature_data, cmap="coolwarm", annot=True, fmt=".1f",
linewidths=0.5, cbar=True)
# Labels and title
[Link]("Temperature Variation Heatmap Across a Region")
[Link]("Region Grid (X-axis)")
[Link]("Region Grid (Y-axis)")
# Show the plot
[Link]()
import pandas as pd
import [Link] as plt
# Load the CSV file
file_path = "[Link]" # Ensure the correct path to your file
df = pd.read_csv(file_path)
# Scatter plot of Years Since PhD vs. Salary
[Link](figsize=(8, 6))
[Link](df["[Link]"], df["salary"], alpha=0.5, color='b')
[Link]("Years Since PhD")
[Link]("Salary")
[Link]("Scatter Plot of Salary vs. Years Since PhD")
[Link](True)
[Link]()