首页 > 解决方案 > 与 figurefactory 重叠的图

问题描述

我正在尝试绘制一些财务图表,并为此使用了 plotly 库。

我对此很陌生,所以不得不寻找一种方法来做到这一点,并想离线进行,所以我找到了这种方式:

import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader import data as web
import datetime
import fix_yahoo_finance as yf
import plotly
from plotly import figure_factory as ff
import os

yf.pdr_override()


class Stock:
    #We need a constructor to get all the data when we create the class
    def __init__(self, name): #This needs a string as argument to work
        self.name = name
        #If we already have the sotcks data we don't download it again
        if os.path.exists('stock_prices_yahoo/' + name + '.csv'):
            self.df = pd.read_csv('stock_prices_yahoo/' + name + '.csv')
        else:
            start_date = '1950-01-01'
            end_date = datetime.date.today()
            df = web.get_data_yahoo(name, start_date, end_date)
            df.to_csv('stock_prices_yahoo/' + name + '.csv')

    def plot_stock(self): #This will print a plot of the stock prices
        fig = ff.create_candlestick(self.df.Open, self.df.High, self.df.Low, self.df.Close, dates = self.df.Date)
        fig['layout'].update({
        'title': self.name + ' prices',
        'yaxis': {'title': 'Price'},
        'xaxis': {'title': 'Date'},
        })
        plotly.offline.plot(fig, filename = self.name + '-candlestick.html', validate = True)
AAPL = Stock('AAPL')
AAPL.plot_stock()

关键是我想在上面重叠一些移动平均线,并且在整个互联网上寻找它之后,人们总是通过 matplotlib 来做。有没有办法用 figure_factory 做到这一点?

标签: pythonplotgraphplotlyoverlap

解决方案


推荐阅读