【问题标题】:Interactive Stock Chart, step by step animation with a slider. Matplolib & Jupyter交互式股票图表,带有滑块的分步动画。 Matplotlib 和 Jupyter
【发布时间】:2021-09-05 18:18:49
【问题描述】:

在这篇文章中:Interactive Stock Chart, step by step animation with keyboard arrows, with Matplolib,我编写了一个代码,用户 Zephyr 出色地修复了该代码,它使用键盘箭头交互式地模拟股票。
事实证明,我在Jupyter 中找到了一种做同样事情的方法,使用模块ipywidgets。该代码有效,但不幸的是同一张图表被绘制了两次。我不知道为什么会这样。有人可以帮忙吗?我只想显示一个图(请注意,当我使用滑块时,第二个图不会移动)。
代码如下:

%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('all_stocks_5yr.csv')
df_apple = df[df['Name'] == 'AAPL'].copy()
df_apple['date'] = pd.to_datetime(df_apple['date'])
df_apple.reset_index(inplace = True)

bars_to_display = 60
step = widgets.IntSlider(value=0, min=0, max=len(df_apple)-bars_to_display)

val_array = []
for idx, val in df_apple.iterrows():
    val_array.append(val)
    
x = np.arange(0, len(df_apple))

fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)

def f(step):
    
    ax.cla()
    ax2.cla()
    
    for i in range(step, bars_to_display + step):
        
        color = '#2CA453'
        if val_array[i]['open'] > val_array[i]['close']: color = '#F04730'
        ax.plot([x[i], x[i]], [val_array[i]['low'], val_array[i]['high']], color = color)
        ax.plot([x[i], x[i] - 0.1], [val_array[i]['open'], val_array[i]['open']], color = color)
        ax.plot([x[i], x[i] + 0.1], [val_array[i]['close'], val_array[i]['close']], color = color)
        ax2.bar(x[i], val_array[i]['volume'], color = 'lightgrey')
        
    display(fig)
    

display(step)
out = widgets.interactive_output(f, {'step': step})
display(out)

【问题讨论】:

  • 这并没有解决交互式问题,但您可能应该使用mplfinance
  • 尝试将display(fig)替换为plt.show()。

标签: python matplotlib jupyter-notebook data-visualization finance


【解决方案1】:

行:

fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)

绘制第一个图形。之后添加plt.close()即可。

完整代码

from IPython.display import display
from ipywidgets import interactive, widgets
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline

df = pd.read_csv('all_stocks_5yr.csv')
df_apple = df[df['Name'] == 'AAPL'].copy()
df_apple['date'] = pd.to_datetime(df_apple['date'])
df_apple.reset_index(inplace = True)

bars_to_display = 60
step = widgets.IntSlider(value = 0, min = 0, max = len(df_apple) - bars_to_display)

val_array = []
for idx, val in df_apple.iterrows():
    val_array.append(val)

x = np.arange(0, len(df_apple))

fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)
plt.close()


def f(step):
    ax.cla()
    ax2.cla()

    for i in range(step, bars_to_display + step):

        color = '#2CA453'
        if val_array[i]['open'] > val_array[i]['close']: color = '#F04730'
        ax.plot([x[i], x[i]], [val_array[i]['low'], val_array[i]['high']], color = color)
        ax.plot([x[i], x[i] - 0.1], [val_array[i]['open'], val_array[i]['open']], color = color)
        ax.plot([x[i], x[i] + 0.1], [val_array[i]['close'], val_array[i]['close']], color = color)
        ax2.bar(x[i], val_array[i]['volume'], color = 'lightgrey')

    display(fig)


display(step)
out = widgets.interactive_output(f, {'step': step})
display(out)

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 2011-10-05
    • 2017-09-03
    • 2018-01-31
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    相关资源
    最近更新 更多