Matplotlib Assignment– 2

Figure & Axes Mastery

Basic Questions

  1. Create an empty figure using plt.figure() and display it.
  2. Create a figure with size 8×6 inches using plt.figure(figsize=(8,6)).
  3. Create a figure with DPI set to 150 and plot a line inside it.
  4. Use plt.subplots() to create one figure with a single axes and plot y=x.
  5. Create a figure with two subplots (1 row, 2 columns) showing y=x and y=x².
  6. Create a figure with two subplots (2 rows, 1 column) showing sine and cosine curves.
  7. Plot a line chart in a figure where the background color of the figure is light gray.
  8. Create a figure with 3 subplots using add_subplot() method.
  9. Create a figure and plot 4 subplots in a 2×2 grid using plt.subplots().
  10. Create a line chart and set the figure aspect ratio to be square (1:1).
  11. Create a line chart and change its aspect ratio to 16:9.
  12. Use tight_layout() to avoid overlapping labels in a 2×2 subplot figure.
  13. Use constrained_layout=True in plt.subplots() and compare with tight_layout().
  14. Plot two lines on two subplots and adjust the spacing with tight_layout().
  15. Create a figure with customized title “Figure & Axes Demo” using fig.suptitle().
  16. Create a figure with subplot titles for each axes using set_title().
  17. Plot a line chart and save the figure as output.png using savefig().
  18. Save the same figure as output.jpg with DPI=300.
  19. Save the same figure as a PDF file.
  20. Save a scatter plot as a transparent PNG file.

Intermediate Questions

  1. Create a figure with 2×2 grid using add_subplot() and plot different functions in each.
  2. Create a figure with 1 row and 3 columns of subplots using plt.subplots().
  3. Create a subplot grid of 3×3 but only fill 5 plots with different graphs.
  4. Use GridSpec to create a figure with one large plot on top and two small plots below.
  5. Create a GridSpec layout where one plot spans two columns and one smaller plot is below it.
  6. Create a subplot layout with unequal subplot sizes using GridSpec.
  7. Create a figure with DPI=200 and plot sine and cosine functions with labels.
  8. Create a wide figure (12×4 inches) showing 3 line plots side by side.
  9. Create a tall figure (6×12 inches) with 2 subplots stacked vertically.
  10. Plot 4 subplots and adjust spacing manually using fig.subplots_adjust().
  11. Create 4 subplots and remove space between them using wspace=0 and hspace=0.
  12. Create a multi-axes figure and set different aspect ratios for each subplot.
  13. Create a 2×2 subplot figure and demonstrate the difference between tight_layout() and no layout adjustment.
  14. Create a 2×2 subplot figure and demonstrate the difference between tight_layout() and constrained_layout.
  15. Create a figure with 6 subplots and adjust the layout using fig.tight_layout(pad=2).
  16. Save a figure as a PNG with resolution 200 DPI and compare with 50 DPI visually.
  17. Save a figure as a JPEG with quality set to 95%.
  18. Save a figure in both PNG and PDF formats for publication.
  19. Save a multi-subplot figure as an SVG format and open it in a browser.
  20. Save a figure with transparent background and overlay it on another image.

Advanced Questions

  1. Create a 3×3 subplot layout using GridSpec where the center plot spans 2 rows and 2 columns.
  2. Create a dashboard-like figure: one wide plot on top and three smaller plots below using GridSpec.
  3. Create a figure where one subplot shows a histogram, another a scatter, another a bar chart, and arrange them using custom GridSpec.
  4. Create a professional-quality figure with aspect ratio 16:9, DPI=300, labeled axes, grid, and legend, and save it as both PDF and PNG.
  5. Create a figure with constrained_layout enabled and annotate each subplot with its index (e.g., “Subplot 1”).
  6. Create a figure with 6 subplots and add a common x-axis label and common y-axis label.
  7. Create a complex GridSpec figure where some subplots are merged to show larger visualizations.
  8. Create a figure showing multiple sine waves on different subplots and save it in four different formats (PNG, JPG, PDF, SVG).
  9. Create a publication-ready figure with exact figure size (10×6 inches), resolution (DPI=300), and font sizes optimized for research papers.
  10. Create a professional visualization where figure size, aspect ratio, layout, and resolution are all customized, and save it as a transparent PNG for presentation slides.