[Go to site: main page, start]

0% found this document useful (0 votes)
11 views8 pages

Python Data Visualisation With Matplotlib

This document provides an overview of data visualisation in Python using Matplotlib and Seaborn, covering key techniques and practical applications through a case study. It includes definitions, examples of various plot types, and guidance on data manipulation with NumPy and Pandas. The session aims to equip users with the skills to convert raw data into insightful visual narratives for effective decision-making.

Uploaded by

prasun.ongc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Python Data Visualisation With Matplotlib

This document provides an overview of data visualisation in Python using Matplotlib and Seaborn, covering key techniques and practical applications through a case study. It includes definitions, examples of various plot types, and guidance on data manipulation with NumPy and Pandas. The session aims to equip users with the skills to convert raw data into insightful visual narratives for effective decision-making.

Uploaded by

prasun.ongc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Data Visualisation with Matplotlib

and Case Study

1. Introduction & Context


This session focuses on data visualisation in Python using the Matplotlib and
Seaborn libraries. It covers essential plotting techniques by brushing up on data
visualisation fundamentals with NumPy and Pandas, and demonstrates practical
applications through a detailed case study. The session is designed to help you
convert raw data into insightful visual narratives.
2. Useful Links

Resource 1

Resource 2

Resource 3

3. Main Content

3.1 Data Visualisation with Matplotlib

3.1.1 What is Data Visualisation?

Definition: Data visualisation is the process of converting raw data from sources
like Excel or CSV files into visual formats (charts, graphs, etc.) that reveal patterns,
trends, and insights.

Purpose: It helps in understanding numeric data and is used during data analysis
as well as for communicating results.

Related Libraries: Matplotlib is the primary package for visualisation, with NumPy
and Pandas used for data processing and preparation.
3.1.2 Basic Plotting with Matplotlib

Importing Libraries:

import [Link] as plt


import numpy as np

Creating Data (Example):

time = [Link](0, 20, 0.5) # Creates an array from 0 to 20 in steps of 0.5

Plotting a Time Series:

time_series = [Link](time)
[Link](time_series)

Customising Plots: Adjust Figure and Axes objects (titles, labels, grids) for better
visual presentation.

3.2 Examples of Plots

3.2.1 Line Plot


Usage Example:

[Link](time) # Plots a straight line from the time array


[Link]([Link](time)) # Plots the cosine curve
[Link]([Link](time)) # Plots the sine curve

Multiple Curves: Multiple lines can be plotted on the same graph.

3.2.2 Scatter Plot


Usage Example:

[Link](time, time_series) # Creates a scatter plot of time versus the sine valu

 

3.2.3 Histogram

Usage Example:
[Link](time_series, bins=10, alpha=0.7) # Plots a histogram with 10 bins and speci

 
Bins: Specify the number of intervals. Alpha: Sets the transparency.

3.2.4 Adding Noise

Concept: Noise can be added to a sinusoidal curve using NumPy functions to


simulate real-world data variations.

3.3 Hands-On Exercise: Generating and Plotting a Histogram


Objective: Generate 1,000 random numbers from a normal distribution (mean =
0, standard deviation = 1) and plot a customised histogram.

Steps:

Generate Data:

data = [Link](0, 1, 1000)

Plot Histogram:

[Link](data, bins=30, edgecolor='black', alpha=0.7)


[Link](True)
[Link]()

3.4 Plotting with Pandas

3.4.1 Importing Data

Example:

import pandas as pd
df = pd.read_excel('[Link]')
[Link]()

3.4.2 Selecting Sheets


Usage Example:
composers = pd.read_excel('[Link]', index_col='Composer', sheet_name='Sheet2'

 

3.4.3 Plotting Data with Pandas


Line and Scatter Plots:

[Link](composers['Birth'], composers['Death'])
# Or using [Link]():
[Link](x='Birth', y='Death', kind='scatter')

Customisation: Options include grids, font size, title, and colour maps.

3.5 Using Seaborn for Enhanced Visualisation


Importing Seaborn:

import seaborn as sns

Creating a Scatter Plot:

[Link](data=composers, x='Birth', y='Death', hue='Period')

Histogram using Pandas Plotting:

[Link](alpha=0.5)

Styling with ggplot:

[Link]('ggplot')
3.6 Case Study: Real-World Data Visualisation
Objective: Apply visualisation techniques to solve practical problems using
Matplotlib, NumPy, and Pandas.

Problem Statements:

Visualising Temperature Trends: Generate temperature data for 365 days


using a NumPy array and plot as a line chart with labels, title, and grid.
Monthly Sales Data: Create a Pandas DataFrame with 'Month' and 'Sales'
columns (with random values between 5,000 and 20,000) and plot a bar chart.
Population Distribution: Generate a NumPy array of 1,000 random ages
between 1 and 90, and plot a customised histogram.
Stock Market Volatility: Generate a NumPy array of 250 random values
between -5 and +5, then plot a line chart with red for negative values and
green for positive values.
Pie Chart for Market Share: Create a pie chart for product market share,
customising colours and exploding the slice with the highest market share.

Tools & Techniques: Use generative AI to understand and solve complex


visualisation problems. Combine multiple plotting techniques to extract and
communicate insights.
4. Additional Reading Resources
For further in-depth study of data visualisation techniques and Python data
analysis, refer to:

Official Matplotlib Documentation

Official Seaborn Documentation

Official NumPy Documentation

Official Pandas Documentation

Dataquest Guide to NumPy, Pandas, and Data Visualisation

5. Self-Test Questions
What are the main advantages of using Matplotlib for data visualisation in
Python?
How do you generate a time series using NumPy and plot it with Matplotlib?
Provide a code example.
Describe the steps to customise a histogram in Matplotlib, including setting
bins, edge colour, and transparency.
How would you load an Excel file into a pandas DataFrame and plot two
columns against each other?
What are the benefits of using Seaborn for creating scatter plots over using
Matplotlib directly?
6. Key Insights & Takeaways
Matplotlib and Seaborn offer extensive capabilities for creating a variety of
plots to visually communicate data insights.
NumPy efficiently generates and manipulates numerical data, forming the
basis for high-performance visualisations.
Pandas simplifies data management by allowing seamless data cleaning,
aggregation, and transformation.
Customisation options such as grids, titles, and labels enhance the clarity and
impact of visualisations.
Real-World Case Studies illustrate how to apply these tools to solve practical
problems like temperature trends, sales analysis, and stock market volatility.

7. Conclusion & Summary


This session provided a comprehensive overview of Python data visualisation
using Matplotlib and Seaborn, complemented by essential techniques from
NumPy and Pandas. You learned how to create basic plots (line, scatter,
histogram), customise them, and apply these skills in a real-world case study.
Mastering these tools enables you to transform raw data into compelling visual
narratives, supporting effective decision-making.

Next Steps:

Explore advanced visualisation techniques with libraries such as Plotly.


Apply these methods to your own datasets to deepen your understanding.

You might also like