MatplotLib Assignment– 5
Working with Data
Basic Questions
- Using NumPy, generate x = np.arange(0, 50) and y = 2x + 5; plot a simple line chart.
- Create a Pandas Series of daily sales (length 10) and plot it using Series.plot(kind=’line’) with a title.
- Build a Pandas DataFrame with two columns temp_c, humidity (10 rows of numbers) and plot both as a line chart using DataFrame.plot().
- 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.
- Plot a time series of 30 days where values are random; format the x-axis ticks to show YYYY-MM-DD using DateFormatter.
- Plot hourly data for a single day (24 points); use HourLocator for major ticks and rotate labels by 45°.
- Plot a large NumPy array (n=50_000) as a line; apply alpha=0.3 to make dense lines readable.
- Plot a large scatter of 30,000 points sampled from two NumPy arrays; use s=1 and alpha=0.2 for efficiency and readability.
- Downsample a 10,000-point signal to every 50th point and plot both original (faint) and downsampled (bold) on the same axes.
- Use Series.plot(kind=’bar’) on a short categorical Series of 5 items from Pandas.
- Create a DataFrame with three columns and plot a stacked area chart using DataFrame.plot.area().
- Plot a Pandas time series and show monthly ticks only using MonthLocator.
- Plot a weekly time series for 12 weeks and format ticks to show “Week 1…Week 12”.
- Create a histogram from a NumPy array of 2,000 values; set bins=50 and alpha=0.7.
- Plot two synchronized subplots: top shows a time series; bottom shows its 7-day rolling mean (Pandas).
- Create a simple FuncAnimation that animates a growing line y = sin(x) as x extends.
- Create a FuncAnimation that updates a scatter’s y-values randomly each frame for 50 frames.
- Use ArtistAnimation to animate 10 pre-rendered images (e.g., 2D arrays with imshow).
- Simulate streaming data by appending one new random point every frame to a line plot for 100 frames.
- Simulate a real-time bar chart where one bar’s height changes each frame for 60 frames.
Intermediate Questions
- From a Pandas DataFrame with columns open, high, low, close, plot close as a time series and add a 10-period rolling mean overlay.
- Plot two time series on the same axes and format ticks with AutoDateLocator and ConciseDateFormatter.
- Create a dual-axis chart: DataFrame[‘sales’] (bar) on left y-axis and DataFrame[‘profit’] (line) on right y-axis over monthly dates.
- 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.
- 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.
- Use Pandas to resample a 1-minute series to 15-minute means and plot both original (light) and resampled (dark).
- Create a DataFrame of 3 columns and plot them with DataFrame.plot(subplots=True, layout=(3,1)) sharing the x-axis.
- Plot a heatmap (imshow) of a 100×100 NumPy array and add a colorbar with label “Intensity”.
- Plot a timeseries and use custom tick format “Day N” for daily ticks (1…30) via FuncFormatter.
- Plot a scatter of 50,000 points; make it readable using alpha, small s, and plt.hexbin as an alternative on a second axes.
- Build a FuncAnimation that animates a rolling window (size 50) over a precomputed signal, updating the visible x-limits per frame.
- Create an ArtistAnimation sequence from a list of line artists capturing a random walk’s trajectory growth.
- Simulate streaming sensor data from three channels; update three lines in one axes using blitting for performance.
- Plot a Pandas time series and add vertical span (axvspan) to highlight weekends across two months.
- Create a candlestick-like plot using bars/lines from OHLC data without external libraries.
- Plot a very sparse timeseries (irregular timestamps) and format the x-axis ticks to avoid overlap with max_n_locator or rotation and padding.
- Plot a multi-index DataFrame (e.g., city × metric) by selecting one metric across cities and plotting as grouped bars.
- Create a large scatter and use path_effects or edgecolors to maintain point visibility; compare runtime with and without edgecolors.
- Build a “play/pause” animation control using FuncAnimation and event callbacks (keyboard or mouse) to start/stop updates.
- Stream random points into a deque (fixed length) and animate a live updating line where old points drop off the left.
Advanced Questions
- 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.
- 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).
- 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.
- 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.
- Create an ArtistAnimation story: 20 frames alternating between imshow heatmaps and contour overlays, synchronized color limits, saved as GIF and MP4.
- 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.
- Stream live data from a generator that occasionally drops samples; maintain real-time plotting while interpolating missing points and marking gaps with red markers.
- 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.
- 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.
- 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.