Module1-Part2 (1)
Module1-Part2 (1)
Data Visualisation
1/40
Data Visualisation
2/40
Bar Chart
A bar chart (or bar graph) is a visual representation of data
where individual bars represent the values of different
categories. It is used to compare quantities across different
categories or groups.
i
mport matplotlib . pyplot as plt
# Sample data
categories = [ ’A ’ , ’B ’ , ’C ’ , ’D ’]
values = [10 , 20 , 15 , 25]
4/40
Types of Bar Charts
5/40
Horizontal Bar Chart
6/40
Python Code for Horizontal Bar Chart
i
mport matplotlib . pyplot as plt
# Sample data
categories = [ ’A ’ , ’B ’ , ’C ’ , ’D ’]
values = [10 , 20 , 15 , 25]
7/40
Grouped Bar Chart
8/40
Python Code for Grouped Bar Chart
i
mport matplotlib . pyplot as plt
import numpy as np
# Sample data
categories = [ ’A ’ , ’B ’ , ’C ’ , ’D ’]
values1 = [10 , 20 , 15 , 25] # First group of
values
values2 = [12 , 22 , 17 , 27] # Second group of
values
# Number of categories
n = len ( categories )
# Define the positions for the bars
x = np . arange ( n )
width = 0.35 # Width of the bars
9/40
...continued (Python Code for Grouped Bar Chart)
11/40
Python Code for Stacked Bar Chart
i
mport matplotlib . pyplot as plt
import numpy as np
# Sample data
categories = [ ’A ’ , ’B ’ , ’C ’ , ’D ’]
values1 = [10 , 20 , 15 , 25] # First group of
values
values2 = [5 , 10 , 10 , 20] # Second group of
values
# Number of categories
n = len ( categories )
# Define the positions for the bars
x = np . arange ( n )
width = 0.5 # Width of the bars
# Create the plot
fig , ax = plt . subplots ()
12/40
...continued (Python Code for Stacked Bar Chart)
14/40
Python Code for Line Chart
i
mport matplotlib . pyplot as plt
# Sample data
years = [2020 , 2021 , 2022 , 2023]
values = [100 , 150 , 200 , 250]
15/40
Pie Chart
16/40
Python Code for Pie Chart
i
mport matplotlib . pyplot as plt
# Sample data
labels = [ ’A ’ , ’B ’ , ’C ’ , ’D ’]
sizes = [20 , 30 , 25 , 25]
17/40
Scatter Plot
18/40
Python Code for Scatter Plot
i
mport matplotlib . pyplot as plt
# Sample data
x = [1 , 2 , 3 , 4 , 5]
y = [10 , 20 , 15 , 25 , 30]
19/40
Understanding 3D Data
20/40
3D Scatter Plots
Description and use cases.
Example: Visualizing relationships between three variables.
21/40
Python Code for 3D Scatter Plot
22/40
...continued (Python Code for 3D Scatter Plot)
# Plot data
sc = ax . scatter (x , y , z , c = colors , s = sizes ,
cmap = ’ viridis ’ , alpha =0.6 , edgecolors = ’w ’ ,
linewidth =0.5)
23/40
...continued (Python Code for 3D Scatter Plot)
24/40
3D Surface Plots
Description and use cases.
Example: Showing topography or a surface of values.
25/40
Python Code for 3D Surface Plot
# Plot data
surf = ax . plot_surface (x , y , z , cmap = ’ viridis ’ ,
edgecolor = ’ none ’)
26/40
...continued (Python Code for 3D Surface Plot)
27/40
Comparative Analysis
28/40
2D Visualization
Benefits:
Simplicity: Easier to create and interpret, especially for simple
datasets.
Clarity: Often clearer for representing data with fewer dimensions.
Performance: Requires less computational power and is faster to
render.
Accessibility: More accessible for audiences not familiar with 3D
concepts.
Limitations:
Limited Data Representation: May not effectively represent complex
datasets.
Less Immersive: Does not provide a sense of depth or spatial
relationships.
29/40
Example Scenario: Projectile Motion
30/40
Projectile Motion
import numpy as np
import matplotlib . pyplot as plt
# Constants
g = 9.8 # Acceleration due to gravity ( m / s ^2)
v0 = 50 # Initial velocity ( m / s )
angle = 45 # Launch angle ( degrees )
# Time of flight
t_flight = 2 * v0 * np . sin ( angle_rad ) / g
# Time array
t = np . linspace (0 , t_flight , num =500)
32/40
...continued (Python Code to Generate Projectile Motion)
# Equations of motion
x = v0 * np . cos ( angle_rad ) * t
y = v0 * np . sin ( angle_rad ) * t - 0.5 * g * t **2
# Plotting
plt . figure ( figsize =(10 , 6) )
plt . plot (x , y , label = ’ Trajectory of the
projectile ’ , color = ’b ’)
plt . title ( ’ Projectile Motion : Vertical Distance
vs . Horizontal Distance ’)
33/40
...continued (Python Code to Generate Projectile Motion)
36/40
Magnetic Field around a Simple Bar Magnet
2D Visualization:
Dataset: Temperature vs. time from a cooling experiment.
Visualization: Line graph showing the cooling curve.
Use Case: Easy to understand how temperature changes over time.
3D Visualization:
Dataset: Distribution of electron density in an atom or molecule.
Visualization: 3D contour plot or isosurface of electron density.
Use Case: Visualizes spatial distribution of electrons, important for
understanding chemical bonding and electronic structure.
38/40
Popular Python Libraries for Visualization
39/40
Tips for Effective Data Visualization
40/40