交互式股票图表,带键盘箭头的分步动画,带Matplolib [英] Interactive Stock Chart, step by step animation with keyboard arrows, with Matplolib

查看:60
本文介绍了交互式股票图表,带键盘箭头的分步动画,带Matplolib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib绘制交互式股票图表。它应该是这样工作的:

  1. 最初它在绘图中显示60个条形图(从数据框的第一个条形图到第60个条形图)
  2. 按键盘上的右箭头后,它显示第61条,第一条消失(因此,现在绘图显示数据帧的第二条到第61条)。如果我再次按向右箭头,曲线图将显示第三个到第62个,依此类推。这里的想法是,当我按向右箭头时,在绘图中显示固定数量的条形图。
  3. 按向左箭头后退一步(例如。如果图表在第2到第61位,按左箭头后将返回到第一位到第60位)
我编写了下面的代码,但不幸的是,当我按向右箭头时,它只向绘图添加了一个条形图,第一个条形图永远不会消失。而且,当我按向左箭头时,没有任何反应。有人能帮帮忙吗?代码:

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 = 0

#Chart Commands

x = np.arange(0,len(df_apple))
fig, (ax, ax2) = plt.subplots(2, figsize=(12,8), gridspec_kw={'height_ratios': [4, 1]}, sharex = True)
val_array = []
for idx, val in df_apple.iterrows():
    val_array.append(val)

# ticks top plot
ax2.set_xticks(x[::3])
#ax2.set_xticklabels(df_apple.date.dt.date[::3])
ax.set_xticks(x, minor=True)
# labels
ax.set_ylabel('USD')
ax2.set_ylabel('Volume')
# grid
ax.xaxis.grid(color='black', linestyle='dashed', which='both', alpha=0.1)
ax2.set_axisbelow(True)
ax2.yaxis.grid(color='black', linestyle='dashed', which='both', alpha=0.1)
# remove spines
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
# get max volume + 10%
mx = df_apple['volume'].max()*1.1
# define tick locations - 0 to max in 4 steps
yticks_ax2 = np.arange(0, mx+1, mx/4)
# create labels for ticks. Replace 1.000.000 by 'mi'
yticks_labels_ax2 = ['{:.2f} mi'.format(i/1000000) for i in yticks_ax2]
ax2.yaxis.tick_right() # Move ticks to the left side
# plot y ticks / skip first and last values (0 and max)
plt.yticks(yticks_ax2[1:-1], yticks_labels_ax2[1:-1])
plt.ylim(0,mx)
# title
ax.set_title('Apple Stock Price
', loc='left', fontsize=20)

#Plot the chart, displaying the bars from 0 to bars_to_display

for i in range(0,bars_to_display):
    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')


plt.ion()

def on_keyboard(event):
    global step
    if event.key == 'right':
        step += 1
    elif event.key == 'left':
        step -= 1
        if step <= 0: step=0

    #Plot the chart, displaying the bars from 1: bars_to_display +1; then 2: bars_to_display +2, etc...
        
    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')
    

plt.gcf().canvas.mpl_connect('key_press_event', on_keyboard)

plt.show()

OBS:我使用的数据帧格式如下:

      index       date      open      high       low     close     volume  Name
0      1259 2013-02-08   67.7142   68.4014   66.8928   67.8542  158168416  AAPL
1      1260 2013-02-11   68.0714   69.2771   67.6071   68.5614  129029425  AAPL
2      1261 2013-02-12   68.5014   68.9114   66.8205   66.8428  151829363  AAPL
3      1262 2013-02-13   66.7442   67.6628   66.1742   66.7156  118721995  AAPL
4      1263 2013-02-14   66.3599   67.3771   66.2885   66.6556   88809154  AAPL

推荐答案

做得好。
您只需添加两项内容即可使您的代码正常工作:

  • plt.draw()on_keyboard函数末尾:这是更新绘图所必需的
  • ax.cla()ax2.cla()ax2.cla()on_keyboard函数中的for循环之前:这是从以前的曲线图中清除轴所必需的

但是ax.cla()删除网格,因此我建议您对代码进行一些编辑:

  • 将所有绘图调用(循环和布局管理的绘图)移到on_keyboard函数中,这样用户按下<🠰>或<🠲>时,不仅会更新绘图,也会更新布局管理。这使您可以避免在on_keyboard外部和内部对循环(和布局管理)进行重排

  • 移出on_keybaordfor循环进行绘制,无用重复

  • 在布局管理中添加ax.set_xlimax2.set_xlim以固定x轴限制

  • plt.show()之前添加对on_keyboard的调用以绘制绘图

  • 上一个调用需要传递给on_keyboard一个event参数:您只需传递0并在If语句中的on_keyboard内捕获它

如上所述,工作代码:
(我删除了plt.ion()并为我工作,您可能需要恢复它)

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 = 0

# Chart Commands

x = np.arange(0, len(df_apple))
fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)
val_array = []
for idx, val in df_apple.iterrows():
    val_array.append(val)


def on_keyboard(event):

    global step
    if event != 0:
        if event.key == 'right':
            step += 1
        elif event.key == 'left':
            step -= 1
            if step <= 0: step = 0

    # Plot the chart, displaying the bars from 1: bars_to_display +1; then 2: bars_to_display +2, etc...

    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')

    # ticks top plot
    ax2.set_xticks(x[::3])
    # ax2.set_xticklabels(df_apple.date.dt.date[::3])
    ax.set_xticks(x, minor = True)
    # labels
    ax.set_ylabel('USD')
    ax2.set_ylabel('Volume')
    # grid
    ax.xaxis.grid(color = 'black', linestyle = 'dashed', which = 'both', alpha = 0.1)
    ax2.set_axisbelow(True)
    ax2.yaxis.grid(color = 'black', linestyle = 'dashed', which = 'both', alpha = 0.1)
    # remove spines
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax2.spines['right'].set_visible(False)
    ax2.spines['left'].set_visible(False)
    # get max volume + 10%
    mx = df_apple['volume'].max()*1.1
    # define tick locations - 0 to max in 4 steps
    yticks_ax2 = np.arange(0, mx + 1, mx/4)
    # create labels for ticks. Replace 1.000.000 by 'mi'
    yticks_labels_ax2 = ['{:.2f} mi'.format(i/1000000) for i in yticks_ax2]
    ax2.yaxis.tick_right()  # Move ticks to the left side
    # plot y ticks / skip first and last values (0 and max)
    plt.yticks(yticks_ax2[1:-1], yticks_labels_ax2[1:-1])
    plt.ylim(0, mx)
    # title
    ax.set_title('Apple Stock Price
', loc = 'left', fontsize = 20)

    ax.set_xlim(x[step] - 1, x[bars_to_display + step] + 1)
    ax2.set_xlim(x[step] - 1, x[bars_to_display + step] + 1)

    plt.draw()


plt.gcf().canvas.mpl_connect('key_press_event', on_keyboard)

on_keyboard(0)

plt.show()

🠲拍摄20次之后:

这篇关于交互式股票图表,带键盘箭头的分步动画,带Matplolib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆