π Matplotlib & Seaborn
Both Matplotlib and Seaborn are Python libraries used for data visualization β creating charts and graphs from data.
π Matplotlib
Matplotlib is a general-purpose plotting library that gives you detailed control over your charts.
π¦ Install
pip install matplotlibImport it:
import matplotlib.pyplot as pltπ Basic Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 15, 25]
plt.plot(x, y)
plt.xlabel("Days")
plt.ylabel("Sales")
plt.title("Sales Trend")
plt.show() #It creates a line chart.Common Charts
plt.plot() # Line chart
plt.bar() # Bar chart
plt.scatter() # Scatter plot
plt.hist() # Histogram
plt.pie() # Pie chart
plt.boxplot() # Box plot
plt.fill_between() # Area chart
plt.stackplot() # Stacked area chartπ¨ Seaborn
Seaborn is built on top of Matplotlib and makes it easier to create attractive and statistical visualizations.
π¦ Install
pip install seabornImport it:
import seaborn as snsπ Example
import seaborn as sns
import matplotlib.pyplot as plt
data = [10, 20, 20, 30, 40, 40, 50]
sns.histplot(data)
plt.show()Seaborn is particularly useful for statistical plots and working with Pandas DataFrames.
Common Charts
sns.lineplot() # Line chart
sns.barplot() # Bar chart
sns.countplot() # Count chart
sns.scatterplot() # Scatter plot
sns.histplot() # Histogram
sns.boxplot() # Box plot
sns.violinplot() # Violin plot
sns.heatmap() # Heatmap
sns.pairplot() # Pair plot
sns.regplot() # Regression plotβοΈ Matplotlib vs Seaborn
Matplotlib π | Seaborn π¨ |
|---|---|
General-purpose visualization | Statistical visualization |
More control over individual elements | Easier to create complex plots |
More code for some plots | Often requires less code |
Works independently | Built on Matplotlib |
|
|
<div class="infographic-pyplotting">
<style>
.infographic-pyplotting {
--ink:#1a1a1a; --ink-soft:#5c5c5c; --paper:#fff; --paper-alt:#f0ece4;
--accent:#2563eb; --accent-soft:#e3ebfd; --line:#ddd6c9;
--sns1:#4c72b0; --sns2:#dd8452; --sns3:#55a868;
--mono-font:'IBM Plex Mono','SFMono-Regular',Menlo,monospace;
--display-font:'Fraunces',Georgia,serif; --body-font:'Inter',system-ui,sans-serif;
box-sizing:border-box; max-width:700px; margin:0 auto; font-family:var(--body-font); color:var(--ink); background:var(--paper);
}
.infographic-pyplotting[data-theme="dark"] {
--ink:#e8e6e1; --ink-soft:#9a968c; --paper:#121212; --paper-alt:#1e1e26;
--accent:#5b8def; --accent-soft:#202942; --line:#2c313d;
}
.infographic-pyplotting *,.infographic-pyplotting *::before,.infographic-pyplotting *::after{box-sizing:border-box;}
.infographic-pyplotting .pl-wrap{padding:0 clamp(1rem,4vw,2rem) 1.5rem;}
.infographic-pyplotting h1{font-family:var(--display-font);font-weight:600;font-size:clamp(1.4rem,5vw,1.9rem);text-align:center;margin:1.5rem 0 0.25rem;letter-spacing:-0.01em;}
.infographic-pyplotting .pl-sub{text-align:center;color:var(--ink-soft);font-size:0.85rem;margin:0 0 1.3rem;}
.infographic-pyplotting .pl-tabs{display:flex;justify-content:center;gap:0.35rem;margin-bottom:1.4rem;border-bottom:1px solid var(--line);}
.infographic-pyplotting .pl-tab-btn{font:inherit;font-family:var(--mono-font);font-size:0.82rem;font-weight:700;background:none;border:none;border-bottom:2.5px solid transparent;color:var(--ink-soft);padding:0.55rem 0.85rem;cursor:pointer;margin-bottom:-1px;}
.infographic-pyplotting .pl-tab-btn.is-active{color:var(--accent);border-bottom-color:var(--accent);}
.infographic-pyplotting .pl-panel.is-hidden-tab{display:none;}
.infographic-pyplotting .code-line{text-align:center;font-family:var(--mono-font);font-size:0.78rem;color:var(--ink-soft);margin-bottom:1rem;}
.infographic-pyplotting .code-line strong{color:var(--accent);}
.infographic-pyplotting .type-row{display:flex;justify-content:center;gap:0.4rem;margin-bottom:1rem;flex-wrap:wrap;}
.infographic-pyplotting .type-btn{font:inherit;font-family:var(--mono-font);font-size:0.8rem;font-weight:700;background:var(--paper-alt);border:1.5px solid var(--line);color:var(--ink-soft);padding:0.42rem 0.8rem;cursor:pointer;border-radius:6px;}
.infographic-pyplotting .type-btn.is-selected{border-color:var(--accent);color:var(--accent);background:var(--accent-soft);}
.infographic-pyplotting .chart-stage{display:flex;justify-content:center;margin-bottom:1rem;background:var(--paper-alt);border:1px solid var(--line);border-radius:8px;padding:0.8rem;}
.infographic-pyplotting .chart-stage svg{width:100%; max-width:560px; height:auto;}
.infographic-pyplotting .bar{transition:height .3s,y .3s;}
.infographic-pyplotting .pl-caption{text-align:center;font-size:0.85rem;color:var(--ink-soft);}
@media (prefers-reduced-motion:reduce){.infographic-pyplotting *,.infographic-pyplotting *::before,.infographic-pyplotting *::after{animation-duration:.001ms!important;transition-duration:.001ms!important;}}
</style>
<div class="pl-wrap">
<h1>Matplotlib & Seaborn</h1>
<p class="pl-sub">Same data. Matplotlib draws it plainly β Seaborn draws it with styling and stats built in.</p>
<div class="pl-tabs">
<button class="pl-tab-btn is-active" data-tab="mpl">Matplotlib</button>
<button class="pl-tab-btn" data-tab="sns">Seaborn</button>
</div>
<!-- MATPLOTLIB -->
<div class="pl-panel" id="tabMpl">
<div class="code-line">import matplotlib.pyplot as plt<br><strong id="mplCode">plt.plot(x, y)</strong>; plt.show()</div>
<div class="type-row">
<button class="type-btn is-selected" data-t="line">plt.plot()</button>
<button class="type-btn" data-t="bar">plt.bar()</button>
<button class="type-btn" data-t="scatter">plt.scatter()</button>
</div>
<div class="chart-stage"><svg id="mplChart" viewBox="0 0 400 220"></svg></div>
<p class="pl-caption">Matplotlib gives you exact control β but the plain style is what you get unless you set it yourself.</p>
</div>
<!-- SEABORN -->
<div class="pl-panel is-hidden-tab" id="tabSns">
<div class="code-line">import seaborn as sns<br><strong id="snsCode">sns.lineplot(x=x, y=y)</strong></div>
<div class="type-row">
<button class="type-btn is-selected" data-t="line">sns.lineplot()</button>
<button class="type-btn" data-t="bar">sns.barplot()</button>
<button class="type-btn" data-t="scatter">sns.scatterplot()</button>
</div>
<div class="chart-stage"><svg id="snsChart" viewBox="0 0 400 220"></svg></div>
<p class="pl-caption">Seaborn is built on top of Matplotlib β same engine, nicer defaults (grid, color palette, spacing) out of the box.</p>
</div>
</div>
<script>
(function(){
var root = document.currentScript.closest('.infographic-pyplotting') || document.querySelector('.infographic-pyplotting');
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark = bodyAttr === 'true' ? true : bodyAttr === 'false' ? false : (document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/) || [])[1] === 'true';
if (dark) root.setAttribute('data-theme', 'dark');
var tabBtns = root.querySelectorAll('.pl-tab-btn');
var panels = { mpl: root.querySelector('#tabMpl'), sns: root.querySelector('#tabSns') };
tabBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
tabBtns.forEach(function (b) { b.classList.toggle('is-active', b === btn); });
var t = btn.dataset.tab;
Object.keys(panels).forEach(function (k) { panels[k].classList.toggle('is-hidden-tab', k !== t); });
});
});
var values = [3, 7, 4, 9, 5, 8];
var W = 400, H = 220, PAD = 30;
var maxV = 10;
function xAt(i) { return PAD + i * ((W - 2 * PAD) / (values.length - 1)); }
function yAt(v) { return H - PAD - (v / maxV) * (H - 2 * PAD); }
function svgNS(tag) { return document.createElementNS('http://www.w3.org/2000/svg', tag); }
function drawAxes(svg, styled) {
var axisColor = styled ? '#c9c9c9' : (dark ? '#555' : '#999');
if (styled) {
for (var g = 0; g <= maxV; g += 2) {
var gy = yAt(g);
var gl = svgNS('line');
gl.setAttribute('x1', PAD); gl.setAttribute('x2', W - PAD);
gl.setAttribute('y1', gy); gl.setAttribute('y2', gy);
gl.setAttribute('stroke', axisColor); gl.setAttribute('stroke-width', '1');
svg.appendChild(gl);
}
var bgRect = svgNS('rect');
bgRect.setAttribute('x', PAD); bgRect.setAttribute('y', PAD);
bgRect.setAttribute('width', W - 2*PAD); bgRect.setAttribute('height', H - 2*PAD);
bgRect.setAttribute('fill', dark ? '#1a2233' : '#eaeaf2');
svg.insertBefore(bgRect, svg.firstChild);
} else {
var xline = svgNS('line');
xline.setAttribute('x1', PAD); xline.setAttribute('x2', W - PAD);
xline.setAttribute('y1', H - PAD); xline.setAttribute('y2', H - PAD);
xline.setAttribute('stroke', axisColor); xline.setAttribute('stroke-width', '1.5');
svg.appendChild(xline);
var yline = svgNS('line');
yline.setAttribute('x1', PAD); yline.setAttribute('x2', PAD);
yline.setAttribute('y1', PAD); yline.setAttribute('y2', H - PAD);
yline.setAttribute('stroke', axisColor); yline.setAttribute('stroke-width', '1.5');
svg.appendChild(yline);
}
}
function renderChart(svg, type, styled) {
svg.innerHTML = '';
drawAxes(svg, styled);
var color = styled ? getComputedStyle(root).getPropertyValue('--sns1').trim() : (dark ? '#5b8def' : '#1f4fd1');
if (type === 'line') {
var pts = values.map(function (v, i) { return xAt(i) + ',' + yAt(v); }).join(' ');
var poly = svgNS('polyline');
poly.setAttribute('points', pts);
poly.setAttribute('fill', 'none');
poly.setAttribute('stroke', color);
poly.setAttribute('stroke-width', styled ? '2.5' : '1.75');
svg.appendChild(poly);
values.forEach(function (v, i) {
var c = svgNS('circle');
c.setAttribute('cx', xAt(i)); c.setAttribute('cy', yAt(v)); c.setAttribute('r', styled ? 4 : 2.5);
c.setAttribute('fill', color);
svg.appendChild(c);
});
} else if (type === 'bar') {
var bw = (W - 2 * PAD) / values.length * 0.6;
values.forEach(function (v, i) {
var bx = xAt(i) - bw / 2;
var by = yAt(v);
var r = svgNS('rect');
r.setAttribute('x', bx); r.setAttribute('y', by);
r.setAttribute('width', bw); r.setAttribute('height', H - PAD - by);
r.setAttribute('fill', styled ? [getComputedStyle(root).getPropertyValue('--sns1').trim(), getComputedStyle(root).getPropertyValue('--sns2').trim(), getComputedStyle(root).getPropertyValue('--sns3').trim()][i % 3] : color);
r.setAttribute('rx', styled ? 2 : 0);
svg.appendChild(r);
});
} else if (type === 'scatter') {
values.forEach(function (v, i) {
var c = svgNS('circle');
c.setAttribute('cx', xAt(i)); c.setAttribute('cy', yAt(v)); c.setAttribute('r', styled ? 6 : 4);
c.setAttribute('fill', styled ? 'none' : color);
c.setAttribute('stroke', color);
c.setAttribute('stroke-width', 2);
c.setAttribute('fill-opacity', styled ? 0.6 : 1);
if (styled) c.setAttribute('fill', color);
svg.appendChild(c);
});
}
}
var mplChart = root.querySelector('#mplChart');
var snsChart = root.querySelector('#snsChart');
var mplCode = root.querySelector('#mplCode');
var snsCode = root.querySelector('#snsCode');
var mplCalls = { line: 'plt.plot(x, y)', bar: 'plt.bar(x, y)', scatter: 'plt.scatter(x, y)' };
var snsCalls = { line: 'sns.lineplot(x=x, y=y)', bar: 'sns.barplot(x=x, y=y)', scatter: 'sns.scatterplot(x=x, y=y)' };
root.querySelectorAll('#tabMpl .type-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
root.querySelectorAll('#tabMpl .type-btn').forEach(function (b) { b.classList.remove('is-selected'); });
btn.classList.add('is-selected');
mplCode.textContent = mplCalls[btn.dataset.t];
renderChart(mplChart, btn.dataset.t, false);
});
});
root.querySelectorAll('#tabSns .type-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
root.querySelectorAll('#tabSns .type-btn').forEach(function (b) { b.classList.remove('is-selected'); });
btn.classList.add('is-selected');
snsCode.textContent = snsCalls[btn.dataset.t];
renderChart(snsChart, btn.dataset.t, true);
});
});
renderChart(mplChart, 'line', false);
renderChart(snsChart, 'line', true);
})();
</script>
</div>