我試圖用mplfinance.
這有效:
for coin in coins:
mpf.plot(df_coins[coin], title=coin, type='line', volume=True, show_nontrading=True)
但是,每個圖在我的 Python Notebook 單元格輸出中都是一個單獨的影像。并且對每個影像重復 x 軸標記。
我嘗試制作一個包含多個子圖/軸的圖形,并在每個軸上繪制一個圖表:
from matplotlib import pyplot as plt
N = len(df_coins)
fig, axes = plt.subplots(N, figsize=(20, 5*N), sharex=True)
for i, ((coin, df), ax) in zip(enumerate(df_coins.items()), axes):
mpf.plot(df, ax=ax, title=coin, type='line', volume=True, show_nontrading=True)
這將顯示正確維度的子圖,但是它們沒有填充資料。坐標區標記為 0.0 到 1.0,標題未出現。
我錯過了什么?
uj5u.com熱心網友回復:
子繪圖有兩種方法。一種是用mplfinance物件建立一個圖形。另一種方法是使用您采用的 matplotlib 子圖來放置它。
金融資料
import matplotlib.pyplot as plt
import mplfinance as mpf
import yfinance as yf
tickers = ['AAPL','GOOG','TSLA']
data = yf.download(tickers, start="2021-01-01", end="2021-03-01", group_by='ticker')
aapl = data[('AAPL',)]
goog = data[('GOOG',)]
tsla = data[('TSLA',)]
公共財政
fig = mpf.figure(style='yahoo', figsize=(12,9))
#fig.subplots_adjust(hspace=0.3)
ax1 = fig.add_subplot(3,1,1, sharex=ax3)
ax2 = fig.add_subplot(3,1,2, sharex=ax3)
ax3 = fig.add_subplot(3,1,3)
mpf.plot(aapl, type='line', ax=ax1, axtitle='AAPL', xrotation=0)
mpf.plot(goog, type='line', ax=ax2, axtitle='GOOG', xrotation=0)
mpf.plot(tsla, type='line', ax=ax3, axtitle='TSLA', xrotation=0)
ax1.set_xticklabels([])
ax2.set_xticklabels([])
matplotlib
N = len(tickers)
fig, axes = plt.subplots(N, figsize=(20, 5*N), sharex=True)
for df,t,ax in zip([aapl,goog,tsla], tickers, axes):
mpf.plot(df, ax=ax, axtitle=t, type='line', show_nontrading=True)# volume=True
uj5u.com熱心網友回復:
除了@r-beginners 提到的技術之外,還有另一種技術可能適用于所有繪圖共享相同 x-axis 的情況。那就是使用mpf.make_addplot().
aps = []
for coin in coins[1:]:
aps.append(mpf.make_addplot(df_coins[coin]['Close'], title=coin, type='line'))
coin = coins[0]
mpf.plot(df_coins[coin],axtitle=coin,type='line',volume=True,show_nontrading=True,addplot=aps)
如果您選擇 dotype='candle'而不是'line',則更改
df_coins[coin]['Close']
簡單地
df_coins[coin]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/342636.html
標籤:matplotlib 子图 公共财政
