Data Science
seaborn applied: faceting, correlation and honest EDA
Separate groups with facets, inspect correlations and write cautious exploratory conclusions.
What you will learn
Separate groups with facets, inspect correlations and write cautious exploratory conclusions.
Before you start
You need basic variables, lists and function calls, not advanced Python algorithms. If these are new, visit Python foundations first.
- Create a local environment:
python -m venv .venv. Activate it for your OS and installpython -m pip install jupyterlab numpy pandas matplotlib seaborn plotly. - Download the 12-row sales CSV into your notebook folder. Keep its filename unchanged.
- Start Jupyter with
jupyter lab. Run the setup below in a fresh notebook before every workshop.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
raw = pd.read_csv('data-science-sales.csv', parse_dates=['date'])
df = raw.copy()
df['revenue'] = df['units'] * df['unit_price']
net = df.loc[df['returned'].eq(0)].copy()
Data dictionary: each row is one order for one product; units is a positive count; unit_price is INR per unit; returned is 0 or 1. Revenue is units × unit_price. For this workshop, net sales exclude the entire returned order (not partial returns). The CSV has 12 rows, 4950 gross revenue, 4450 net revenue and 35 net units. This small fictional sample teaches technique; it does not justify population or causal claims.
Save your notebook and written interpretation. There is no browser code judge and no automatic grading of visual design.
Official seaborn documentation
Learn the idea
relplot returns a FacetGrid, not a single Axes; save with the grid's savefig and close grid.figure. Facets keep scales comparable across groups. Correlation measures linear association, not causality, and including a derived column such as revenue alongside units and price creates a partly mechanical relationship. Only correlate numeric columns you deliberately chose. Missing values and small samples can alter correlations dramatically.
Follow a worked example
corr=net[['units','unit_price','revenue']].corr()
fig,ax=plt.subplots(); sns.heatmap(corr,vmin=-1,vmax=1,center=0,annot=True,cmap='vlag',ax=ax)
ax.set_title('Exploratory correlations — n=11, not causal'); fig.tight_layout(); plt.show()
Expected result
A labelled 3×3 symmetric matrix with diagonal 1. Revenue is derived from units and price; interpretation must mention this.
Skill-development workshop
These are practical assignments, not MCQs or code-judge puzzles. Work in your own notebook or MySQL client. Complete the levels in order; if a task feels difficult, follow the hint and compare with the reference only after making an attempt. Keep your result and a short explanation of what it means.
Easy: Check the correlation matrix dimensions and diagonal.
Success criteria: Shape (3,3), diagonal all 1.
Need a starting point? Show the hint
Choose numeric columns explicitly and use np.diag.
Reference solution — open after trying
corr=net[['units','unit_price','revenue']].corr()
assert corr.shape==(3,3) and np.allclose(np.diag(corr),1)
Medium: Separate units/revenue scatterplots by region.
Success criteria: Three labelled panels, identical units, total 11 observations.
Need a starting point? Show the hint
relplot with col='region' and col_wrap=3.
Reference solution — open after trying
g=sns.relplot(data=net,x='units',y='revenue',col='region',col_wrap=3,height=3)
g.set_axis_labels('Units','Order revenue (INR)'); g.savefig('region-facets.png'); plt.close(g.figure)
Hard: Overlay the observations on category summaries.
Success criteria: All 11 dots remain visible; chart shows small sample sizes, not just boxes.
Need a starting point? Show the hint
Use boxplot then stripplot on the same Axes with jitter=False.
Reference solution — open after trying
fig,ax=plt.subplots(); sns.boxplot(data=net,x='region',y='revenue',ax=ax); sns.stripplot(data=net,x='region',y='revenue',jitter=False,color='black',ax=ax)
ax.set(title='Small sample: inspect individual orders',ylabel='Order revenue (INR)'); plt.show()
Professional: Deliver an exploratory analysis without causal overclaiming.
Success criteria: Include counts, one facet chart, one correlation table and two limitations: n=11 and derived revenue.
Need a starting point? Show the hint
Write a conclusion that distinguishes observation from explanation.
Reference solution — open after trying
print(net.groupby('region').size())
net[['units','unit_price','revenue']].corr().to_csv('eda-correlation.csv')
print('Observation: South contributes the most net revenue in this sample. Limitation: only 11 non-returned orders; revenue is mechanically derived from units and price. No causal conclusion follows.')
Reflection and submission
Save your queries/notebook and outputs. Write three sentences: what you changed, how you checked it, and one limitation. A correct chart or query without a clear interpretation is not a completed professional assignment.
corr=net[['units','unit_price','revenue']].corr()
fig,ax=plt.subplots(); sns.heatmap(corr,vmin=-1,vmax=1,center=0,annot=True,cmap='vlag',ax=ax)
ax.set_title('Exploratory correlations — n=11, not causal'); fig.tight_layout(); plt.show()Lesson Code (python)
corr=net[['units','unit_price','revenue']].corr()
fig,ax=plt.subplots(); sns.heatmap(corr,vmin=-1,vmax=1,center=0,annot=True,cmap='vlag',ax=ax)
ax.set_title('Exploratory correlations — n=11, not causal'); fig.tight_layout(); plt.show()Console Output
A labelled 3×3 symmetric matrix with diagonal 1. Revenue is derived from units and price; interpretation must mention this.Up next · Continue learning
interactive charts and useful hover data
Build interactive bar, scatter and line charts with deliberate hover fields and export them without a hosted account.