Matplotlib/Seaborn - Interview Questions and Answers
Matplotlib is a popular Python library for creating static, animated, and interactive visualizations. It provides an interface similar to MATLAB and is useful for plotting various graphs, such as line charts, bar charts, histograms, scatter plots, and more.
You can install Matplotlib using pip:
pip install matplotlib
A Matplotlib figure consists of:
- Figure: The overall container holding the plot.
- Axes: The plotting area (can be multiple in a figure).
- Axis: The x and y coordinate system.
- Ticks & Labels: Markings along the axes.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
plt.plot(x, y)
plt.show()
pyplot
is a module that provides a simple interface for creating figures. figure
is used to create a blank canvas for plotting multiple subplots.
plt.figure(figsize=(10, 5)) # Width=10, Height=5
fig, ax = plt.subplots(2, 2) # 2x2 grid of plots
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.title("Plot Title")
plt.show()
displays the plot on the screen, while plt.savefig("plot.png")
saves it as an image file.
plt.style.use('ggplot') # Apply the ggplot style
plt.bar(x, y, color='blue')
plt.plot(x, y, label="Line 1")
plt.plot(x, [y_i + 5 for y_i in y], label="Line 2")
plt.legend()
A scatter plot shows individual points, while a line plot connects them.
plt.hist(data, bins=10, color='green')
plt.plot(x, y, linestyle='--', marker='o', color='red')
plt.xticks([0, 2, 4, 6])
plt.yticks([10, 20, 30])
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
import pandas as pd
data = pd.date_range(start="2023-01-01", periods=10, freq="D")
plt.plot(data, y)
plt.savefig("plot.png", dpi=300)
- Use
scatter()
instead ofplot()
for large datasets. - Reduce transparency with
alpha=0.5
. - Optimize with
downsampling
techniques.
Seaborn is a higher-level visualization library built on Matplotlib that provides better defaults and advanced statistical plotting.
import seaborn as sns
sns.scatterplot(x="column1", y="column2", data=df)
It groups data points by color.
sns.scatterplot(x="x", y="y", hue="category", data=df)
sns.violinplot(x="category", y="value", data=df)
sns.heatmap(data.corr(), annot=True, cmap="coolwarm")
sns.regplot(x="x", y="y", data=df)
g = sns.FacetGrid(df, col="category")
g.map(sns.histplot, "value")
sns.despine()
Use sample()
, agg()
for aggregation, or FacetGrid
for breaking data into smaller plots.
- ImportError: Ensure libraries are installed.
- ValueError: Check data types before plotting.
import numpy as np
x = np.arange(3)
values1 = [5, 7, 9]
values2 = [6, 8, 10]
plt.bar(x - 0.2, values1, width=0.4, label='Group 1')
plt.bar(x + 0.2, values2, width=0.4, label='Group 2')
plt.xticks(x, ['A', 'B', 'C'])
plt.legend()
plt.show()
plt.text(2, 15, "Annotation Here", fontsize=12, color='red')
alpha
controls the transparency of a plot.
plt.scatter(x, y, alpha=0.5)
plt.grid(True, linestyle='--', linewidth=0.5)
plt.bar(x, values1, label="Category 1")
plt.bar(x, values2, bottom=values1, label="Category 2")
plt.legend()
plt.xscale("log")
plt.yscale("log")
plt.legend(loc='upper left', bbox_to_anchor=(1,1))
labels = ['A', 'B', 'C']
sizes = [40, 35, 25]
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title("Title", fontsize=14, fontweight='bold')
Use bar()
with cumulative sums and a baseline shift.
sns.boxplot(x="category", y="value", data=df)
- It sets the color scheme for the plots.
sns.scatterplot(x="x", y="y", data=df, palette="coolwarm")
sns.kdeplot(df["column"], shade=True)
sns.pairplot(df)
stripplot()
places points randomly along the x-axis.swarmplot()
avoids overlapping points.
sns.swarmplot(x="category", y="value", data=df)
plt.figure(figsize=(10, 5))
sns.heatmap(df.corr(), annot=True, cmap="Blues")
A FacetGrid
allows multiple plots to be displayed side by side for different subsets of the data.
sns.jointplot(x="x", y="y", data=df, kind="scatter")
fig, ax = plt.subplots(2, 2)
plt.scatter(x, y, c=y, cmap="viridis")
Seaborn provides better aesthetics and statistical plots.
%matplotlib notebook
sns.set_theme(style="darkgrid")
sns.set_theme()
provides better control over aesthetics.
for index, value in enumerate(y):
plt.text(index, value, str(value))
sns.heatmap(df.corr(), annot=True)
plt.violinplot(data)
- Large datasets slow down rendering.
- Incompatible versions cause unexpected behavior.
- Matplotlib styles can sometimes override Seaborn themes.
Tutorials
Random Blogs
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- What is YII? and How to Install it?
- The Ultimate Guide to Data Science: Everything You Need to Know
- Exploratory Data Analysis On Iris Dataset
- Python Challenging Programming Exercises Part 1
- Quantum AI – The Future of AI Powered by Quantum Computing
- Important Mistakes to Avoid While Advertising on Facebook
- Python Challenging Programming Exercises Part 2
- The Ultimate Guide to Machine Learning (ML) for Beginners