Summary
Keywords
Full Transcript
@MattMacarty ## ๐ Python Matplotlib: Plot Performance Grid of Multiple Stocks with Dynamic Subplots | Part 7 Welcome to Part 7 of the **Python Stock Analysis Course**! This video focuses on **advanced data visualization** using **Matplotlib**. We solve the challenge of plotting the relative performance of numerous stocks (e.g., all 20+ securities in an economic sector) by creating a **dynamic grid of subplots**. You will learn how to read every individual stock file from a folder and dynamically calculate the required number of rows and columns to fit all plots onto a single, organized figure. This is crucial for comparing the performance of many stocks at a glance. ### ๐ฏ Key Learning Outcomes: 1. **Dynamic Subplot Sizing:** Use the **`math` module** and the `plt.subplots` function to calculate the exact number of rows needed for your grid, regardless of how many stock files are in the folder. 2. **File Iteration:** Loop through all individual stock data files in a folder and plot each one sequentially. 3. **Relative Performance:** Transform the raw closing prices into **relative performance** data (normalized to the starting price) for a meaningful comparison. 4. **Matplotlib Formatting:** Apply advanced formatting using **`matplotlib.ticker.PercentFormatter`** to display the y-axis as clean percentages. ### โฑ๏ธ Video Chapters (Jump Ahead!): 0:00 - Introduction & Review (Goal: Plot all stocks in one figure) 0:40 - Reading All Files & Importing the `math` Module 1:06 - **Dynamic Subplots Setup** (Using `math.ceil` for grid dimensions) 1:49 - Nested For Loop for Plotting to the Correct Axes 2:20 - Reading CSV, Calculating **Relative Performance** 2:30 - Plotting to the Correct Axes (`ax[row, col]`) 2:46 - Formatting Y-Axis as **Percent** with `matplotlib.ticker` 3:17 - Handling Errors/End of Data with `try/except` Block 3:40 - Testing the Function (Plotting the Energy Sector Grid) 4:25 - Preview of Part 8: More cuts of data from the EOD API ### ๐ Course Series & Resources: * **Part 6 (Export to Excel):** [https://youtu.be/Ox7qVlfNYNE](https://youtu.be/Ox7qVlfNYNE) * **Get the Code:** https://github.com/mjmacarty/python-stock-analysis ***Disclaimer: This video is for educational purposes only. The information provided should not be construed as investment advice. *** \#Matplotlib \#PythonSubplots \#PerformanceGrid \#DataVisualization \#RelativePerformance \#PythonForFinance \#StockAnalysis \#DynamicPlotting \#Part7
