Interview Q&A — Seaborn Statistical Graphics¶
30 High-Yield Questions & Model Answers for AI/ML and Data Science Technical Interviews.
Q1. Explain the Grammar of Graphics framework underlying Matplotlib and Seaborn.¶
Answer: The Grammar of Graphics decomposes charts into formal semantic components: Data, Aesthetic Mappings (mapping variables to x, y, color, size, shape), Geometric Objects (geom_point, geom_bar), Scales, Coordinate Systems (Cartesian, polar), and Facets (multi-panel small multiples).
Q2. What is the difference between Matplotlib's Object-Oriented (OO) API and 'pyplot' state-machine API?¶
Answer: The 'pyplot' state-machine API ('plt.plot()') implicitly tracks current figure and axes, which is convenient for quick interactive notebooks but fragile in complex scripts. The Object-Oriented API ('fig, ax = plt.subplots()') explicitly references Figure and Axes objects, enabling precise multi-panel layouts, styling, and reusable plotting functions.
Q3. When should you choose a Box Plot vs a Violin Plot vs a KDE Plot?¶
Answer: Box Plot: summarizes five-number summary and flags discrete outliers, but hides multimodal distributions. KDE Plot: visualizes continuous probability density shape and multimodality, but lacks quartile benchmarks. Violin Plot: combines box plot markers inside a mirrored KDE distribution, displaying both summary quartiles and probability density shape.
Q4. Explain the difference between a Bar Plot and a Histogram.¶
Answer: A Bar Plot visualizes categorical variables: bars represent discrete categories, bar widths are arbitrary, and spaces separate bars. A Histogram visualizes continuous numerical distributions: the x-axis is segmented into contiguous, equal-width quantitative bins, and bars touch to reflect continuity.
Q5. What is a Heatmap and how is it used to visualize correlation matrices?¶
Answer: A Heatmap visualizes a 2D matrix where cell values are mapped to a continuous color gradient. In correlation analysis ('sns.heatmap(df.corr(), annot=True, cmap="coolwarm", vmin=-1, vmax=1)'), it immediately highlights collinear feature pairs and feature-target associations.
Q6. What are Small Multiples (Faceting) and why are they effective?¶
Answer: Faceting ('sns.FacetGrid' or 'sns.catplot(col="Category")') splits data across multiple subplots using the same scale and coordinate axes. It allows comparison of complex multivariate relationships across categories without overcrowding a single plot with overlapping visual clutter.
Q7. Explain perceptual color palettes: Diverging, Sequential, and Qualitative.¶
Answer: Sequential: for continuous ordered data progressing from low to high (e.g. 'Blues', 'Viridis'). Diverging: for data with a meaningful critical midpoint (e.g. zero, mean) deviating in two opposing directions (e.g. 'coolwarm', 'RdBu'). Qualitative: for unordered categorical groups where colors must have equal perceptual weight and distinct hues (e.g. 'Set2', 'tab10').
Q8. Why is the 'jet' or 'rainbow' colormap strongly discouraged in scientific visualization?¶
Answer: Rainbow colormaps lack perceptual uniformity: non-linear changes in perceived brightness create artificial visual boundaries and false gradients that do not exist in the data, and they are completely unreadable for colorblind viewers. Use perceptually uniform colormaps like 'viridis', 'plasma', or 'magma'.
Q9. What is the Ink-to-Data Ratio (Edward Tufte's principle)?¶
Answer: Tufte's Data-Ink ratio is the proportion of graphic ink dedicated to displaying non-redundant actual data information: Data-Ink / Total Ink. High-quality charts maximize this ratio by removing 'chartjunk' (excessive 3D effects, heavy gridlines, redundant legends, unnecessary borders).
Q10. How do you resolve visual overplotting in scatter plots with 100,000+ points?¶
Answer: Techniques: (1) reduce point opacity ('alpha=0.1'), (2) decrease marker size, (3) use 2D hexagonal binning ('plt.hexbin()') or 2D KDE contours, (4) use scatter plots with rasterization ('rasterized=True'), or (5) use Datashader for billion-point interactive rendering.
Q11. What is the difference between 'sns.scatterplot()' and 'sns.regplot()'?¶
Answer: 'sns.scatterplot()' plots raw data points with aesthetic encodings (hue, style, size). 'sns.regplot()' plots points, fits a linear regression trendline, and calculates a 95% bootstrap confidence interval band for the regression line.
Q12. Explain a Pairplot ('sns.pairplot') and its utility in preliminary EDA.¶
Answer: A Pairplot creates an N x N matrix of subplots showing pairwise scatter plots for all numerical features, with univariate distributions (histograms or KDEs) on the diagonal. It rapidly exposes non-linear clusters, outliers, and collinearity across all feature pairs.
Q13. What is a Residual Plot and what visual patterns indicate model misspecification?¶
Answer: A residual plot graphs model residuals (y - ŷ) on the y-axis against predicted values ŷ on the x-axis. A healthy linear model shows a random, symmetric cloud of points centered around zero with constant variance. U-shaped patterns indicate non-linear relationships missing from the model; funnel-shaped spreads indicate heteroscedasticity.
Q14. How do you visualize high-dimensional data (e.g. 100+ features) in 2D?¶
Answer: Techniques: (1) t-SNE (t-Distributed Stochastic Neighbor Embedding) preserves local neighborhood structure, (2) UMAP (Uniform Manifold Approximation and Projection) preserves both local and global manifold structure with faster computation, and (3) PCA projects along top linear variance axes.
Q15. What is the difference between 'fig.savefig()' and 'plt.show()' in headless server environments?¶
Answer: 'plt.show()' requires an interactive GUI display backend (like TkAgg, Qt); on headless servers or CI/CD pipelines without X11, it crashes with display errors. 'fig.savefig()' writes image files directly to disk using non-interactive backends (like 'Agg'). Set 'matplotlib.use("Agg")' at script start.
Q16. Explain how to configure secondary y-axes in Matplotlib ('ax.twinx()').¶
Answer: 'ax2 = ax.twinx()' creates a twin Axes sharing the same x-axis but maintaining an independent y-axis scale. Useful for plotting related metrics with different units simultaneously (e.g. advertising spend in USD on left axis vs conversion rate percentage on right axis).
Q17. What is an empirical cumulative distribution function (ECDF) plot and why is it superior to histograms?¶
Answer: An ECDF plot ('sns.ecdfplot(x)') maps every single data point to its cumulative percentile rank without binning. Unlike histograms, ECDF requires no arbitrary bin width choices, shows all data points, and allows reading percentiles and medians directly.
Q18. How does Matplotlib handle layout adjustments with 'tight_layout()' and 'constrained_layout'?¶
Answer: 'plt.tight_layout()' automatically adjusts subplot parameters and margins so that tick labels, axis titles, and subplot borders do not overlap. 'layout="constrained"' in subplots provides dynamic padding optimization during resizing.
Q19. What is a JointGrid ('sns.jointplot')?¶
Answer: A JointGrid combines a central bivariate plot (scatter plot, 2D KDE, or hexbin) with aligned marginal univariate plots (histograms/KDEs) along the top and right margins, displaying both joint relationships and individual distributions simultaneously.
Q20. How do you visualize confusion matrices effectively for multi-class classification?¶
Answer: Use 'ConfusionMatrixDisplay.from_predictions()' or 'sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")' with true labels on rows and predicted labels on columns. Normalize by row ('normalize="true"') to display recall percentages per class, highlighting confusion between specific classes.
Q21. What is an ROC curve and Precision-Recall curve visualization?¶
Answer: ROC curve plots True Positive Rate (Sensitivity) vs False Positive Rate (1 - Specificity) across all classification thresholds, displaying the diagonal random baseline. Precision-Recall curve plots Precision vs Recall across thresholds, showing the horizontal baseline equal to class prevalence (preferred for imbalanced data).
Q22. Explain Andrews Curves and Parallel Coordinates for multivariate visualization.¶
Answer: Parallel Coordinates plots each high-dimensional sample as a connected line across parallel vertical axes representing individual features. Andrews Curves transforms each sample's features into coefficients of a Fourier series, plotting smooth curves where clusters of points form closely grouped wave patterns.
Q23. How do you ensure accessibility in data visualizations for colorblind users?¶
Answer: Use colorblind-safe palettes (e.g. 'cividis', 'colorblind' in Seaborn), combine color with secondary aesthetic channels (e.g. differing line styles '--', ':', or distinct marker shapes 'o', 's', '^'), and directly label lines/bars rather than relying solely on separate color legends.
Q24. What is a Dendrogram and how does it visualize hierarchical clustering?¶
Answer: A Dendrogram is a tree diagram showing the taxonomic sequence of nested clusters in Agglomerative Hierarchical Clustering. The vertical axis represents the linkage distance at which clusters merge; cutting the dendrogram horizontally at a specific height yields discrete cluster assignments.
Q25. Explain the difference between static, interactive, and streaming visualizations.¶
Answer: Static: fixed bitmap/vector files (PNG, SVG via Matplotlib). Interactive: client-side web charts supporting zooming, panning, tooltips, and filtering (Plotly, D3.js). Streaming: real-time live-updating dashboards that consume event streams via WebSockets (Grafana, Dash).
Q26. How do you plot time-series data with missing date gaps without distorting visual continuity?¶
Answer: Reindex the DataFrame against a complete 'pd.date_range()', then plot using explicit date objects on the x-axis so Matplotlib's DateLocator and DateFormatter scale intervals accurately according to calendar elapsed time rather than row index numbers.
Q27. What is a Waterfall Chart and where is it used in business analytics?¶
Answer: A waterfall chart displays how an initial value (e.g. beginning quarterly revenue) increases or decreases through a sequential series of positive and negative adjustments (new sales, discounts, churn) to reach a final ending value.
Q28. How do you annotate specific points or threshold lines in Matplotlib?¶
Answer: Use 'ax.axhline(y_val, color="red", linestyle="--")' for horizontal benchmark lines. Use 'ax.annotate("text", xy=(x, y), xytext=(x_off, y_off), arrowprops=dict(arrowstyle="->"))' to draw callout text boxes with connecting arrows pointing to key data points.
Q29. What is the difference between vector graphics (SVG/PDF) and raster graphics (PNG/JPEG) for reports?¶
Answer: Raster graphics (PNG) store data as a fixed grid of pixels; zooming causes pixelation and blurriness. Vector graphics (SVG, PDF) store graphical shapes as mathematical vectors (lines, curves, polygons), allowing infinite crisp scaling for high-resolution print publications and responsive web displays.
Q30. How do you create customized multi-panel layouts using 'GridSpec' in Matplotlib?¶
Answer: 'matplotlib.gridspec.GridSpec(nrows, ncols)' partitions figure space into a flexible grid. Individual axes can span multiple rows and columns: 'ax1 = fig.add_subplot(gs[0, :])' (full-width top header) and 'ax2 = fig.add_subplot(gs[1, 0])' (bottom left panel).