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.
More Related questions...