Let's imagine I get the stock market of Tesla and Microsoft and I want to print the close price for both.
import pandas as pd
import yfinance as yf
start = '2010-01-01'
end = '2020-12-31'
tesla = yf.download('TSLA', start=start, end=end)
microsoft = yf.download('MSFT', start=start, end=end)
I can easily plot each on its own plot:
microsoft.Close.plot()
Or use axes to hack them on the same plot, but I don't get the legend:
ax = microsoft.Close.plot()
ax = tesla.Close.plot(ax=ax)
Or I can merge my two DataFrame
# Ugly but elegant...
frames = []
for u in ['microsoft', 'tesla']:
df = globals()[u]
df.columns = pd.MultiIndex.from_arrays([[u] * len(df.columns), list(df.columns.values)])
frames.append(df)
df = pd.merge(*frames, on=["Date"])
df[[('tesla', 'Open'), ('microsoft', 'Open')]].plot()
The latter solution seems better since I am simply plotting whatever I want, but the merge process seems cumbersome.
Any better alternative?