import yfinance as yf import pandas as pd import matplotlib.pyplot as plt # Dow Jones Index Ticker Symbols dow_tickers = [ "AAPL", "MSFT", "UNH", "JNJ", "V", "WMT", "PG", "NVDA", "HD", "JPM", "AMZN", "CVX", "MRK", "KO", "CSCO", "MCD", "DIS", "CRM", "NKE", "AMGN", "VZ", "IBM", "CAT", "GS", "BA", "TRV", "MMM", "AXP", "SHW", "HON" ] # Fetch market cap and opening price data from Yahoo Finance data = [] for ticker in dow_tickers: stock = yf.Ticker(ticker) info = stock.info market_cap = info.get("marketCap", 0) hist = stock.history(period="1d") opening_price = hist["Open"].iloc[0] if not hist.empty else 0 data.append((ticker, market_cap, opening_price)) # Convert to DataFrame market_caps = pd.DataFrame(data, columns=["Ticker", "MarketCap", "OpeningPrice"]) # Calculate Normalized Market Caps market_caps = market_caps.sort_values(by="MarketCap", ascending=False).reset_index(drop=True) market_caps["NormalizedMarketCap"] = market_caps["MarketCap"] / market_caps["MarketCap"].sum() # Calculate Price Weighted Normalized Values market_caps["NormalizedPriceWeight"] = market_caps["OpeningPrice"] / market_caps["OpeningPrice"].sum() # Plot Normalized Market Caps plt.figure(figsize=(12, 6)) plt.bar(market_caps["Ticker"], market_caps["NormalizedMarketCap"], color='lightblue') plt.title("Normalized Market Caps of Dow Jones Stocks", fontsize=14) plt.ylabel("Normalized Market Cap", fontsize=12) plt.xlabel("Ticker", fontsize=12) plt.xticks(rotation=45, fontsize=10) plt.yticks(fontsize=10) plt.grid(axis='y', visible=True) plt.tight_layout() plt.show() # Plot Price Weighted Normalized Values plt.figure(figsize=(12, 6)) plt.bar(market_caps["Ticker"], market_caps["NormalizedPriceWeight"], color='orange') plt.title("Price Weighted Normalized Values of Dow Jones Stocks", fontsize=14) plt.ylabel("Normalized Price Weight", fontsize=12) plt.xlabel("Ticker", fontsize=12) plt.xticks(rotation=45, fontsize=10) plt.yticks(fontsize=10) plt.grid(axis='y', visible=True) plt.tight_layout() plt.show()