MatplotLib Assignment– 5

Working with Data

Basic Questions

  1. Using NumPy, generate x = np.arange(0, 50) and y = 2x + 5; plot a simple line chart.
  2. Create a Pandas Series of daily sales (length 10) and plot it using Series.plot(kind=’line’) with a title.
  3. Build a Pandas DataFrame with two columns temp_c, humidity (10 rows of numbers) and plot both as a line chart using DataFrame.plot().
  4. Read a CSV-like structure into a DataFrame (you may construct it in code) with columns day and visitors; set day as index (DatetimeIndex) and plot visitors.
  5. Plot a time series of 30 days where values are random; format the x-axis ticks to show YYYY-MM-DD using DateFormatter.
  6. Plot hourly data for a single day (24 points); use HourLocator for major ticks and rotate labels by 45°.
  7. Plot a large NumPy array (n=50_000) as a line; apply alpha=0.3 to make dense lines readable.
  8. Plot a large scatter of 30,000 points sampled from two NumPy arrays; use s=1 and alpha=0.2 for efficiency and readability.
  9. Downsample a 10,000-point signal to every 50th point and plot both original (faint) and downsampled (bold) on the same axes.
  10. Use Series.plot(kind=’bar’) on a short categorical Series of 5 items from Pandas.
  11. Create a DataFrame with three columns and plot a stacked area chart using DataFrame.plot.area().
  12. Plot a Pandas time series and show monthly ticks only using MonthLocator.
  13. Plot a weekly time series for 12 weeks and format ticks to show “Week 1…Week 12”.
  14. Create a histogram from a NumPy array of 2,000 values; set bins=50 and alpha=0.7.
  15. Plot two synchronized subplots: top shows a time series; bottom shows its 7-day rolling mean (Pandas).
  16. Create a simple FuncAnimation that animates a growing line y = sin(x) as x extends.
  17. Create a FuncAnimation that updates a scatter’s y-values randomly each frame for 50 frames.
  18. Use ArtistAnimation to animate 10 pre-rendered images (e.g., 2D arrays with imshow).
  19. Simulate streaming data by appending one new random point every frame to a line plot for 100 frames.
  20. Simulate a real-time bar chart where one bar’s height changes each frame for 60 frames.

Intermediate Questions

  1. From a Pandas DataFrame with columns open, high, low, close, plot close as a time series and add a 10-period rolling mean overlay.
  2. Plot two time series on the same axes and format ticks with AutoDateLocator and ConciseDateFormatter.
  3. Create a dual-axis chart: DataFrame[‘sales’] (bar) on left y-axis and DataFrame[‘profit’] (line) on right y-axis over monthly dates.
  4. Plot minute-level data for 6 hours (360 points) and show major ticks at the start of each hour with minor ticks every 10 minutes.
  5. Plot a 100k-point noisy sine wave; compare three strategies on separate subplots: (i) direct plot, (ii) downsample to every 100th, (iii) line with rasterized=True.
  6. Use Pandas to resample a 1-minute series to 15-minute means and plot both original (light) and resampled (dark).
  7. Create a DataFrame of 3 columns and plot them with DataFrame.plot(subplots=True, layout=(3,1)) sharing the x-axis.
  8. Plot a heatmap (imshow) of a 100×100 NumPy array and add a colorbar with label “Intensity”.
  9. Plot a timeseries and use custom tick format “Day N” for daily ticks (1…30) via FuncFormatter.
  10. Plot a scatter of 50,000 points; make it readable using alpha, small s, and plt.hexbin as an alternative on a second axes.
  11. Build a FuncAnimation that animates a rolling window (size 50) over a precomputed signal, updating the visible x-limits per frame.
  12. Create an ArtistAnimation sequence from a list of line artists capturing a random walk’s trajectory growth.
  13. Simulate streaming sensor data from three channels; update three lines in one axes using blitting for performance.
  14. Plot a Pandas time series and add vertical span (axvspan) to highlight weekends across two months.
  15. Create a candlestick-like plot using bars/lines from OHLC data without external libraries.
  16. Plot a very sparse timeseries (irregular timestamps) and format the x-axis ticks to avoid overlap with max_n_locator or rotation and padding.
  17. Plot a multi-index DataFrame (e.g., city × metric) by selecting one metric across cities and plotting as grouped bars.
  18. Create a large scatter and use path_effects or edgecolors to maintain point visibility; compare runtime with and without edgecolors.
  19. Build a “play/pause” animation control using FuncAnimation and event callbacks (keyboard or mouse) to start/stop updates.
  20. Stream random points into a deque (fixed length) and animate a live updating line where old points drop off the left.

Advanced Questions

  1. Ingest a month of second-level time series (≈2.5M points). Design a plotting pipeline that: (a) pre-aggregates to 1-min means for overview, (b) loads on-demand a 10-minute window at full resolution; implement both views in a 2-row figure.
  2. Create a multi-panel dashboard:
    • Panel A: raw timeseries (alpha-blended),
    • Panel B: resampled (hourly) with rolling std as error band,
    • Panel C: daily boxplot distribution,
    • Panel D: weekly heatmap of hourly averages (day vs hour).
  3. Implement a performant real-time plot for 4 channels at 30 FPS using blitting: pre-allocate NumPy arrays and update .set_ydata() only; keep GC low.
  4. Build a FuncAnimation that records to MP4 (via FFMPEG writer) a simulation of a moving object (2D position), with a trail (fading alpha) and time-stamped annotation each frame.
  5. Create an ArtistAnimation story: 20 frames alternating between imshow heatmaps and contour overlays, synchronized color limits, saved as GIF and MP4.
  6. Design a robust tick/locator scheme for multi-scale time data (seconds→months): on zoomed-in axes show second/minute ticks; on zoomed-out axes show month ticks—implement with callbacks that replace locators based on current xlim.
  7. Stream live data from a generator that occasionally drops samples; maintain real-time plotting while interpolating missing points and marking gaps with red markers.
  8. For a very large dataset (≥1M points), benchmark three approaches—(i) downsampling (every Nth), (ii) aggregation to bins and plotting bin averages, (iii) LineCollection—and visualize memory/time results in a summary chart.
  9. Build an animated “live candlestick” from streaming OHLC ticks (synthetic). Update the current candle in-place until the interval closes; then append a new candle.
  10. Implement a real-time anomaly dashboard:
    • Subplot 1: streaming signal,
    • Subplot 2: rolling z-score with threshold lines,
    • Subplot 3: scatter of detected anomalies with timestamps;
      Use blitting and minimal redraw to sustain ≥20 FPS; save a short clip.