MatplotLib Assignment– 6
Subplots, Grids & Layouts
Basic Questions
- Create a 1×2 subplot figure (plt.subplots(1,2)) and plot a line on the left, a scatter on the right.
- Create a 2×1 subplot figure and plot sine (top) and cosine (bottom) with titles on each axes.
- Make a 2×2 grid and fill only the top row; leave the bottom row empty. Use tight_layout() to avoid overlap.
- Use subplot2grid to create a 2×3 layout where one plot spans the entire first row.
- Create a 2×2 grid; set a common super-title with fig.suptitle(“My Subplots”).
- Create a 2×2 grid and share the x-axis across all subplots (sharex=True).
- Create a 2×2 grid and share the y-axis across all subplots (sharey=True).
- Plot three subplots in one row with different aspect ratios for each axes.
- Build a figure with constrained_layout=True and compare spacing with tight_layout() in a second figure.
- Create a 2×2 grid and add a single common x-label and y-label for the entire figure.
- Make a vertical stack of three subplots and reduce the spacing using fig.subplots_adjust(hspace=0.2).
- Create a 2×2 grid and turn off the top-right subplot (empty axes).
- Create a 2×1 layout; on the top plot show gridlines, on the bottom hide gridlines.
- Add an inset axes inside a main axes (using inset_axes or fig.add_axes) showing a zoomed region.
- Plot a line chart and add a secondary y-axis with twinx() for a second series.
- Plot a bar chart and add a secondary x-axis with twiny() showing custom tick labels.
- Create a 2×2 layout where only the bottom-left subplot has a legend; hide legends in others.
- Create a figure with exact size 12×6 inches, three subplots, and equal y-limits across all.
- Create a 2×2 grid and enable major ticks only on the left column and bottom row subplots.
- Save a multi-subplot figure as PNG and PDF with a single call to savefig() each.
Intermediate Questions
- Use GridSpec to create a layout with one wide plot on top and two smaller plots below (2×2 arrangement with top spanning both columns).
- Build a GridSpec where the left column has two stacked plots and the right column has one tall plot spanning both rows.
- Create a 3×3 GridSpec; fill the center cell with a plot that shares x with the center column plots.
- Combine line (left) and bar (right) in a 1×2 layout with shared y-axis and separate legends.
- Create a 2×3 grid; hide the spines for the corner subplots only.
- Make a figure with four subplots that share x; synchronize x-limits via set_xlim from one controller axes.
- Create a twinx() plot with line on left axis and scatter on right axis; format tick colors to match series colors.
- Create a twiny() plot showing the same data scaled to two x-units (e.g., seconds vs minutes) with aligned ticks.
- Build a layout using subplot_mosaic (or dict-like layout) with areas labeled A, B, C; place a legend only in region C.
- Create a 2×2 grid; add a colorbar shared by the two right subplots using fig.colorbar.
- Create a figure with three subplots; the middle subplot must have an inset zoom with a rectangle marking the zoom region.
- Construct a mixed-type figure: (a) area chart, (b) stem plot, (c) errorbar plot in a 1×3 layout with a common title.
- Create a 2×2 GridSpec layout and align y=0 baseline across all axes using horizontal lines and identical y-limits.
- Build a small-multiples figure of 6 line charts (2×3) using a loop and a shared legend placed below all axes.
- Create a GridSpecFromSubplotSpec (nested grids) to place two tiny diagnostic plots beneath a main plot.
- Implement a layout where top-left and top-right share x; bottom-left and bottom-right share y; demonstrate independent scaling.
- Create a twin-axes chart with stacked bars on the primary axis and a line with markers on the secondary axis; ensure readable ticks.
- Combine imshow heatmap (left) and matching contour (right) with a single shared colorbar and consistent vmin/vmax.
- Build a 2×2 figure and connect event callbacks so clicking in any subplot prints which subplot was clicked (by row/col).
- Create a 2×2 grid; annotate each subplot with its Axes.get_position() (normalized coordinates) rendered as text.
Advanced Questions
Dashboard Case Study: Create a 2×2 analytics dashboard—
A: time series with rolling mean
B: bar chart with error bars
C: scatter with regression line
D: distribution (hist + KDE)
Use a shared color palette, common suptitle, and consistent tick formatting.
Dashboard Case Study: Build a KPI board using GridSpec—
Top (full width): area chart with shaded confidence band
Bottom-left: heatmap (weekly activity)
Bottom-right: box/violin comparison
Add a shared colorbar for the heatmap and a legend panel.
Complex GridSpec: Create a 3×4 grid where the central 2×2 block is a large plot. Surround it with four small diagnostic plots and share the x-axis with the top row diagnostics.
Twin Axes Suite: One figure with three subplots—
(i) twinx line + bar
(ii) twiny with two x-scales
(iii) both twinx and twiny combined
Ensure clear labeling and legends.
Mixed Types: Compose a figure with—
Top: stacked area + line overlay
Bottom-left: stem + errorbar
Bottom-right: bar + scatter overlay
Align y=0 across all plots.
Mosaic Dashboard: Use
subplot_mosaic
to place—Header plot
Left column diagnostics (2 small plots)
Right column main plot
Add global annotations (arrows/labels) pointing to key regions.
Responsive Layout: Write code that switches between
tight_layout()
andconstrained_layout
depending on figure size. Demonstrate with two saved outputs.Shared Color + Legend: Create four subplots each using the same colormap/category colors. Implement a single master legend/colorbar outside the grid.
Inset & Zoom Case: Main axes shows a crowded time series. Add two inset zooms at different regions, linked by rectangles. Ensure insets use a shared y-axis with the main plot.
Publication-Ready Dashboard: Produce a 16:9, 300 DPI multi-axes figure with global typography, consistent grids, aligned tick locators, panel labels (A–D), a footer note, and export as PNG and PDF.