我有一個 python 代碼,通過使用 Matplotlib 實時顯示燭臺圖表,所以最后一個柱狀圖每秒更新一次,問題是程式不允許我向后滾動/放大......(與圖表互動),因為它不斷重置位置。我該如何解決這個問題?謝謝,祝你有美好的一天。
import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation
# Class to simulate getting more data from API:
class RealTimeAPI():
def __init__(self):
self.data_pointer = 0
self.data_frame = pd.read_csv('SP500_NOV2019_IDay.csv',
index_col=0, parse_dates=True)
# self.data_frame = self.data_frame.iloc[0:120,:]
self.df_len = len(self.data_frame)
def fetch_next(self):
r1 = self.data_pointer
self.data_pointer = 1
if self.data_pointer >= self.df_len:
return None
return self.data_frame.iloc[r1:self.data_pointer, :]
def initial_fetch(self):
if self.data_pointer > 0:
return
r1 = self.data_pointer
self.data_pointer = int(0.2*self.df_len)
return self.data_frame.iloc[r1:self.data_pointer, :]
rtapi = RealTimeAPI()
resample_map = {'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last'}
resample_period = '15T'
df = rtapi.initial_fetch()
rs = df.resample(resample_period).agg(resample_map).dropna()
fig, axes = mpf.plot(rs, returnfig=True, figsize=(11, 8),
type='candle', title='\n\nGrowing Candle')
ax = axes[0]
def animate(ival):
global df
global rs
nxt = rtapi.fetch_next()
if nxt is None:
print('no more data to plot')
ani.event_source.interval *= 3
if ani.event_source.interval > 12000:
exit()
return
df = df.append(nxt)
rs = df.resample(resample_period).agg(resample_map).dropna()
ax.clear()
mpf.plot(rs, ax=ax, type='candle')
ani = animation.FuncAnimation(fig, animate, interval=250)
mpf.show()
uj5u.com熱心網友回復:
您發布的代碼是mplfinance 存盤庫中的“蠟燭影片”示例。Mplfinance并沒有用Matlab而是 MatPlotLib。
(編輯您的問題并將“Matlab”的所有參考更改為“Mplfinance/Matplotlib”很誘人,但我會將其留給您去做,以防萬一我遺漏了某些內容或者您實際上正在使用 Matlab 做某事。此外,只是為了清楚,代碼繪制的是“燭臺圖”,而不是“條形圖”。順便說一下,MatPlotLib 最初是為了有點類似于 Matlab 繪圖界面而撰寫的,所以有些混亂是可以理解的,但是 matplotlib 已經發展了在許多方面)。
@CrisLuengo 的評論是正確的,因為 mplfinance 代碼正在使用每個影片幀或多或少地從頭開始重新繪制繪圖,因此任何互動式更改(例如放大)都將隨每個影片幀重新設定。mplfinance 影片示例這樣做是為了使代碼更簡單。
我相信有可能完成你想要的(具有互動性的影片)但是我自己從來沒有做過,所以沒有明確說明如何去做,如果我打算這樣做,我可能會做一些事情以下帖子的行:
- https://stackoverflow.com/a/46327978/1639359
- https://stackoverflow.com/a/46328223/1639359
- https://github.com/matplotlib/mplfinance/issues/262#issuecomment-692316347
完全披露:我是mplfinance 包的維護者。也許有一天我們會增強 mplfinance 使這更容易;但就目前而言,這種增強不是優先事項;因此需要上述更復雜的解決方案之一。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324254.html
標籤:蟒蛇-3.x matplotlib 图表 重启 公共财政
