MatplotLib Assignment– 6

Subplots, Grids & Layouts

Basic Questions

  1. Create a 1×2 subplot figure (plt.subplots(1,2)) and plot a line on the left, a scatter on the right.
  2. Create a 2×1 subplot figure and plot sine (top) and cosine (bottom) with titles on each axes.
  3. Make a 2×2 grid and fill only the top row; leave the bottom row empty. Use tight_layout() to avoid overlap.
  4. Use subplot2grid to create a 2×3 layout where one plot spans the entire first row.
  5. Create a 2×2 grid; set a common super-title with fig.suptitle(“My Subplots”).
  6. Create a 2×2 grid and share the x-axis across all subplots (sharex=True).
  7. Create a 2×2 grid and share the y-axis across all subplots (sharey=True).
  8. Plot three subplots in one row with different aspect ratios for each axes.
  9. Build a figure with constrained_layout=True and compare spacing with tight_layout() in a second figure.
  10. Create a 2×2 grid and add a single common x-label and y-label for the entire figure.
  11. Make a vertical stack of three subplots and reduce the spacing using fig.subplots_adjust(hspace=0.2).
  12. Create a 2×2 grid and turn off the top-right subplot (empty axes).
  13. Create a 2×1 layout; on the top plot show gridlines, on the bottom hide gridlines.
  14. Add an inset axes inside a main axes (using inset_axes or fig.add_axes) showing a zoomed region.
  15. Plot a line chart and add a secondary y-axis with twinx() for a second series.
  16. Plot a bar chart and add a secondary x-axis with twiny() showing custom tick labels.
  17. Create a 2×2 layout where only the bottom-left subplot has a legend; hide legends in others.
  18. Create a figure with exact size 12×6 inches, three subplots, and equal y-limits across all.
  19. Create a 2×2 grid and enable major ticks only on the left column and bottom row subplots.
  20. Save a multi-subplot figure as PNG and PDF with a single call to savefig() each.

Intermediate Questions

  1. 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).
  2. Build a GridSpec where the left column has two stacked plots and the right column has one tall plot spanning both rows.
  3. Create a 3×3 GridSpec; fill the center cell with a plot that shares x with the center column plots.
  4. Combine line (left) and bar (right) in a 1×2 layout with shared y-axis and separate legends.
  5. Create a 2×3 grid; hide the spines for the corner subplots only.
  6. Make a figure with four subplots that share x; synchronize x-limits via set_xlim from one controller axes.
  7. Create a twinx() plot with line on left axis and scatter on right axis; format tick colors to match series colors.
  8. Create a twiny() plot showing the same data scaled to two x-units (e.g., seconds vs minutes) with aligned ticks.
  9. Build a layout using subplot_mosaic (or dict-like layout) with areas labeled A, B, C; place a legend only in region C.
  10. Create a 2×2 grid; add a colorbar shared by the two right subplots using fig.colorbar.
  11. Create a figure with three subplots; the middle subplot must have an inset zoom with a rectangle marking the zoom region.
  12. Construct a mixed-type figure: (a) area chart, (b) stem plot, (c) errorbar plot in a 1×3 layout with a common title.
  13. Create a 2×2 GridSpec layout and align y=0 baseline across all axes using horizontal lines and identical y-limits.
  14. Build a small-multiples figure of 6 line charts (2×3) using a loop and a shared legend placed below all axes.
  15. Create a GridSpecFromSubplotSpec (nested grids) to place two tiny diagnostic plots beneath a main plot.
  16. Implement a layout where top-left and top-right share x; bottom-left and bottom-right share y; demonstrate independent scaling.
  17. Create a twin-axes chart with stacked bars on the primary axis and a line with markers on the secondary axis; ensure readable ticks.
  18. Combine imshow heatmap (left) and matching contour (right) with a single shared colorbar and consistent vmin/vmax.
  19. Build a 2×2 figure and connect event callbacks so clicking in any subplot prints which subplot was clicked (by row/col).
  20. Create a 2×2 grid; annotate each subplot with its Axes.get_position() (normalized coordinates) rendered as text.

Advanced Questions

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Responsive Layout: Write code that switches between tight_layout() and constrained_layout depending on figure size. Demonstrate with two saved outputs.

  8. Shared Color + Legend: Create four subplots each using the same colormap/category colors. Implement a single master legend/colorbar outside the grid.

  9. 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.

  10. 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.