2 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
我們從同一個(gè)地方獲取數(shù)據(jù),盡管數(shù)據(jù)獲取方法不同。在以 15 個(gè)單位提取后,我通過(guò)排除晚上 8 點(diǎn)之后和下午 4 點(diǎn)之前的數(shù)據(jù)創(chuàng)建了一個(gè)圖表。我在理解您的跳過(guò)會(huì)打開(kāi)暫停的情況下創(chuàng)建了代碼。一旦設(shè)置了 NaN,您希望它跳過(guò)的內(nèi)容就會(huì)被跳過(guò)。
import datetime
import pandas as pd
import numpy as np
import pandas_datareader.data as web
import mplfinance as mpf
# import matplotlib.pyplot as plt
with open('./alpha_vantage_api_key.txt') as f:
api_key = f.read()
now_ = datetime.datetime.today()
start = datetime.datetime(2019, 1, 1)
end = datetime.datetime(now_.year, now_.month, now_.day - 1)
symbol = 'TSLA'
df = web.DataReader(symbol, 'av-intraday', start, end, api_key=api_key)
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
df.index = pd.to_datetime(df.index)
df["100ma"] = df["Close"].rolling(window = 50, min_periods = 0).mean()
df["Date"] = df.index
df_15 = df.asfreq('15min')
df_15 = df_15[(df_15.index.hour >= 4)&(df_15.index.hour <= 20) ]
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,4.5),dpi=144)
#Plotting all as 2 different subplots
ax1 = plt.subplot2grid((7,1), (0,0), rowspan = 5, colspan = 1)
ax1.plot(df_15["Date"], df_15['Close'])
ax1.plot(df_15["Date"], df_15["100ma"], linewidth = 0.5)
plt.xticks(rotation=20)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 2, colspan = 2, sharex = ax1)
ax2.bar(df_15["Date"], df_15["Volume"])
ax2.axes.xaxis.set_visible(False)
# plt.tight_layout()
plt.show()

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
我使用 matplotlib.ticker.formatter 修復(fù)了它。
我首先創(chuàng)建了一個(gè)類并使用:
class MyFormatter(Formatter):
def __init__(self, dates, fmt='%Y-%m-%d %H:%M'):
self.dates = dates
self.fmt = fmt
def __call__(self, x, pos=0):
'Return the label for time x at position pos'
ind = int(np.round(x))
if ind >= len(self.dates) or ind < 0:
return ''
return self.dates[ind].strftime(self.fmt)
formatter = MyFormatter(df.index)
ax1 = plt.subplot2grid((7,1), (0,0), rowspan = 5, colspan = 1)
ax1.xaxis.set_major_formatter(formatter)
ax1.plot(np.arange(len(df)), df["Close"])
ax1.plot(np.arange(len(df)), df["100ma"], linewidth = 0.5)
ax1.xticks(rotation=45)
ax1.axis([xmin,xmax,ymin,ymax])
ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 2, colspan = 2, sharex = ax1)
ax2.bar(np.arange(len(df)), df["5. volume"])
plt.show()
這給了我一個(gè)比之前更平滑的圖表,也是 r-beginner 推薦的圖表。
我遇到的唯一問(wèn)題是,如果我放大 x 軸并沒(méi)有真正改變。它總是有年、月、日、小時(shí)和分鐘。顯然,當(dāng)我進(jìn)一步放大時(shí),我只想要小時(shí)和分鐘。我還沒(méi)有弄清楚該怎么做
添加回答
舉報(bào)