Python / Data Science Essentials Interview Questions
What are the most common chart types in Matplotlib and when do you use each?
Choosing the right chart type communicates data clearly; choosing the wrong one obscures it. Here are the workhorses of exploratory data analysis:
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(2, 3, figsize=(14, 8))
# 1. Line chart — trends over time or ordered x-axis
ax = axes[0, 0]
ax.plot([1, 2, 3, 4], [10, 15, 13, 18])
ax.set_title('Line: trends')
# 2. Bar chart — comparing discrete categories
ax = axes[0, 1]
ax.bar(['A', 'B', 'C'], [30, 45, 20])
ax.set_title('Bar: categories')
# 3. Scatter plot — relationship between two continuous variables
ax = axes[0, 2]
x = np.random.randn(100); y = x * 0.8 + np.random.randn(100) * 0.5
ax.scatter(x, y, alpha=0.5, c='steelblue')
ax.set_title('Scatter: correlation')
# 4. Histogram — distribution of one continuous variable
ax = axes[1, 0]
ax.hist(np.random.randn(1000), bins=30, color='salmon', edgecolor='white')
ax.set_title('Histogram: distribution')
# 5. Box plot — distribution summary with outliers
ax = axes[1, 1]
ax.boxplot([np.random.randn(100) for _ in range(3)], labels=['G1','G2','G3'])
ax.set_title('Box: spread & outliers')
# 6. Heatmap via imshow — 2-D matrix data (e.g., correlation matrix)
ax = axes[1, 2]
data = np.random.rand(4, 4)
im = ax.imshow(data, cmap='viridis')
plt.colorbar(im, ax=ax)
ax.set_title('Heatmap: 2-D matrix')
fig.tight_layout()
plt.show()Rule of thumb: line for temporal/ordered data, bar for nominal comparisons, scatter for two-variable relationships, histogram for single-variable distributions, box for group comparisons with outlier context, heatmap for correlation matrices and confusion matrices.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
