import pandas as pd
from pandas_datareader import data
import matplotlib.pyplot as plt
%matplotlib inline
Visualization β Plotting Stock Data with Pandas and MatplotlibΒΆ
Visualization transforms raw numbers into insights. This notebook demonstrates plotting financial time-series data (Apple stock prices) using Pandasβ built-in .plot() method backed by Matplotlib. Topics include: line plots for individual and multiple columns, exploring Matplotlib style sheets (fivethirtyeight, ggplot, dark_background), creating bar charts and pie charts from custom-binned data using .apply() and .value_counts(), and combining data transformation with visualization.
Why this matters: The ability to quickly visualize data during exploration helps you spot trends, anomalies, and distributions that summary statistics alone might miss. In financial analysis, plotting closing prices over time reveals trends, while bar/pie charts of categorized performance help communicate findings to non-technical stakeholders.
apple = data.DataReader(name="AAPL", data_source="yahoo", start="2012", end="2018")
apple.head()
apple.plot()
apple.plot(y="Volume")
apple.plot(y="High")
apple.plot(y="Close")
apple["Close"].plot()
apple[["High","Low"]].plot()
plt.style.available
plt.style.use("fivethirtyeight")
apple["Close"].plot()
plt.style.use("seaborn-deep")
apple["Close"].plot()
plt.style.use("dark_background")
apple["Close"].plot()
plt.style.use("ggplot")
apple["Close"].plot()
def rank_performance(stock_price):
if stock_price < 120:
return "Poor"
elif stock_price > 120 and stock_price < 150:
return "Average"
else:
return "Awesome"
plt.style.use("ggplot")
apple["Close"].apply(rank_performance).value_counts().plot(kind="bar")
apple["Close"].min()
plt.style.use("ggplot")
apple["Close"].apply(rank_performance).value_counts().plot(kind="barh")
apple["Close"].mean()
plt.style.use("ggplot")
apple["Close"].apply(rank_performance).value_counts().plot(kind="pie",legend=True)
def custom_round(stock_price):
return int(stock_price/100) * 100
apple['High'].apply(custom_round).value_counts().sort_index()
apple["High"].min()
apple['Low'].apply(custom_round).nunique()