Python for Data Analytics
Module 3: Data Visualization
Data Visualization
Module 3 introduction
Module 3 outline Peer to peer loans
Plotting Visual appeal
Multiple charts
with matplotlib with Seaborn
Column charts Styles Charts on charts
Scatter plots Color palettes Side-by-side
Chained methods Box plots Looping techniques
Histograms
Sean
SeanBarnes
Barnes
Data Visualization
Plotting with matplotlib
Matplotlib
● A visualization module
import matplotlib
● Customization features:
○ Titles
○ Annotations
○ Colors
○ Axis limits
○ Label formatting
● Hundreds of thousands of lines of
code already written for you
Sean
SeanBarnes
Barnes
Figure
Figure
● Can customize figure directly
Axes
○ Control the size of the canvas
Legend
○ Set options like background color
Axes (plots)
● Create using different functions:
Grid
○ .plot() Have named arguments:
○ .scatter() ● Data for plotting
○ .hist() ● Data ink for that chart type
Additional chart elements
● .title() ● .annotate() Label
● .xlabel() ● .legend()
● .ylabel()
Sean
SeanBarnes
Barnes
Scenario
🏆 Goal: Develop a state-of-the-art risk management strategy for
providing loans to different communities across the United States
📊 Dataset: Loans from Lending Tree, a peer-to-peer lending platform
🎯 Task: Conducting exploratory data analysis to better understand
the characteristics of loans with different levels of risk
⬜ Develop a report of findings to share with the bank
You
⬜ Develop insightful visualizations to help client understand
Data Analyst different risk profiles
Sean
SeanBarnes
Barnes
Recap: Matplotlib
1. Select & order data using Pandas 3. Use [Link]():
df["grade"].value_counts().sort_index() ● To clean up the output of each code cell
● Multiple times to display multiple plots
2. Stack commands to enhance visualizations
import [Link] as plt
sorted_grades.plot(kind="bar")
[Link]("")
[Link]("Frequency")
[Link]("Frequency of Loan by Grade")
Sean
SeanBarnes
Barnes
Data Visualization
Colors, grids, & saving plots
Recap: Colors, grids, & saving plots
● To specify color for chart: ● To save an image
"8af133"
sorted_grades.plot(kind="bar", color="purple") [Link]("loan_column_chart.png")
File name
● To use list to give each bar its own color: Any common image format will work:
colors = ["Green", "Lime", … , "Red"] ● .jpeg
● .svg
sorted_grades.plot(kind="bar", color=colors)
● .pdf
● To add grid lines:
[Link](axis="y", color="black", alpha=0.7, linestyle="--")
Specify x or y Color Line style
axes, or both
Sean
SeanBarnes
Barnes
Data Visualization
Text & annotations
● To adjust size and style of the font:
[Link]("Frequency of Loan by Grade", fontsize=16, fontweight="bold", pad=15)
[Link]("Frequency", fontsize=14)
● Move annotation text:
[Link](text="Loans Grade E and below \nare very high risk.",
Position for the text xy=(4, 50),
How to move relative to point xytext=(-10, 30),
Didn’t set absolute value textcoords="offset points",...)
for text location
● Used LLM to:
● Create arrow
● Label each bar with
its frequency
Sean
SeanBarnes
Barnes
Data Visualization
Ticks & spines
Recap: Ticks & spines
● To rotate the x axis labels
[Link](rotation=0)
● To save the result of a plot method into a variable:
ax = sorted_grades.plot(kind="bar", color=colors)
● To add more ticks to plot:
[Link].set_minor_locator(AutoMinorLocator(2))
● To remove the spines from plot:
[Link]["left"].set_visible(False)
Sean
SeanBarnes
Barnes
Data Visualization
Grouped column charts
Scenario 🏆 Goal: Identify key characteristics of loans from
states with highest average loan amount
● District of Columbia
● Alaska
● Hawaii
🎯 Task: Plot the loan amount by grade across these
three states using a grouped bar chart Alaska A
1. Set up your data:
Alaska B
● Filter data to only include top three states
You ● Group by state and grade Hawaii A
Data Analyst ● Select loan amount column and calculate ..
mean for each group .
● Create a grouped column chart showing the
mean value of loans of each grade
Sean
SeanBarnes
Barnes
MultiIndex
Recap: Grouped column charts state grade
AK A 25750.000000
1. Selected the rows of interest B 30833.333333
C 12500.000000
# names of top three states for loan amount D 11100.000000
states = ["DC", "AK", "HI"] DC A 40000.000000
filtered_df = df[df["state"].isin(states)] B 12500.000000
C 25900.000000
HI A 1200.000000
2. Grouped by two features B 17733.333333
D 10000.000000
grouped_df = filtered_df.groupby(["state", "grade"]) E 31666.666667
F 28000.000000
3. Calculated mean of loan amount for each state Name: loan_amount, dtype: float64
and grade combination
✅ A lot of flexibility to create unique rows
grouped_loan_amount = grouped_df["loan_amount"].mean()
⛔ To plot, you’ll need to use .unstack()
Sean
SeanBarnes
Barnes
.unstack()
state grade A B C D E F
AK A 25750.000000
B 30833.333333 AK 25750.0 30833.3 12500.0 11100.0 NaN NaN
C 12500.000000
D 11100.000000 DC 40000.0 12500.0 25900.0 NaN NaN NaN
DC A 40000.000000
B 12500.000000
HI 1200.0 17733.3 NaN 10000.0 31666.6 28000.0
C 25900.000000
HI A 1200.000000
B 17733.333333 grouped_loan_amount.unstack().plot(kind = "bar")
D 10000.000000
E 31666.666667
F 28000.000000 Columns are grouped by index automatically
Sean
SeanBarnes
Barnes
Data Visualization
Stacked column charts
Scenario 🏆 Goal: Understand whether the composition of homeownership
changes based on the risk profile of the loan
🎯 Task: Highlight the proportion of renters for each group
Exploring total loan by:
● Grade
● Homeownership status
Perform similar steps:
● Grouping
You ● Aggregating
Data Analyst ● Unstacking before plotting
Sean
SeanBarnes
Barnes
Recap: Stacked column chart
● Create a stacked column chart rather than grouped one
grouped_df.plot(kind = "bar", stacked = True,
color=["lightgray", "darkgray", "mediumseagreen"])
● Compare within a category rather than across them
● Worked with LLM to develop 100% stacked bar chart:
proportion_df = grouped_df.div(grouped_df.sum(axis=1), axis=0)
proportion_df.plot(kind = "bar", stacked = True,
color=["lightgray", "darkgray", "mediumseagreen"])
● Easier comparison across the different categories
Sean
SeanBarnes
Barnes
Data Visualization
Scatter plots
Scenario 💡 Findings: Annual income is a moderate predictor of total
credit limit
○ Pearson correlation coefficient of 0.55
🎯 Tasks:
○ Graph how income
distribution impacts
amount of credit they
should offer
You
○ Visualize credit limits of
Data Analyst
incomes in top 5%
Sean
SeanBarnes
Barnes
Recap: Scatter plots Option Style
"^" ▲
● To create a scatter plot: "." ⬤
Columns
[Link](df["annual_income"], df["total_credit_limit"], alpha=0.5 , marker=".", color="darkgreen")
x-axis Transparency Style Color of marker
● To set x axis limit: [Link](0, 500000) [Link](alpha=0)
● To set y axis limit: [Link](...)
● To draw vertical line: [Link](x=top_5_percent_income, color="black", linestyle="--")
● To draw horizontal line: [Link](...)
Sean
SeanBarnes
Barnes
Data Visualization
Method chaining
Method chaining
● Process of linking several (((5+3)×2)−4)÷2
operations in a row
● Each operation depends
((8×2)−4)÷2
on the previous
( 16 − 4 ) ÷ 2
🔗 Analogy: Connecting
operations like links in a chain 12 ÷ 2
6
Sean
SeanBarnes
Barnes
[Link](["grade", "homeownership"])["loan_amount"].sum().unstack().plot(kind="bar", stacked=True)
Select Sum loan Get data into Create
Group by grade & rows and
DataFrame loan_amount amounts for stacked
homeownership columns
column each group bar chart
amount
column
Groupby
from
Object
grouped
data
Sean
SeanBarnes
Barnes
grouped_data = [Link](["grade", "homeownership"])
grouped_data_loan_amount = grouped_data["loan_amount"]
grouped_sum_of_loans = grouped_data_loan_amount.sum()
unstacked_sum_of_loans = grouped_sum_of_loans.unstack()
unstacked_sum_of_loans.plot(kind="bar", stacked=True)
Using variables for each step: Method chaining:
● Useful to save intermediate steps for later ● Get to plot as quickly as possible
● More flexible, but takes longer ● Less flexible
● If you wanted to stop at an intermediate
step, you wouldn’t be able to
Sean
SeanBarnes
Barnes
Data Visualization
Plotting with Seaborn
Seaborn
● A visualization tool
import seaborn
● Works well with matplotlib
● Main strengths:
○ Improved visual appeal
○ Reduced need to manipulate
the data
○ Additional plot types
Sean
SeanBarnes
Barnes
Plotting with Seaborn
At this point in analysis, you’re looking to: Seaborn can help in both:
⬜ Level up visual appeal of visualizations ✅ Improving visual appeal
⬜ Include many plots of different features ✅ Creating many charts quickly
and relationships in data
○ Including distributional charts
Sean
SeanBarnes
Barnes
Recap: Plotting with Seaborn
● Gives additional functions for plotting:
○ [Link]()
○ [Link]()
○ [Link]()
● Automatically summarizes your data: ● Set the estimation to:
Default
○ [Link]
[Link](selected_stocks, estimator = [Link])
○ [Link]
○ [Link]
Sean
SeanBarnes
Barnes
Data Visualization
Themes & palettes
Recap
● Use sns.set_theme() to change default styling
sns.set_theme(style = "white"
)
Default matplotlib look
● Gives more control of visual style of plots
● Used palette named argument
[Link](...palette = "Blues")
[Link](...palette ="RdYlGn_r")
● Avoid manually selecting colors
Sean
SeanBarnes
Barnes
Data Visualization
Box plots
Scenario
🏆 Goal: Characterizing different features present in the dataset
Features of each person: Features of individual loans:
● Occupation ● Loan amount
● Loan history ● Interest rate
You
Data Analyst
Sean
SeanBarnes
Barnes
Horizontal ↔ Vertical ↕
Recap
Creates box plot: y = "interest_rate"
or
[Link](df, x = "interest_rate" )
Set both to segment by another variable:
[Link](df, x = "grade" , y = "interest_rate" )
Remove some axes from the plot:
[Link](left = True, bottom = True)
Increase the figure size:
[Link](figsize = (8, 6))
Width & height
in inches
Sean
SeanBarnes
Barnes
Data Visualization
Histograms
Scenario
🏆 Goal: Characterizing different features present in the dataset
Features of each person: Features of individual loans:
● Occupation ● Loan amount
● Loan history ● Interest rate
🎯 Task: Visualize using histogram to explain its unique properties
You
Data Analyst ● This visualization will help:
○ Identify and price common loan products
Sean
SeanBarnes
Barnes
Recap
Vertical bars Number of bins
To graph y = "interest_rate" bins = 10
histogram or or
[Link]( df, x = "grade" , binwidth = 50 , kde = True )
Horizontal bars Width of bins Density curve
estimate
sns.set_style("ticks" )
Sean
SeanBarnes
Barnes
Data Visualization
Other charts
Upcoming Plots
For each plot, you’ll see: ⛔ Try not to focus too much on code
Overview of the code ✅ Do keep in mind:
○ Commonalities with other
The output visualizations you’ve created
○ Output you expect from each
visualization
Sean
SeanBarnes
Barnes
Data Visualization
Combining charts
Matplotlib subplots
You can:
● Stack commands from seaborn and
matplotlib to create a chart
● Stack plots on top of each other
● Use technique to:
○ Stack complementary plots
○ Plot multiple distributions
together on the same axes
Sean
SeanBarnes
Barnes
Recap: Subplots
● Create a rugplot on histogram
● Disaggregated plot of individual
values
● Combine a stripplot with a boxplot
● Overlay histograms on the same
chart to compare distributions
● Accomplished by creating two
plots in the same figure
Sean
SeanBarnes
Barnes
Data Visualization
Matplotlib subplots
Scenario
🏆 Goal: Explore number of open credit lines
customers have
matplotlib Seaborn
○ Credit cards
○ Home equity +
○ Business line of credit
🎯 Task: Segmenting paid interest based on Create a grid of Fill up with
loan grade and open lines of credit empty subplots appealing plots
○ A lot of possible values for line of credit
○ Need to create a lot of graphs
Sean
SeanBarnes
Barnes
Recap: Matplotlib subplots
⬜ Create the figure
Canvas Width, Height
[Link](figsize = (5,5) ) 1 2 3
⬜ Use the matplolib .subplot()
Rows Col. Plot 4 5 6
[Link]( 2, 3, 4)
⬜ Create a plot
[Link](filtered_df, hue="grade", y="paid_interest", palette="RdYlGn_r")
Sean
SeanBarnes
Barnes
Data Visualization
Looping with subplots
Recap: Looping with subplots
Use for loop to iterate through subplots [Link](figsize=(15,15))
• Loop through a range: for i in range(1,10):
[Link](3, 3, i)
Starting: 1
filtered_df = df[df["open_credit_lines"] == i]
Ending: 1 after the last number
[Link]( filtered_df,
• Use to help improve code’s hue="grade",
efficiency and readability y="paid_interest",
palette="RdYlGn_r")
[Link](i)
[Link]("[Link]")
Sean
SeanBarnes
Barnes
Data Visualization
Seaborn pairplot
You’re almost done with EDA! 🎉
● Next steps: Create an appendix with many distributional plots
● Why: For clients interested in looking for their own insights
● How: Use Seaborn’s pairplot tool to create many plots quickly
Sean
SeanBarnes
Barnes
Recap: Seaborn pairplot
How to use [Link]() to:
● Create scatterplots between features and histogram for each feature individually
[Link](df[["loan_amount", "annual_income", "interest_rate", "paid_interest"]])
● Add to the title of the entire figure
[Link]("Pairplot of Loan Amount, Annual Income, Interest Rate, and Paid Interest")
Sean
SeanBarnes
Barnes