簡體   English   中英

運行程序時圖形不顯示(PLOTLY)

[英]Graph Not Showing Up when running program (PLOTLY)

當我運行我的程序時,它不會拋出任何錯誤,但是它似乎作為一個無限循環運行,永遠不會完成執行或在任何地方向我顯示圖形輸出,當預期輸出應該是帶有燭台圖和多條線和音量條的圖形時圖表:

import pandas_datareader as web
from datetime import datetime
import numpy as np
import pandas as pd
import chart_studio.plotly as plt

dataframe=\
    web.DataReader('SPY','yahoo',datetime(2020,10,16),datetime(2020,11,16))
dataframe.head()

INCREASING_COLOR = '#17BECF'
DECREASING_COLOR = '#7F7F7F'

data = [ dict(
    type='candlestick',
    open=dataframe.Open,
    high=dataframe.High,
    low=dataframe.Low,
    close=dataframe.Close,
    x=dataframe.index,
    yaxis = 'y2',
    name = 'SPY',
)]

layout = dict()
figure = dict(data=data,layout=layout)

figure['layout'] = dict()
figure['layout']['plot_bgcolor'] = 'rgb(250, 250, 250)'
figure['layout']['xaxis'] = dict( rangeselector = dict( visible = True ) )
figure['layout']['yaxis'] = dict( domain = [0, 0.2], showticklabels = False )
figure['layout']['yaxis2'] = dict( domain = [0.2, 0.8] )
figure['layout']['legend'] = dict( orientation = 'h', y=0.9, x=0.3, yanchor='bottom' )
figure['layout']['margin'] = dict( t=40, b=40, r=40, l=40 )

rangeselector=dict(
    visible=True,
    x=0, y=0.9,
    bgcolor='rgba(150,200,250,0.4)',
    font=dict(size=13),
    buttons=list([
        dict(count=1,
             label='reset',
             step='all'),
        dict(count=1,
             label='1yr',
             step='year',
             stepmode='backward'),
        dict(count=3,
             label='3mo',
             step='month',
             stepmode='backward'),
        dict(count=1,
             label='1mo',
             step='month',
             stepmode='backward'),
        dict(step='all')
    ]))
figure['layout']['xaxis']['rangeselector']=rangeselector

def movingaverage(interval,window_size=10):
    window=np.ones(int(window_size))/float(window_size)
    return np.convolve(interval,window,'same')

movingaverage_y=movingaverage(dataframe.Close)
movingaverage_x=list(dataframe.index)

# Clip the ends
movingaverage_x=movingaverage_x[5:-5]
movingaverage_y=movingaverage_y[5:-5]

figure['data'].append(dict(x=movingaverage_x,y=movingaverage_y,
                           type='scatter',mode='lines',
                           line=dict(width=1),
                           marker=dict(color='#E377C2'),
                           yaxis='y2',name='Moving Average'))

colors=[]
for i in range(len(dataframe.Close)):
    if i!=0:
        if dataframe.Close[i]>dataframe.Close[i-1]:
            colors.append(INCREASING_COLOR)
        else:
            colors.append(DECREASING_COLOR)
    else:
        colors.append(DECREASING_COLOR)

figure['data'].append(dict(x=dataframe.index,y=dataframe.Volume,
                           marker=dict(color=colors),
                           type='bar',yaxis='y',name='Volume'))

# ---------- BOLLINGER BANDS ------------
def bollinger_bands(price,window_size=10,num_of_std=5):
    rolling_mean = price.rolling(window=window_size).mean()
    rolling_std = price.rolling(window=window_size).std()
    upper_band = rolling_mean + (rolling_std * num_of_std)
    lower_band = rolling_mean - (rolling_std * num_of_std)
    return rolling_mean, upper_band, lower_band

bollinger_bands_average,upper_band,lower_band=bollinger_bands(dataframe.Close)
figure['data'].append(dict(x=dataframe.index,y=upper_band,type='scatter',yaxis='y2',
                            line=dict(width=1),
                            marker=dict(color='#ccc'), hoverinfo='none',
                            legendgroup='Bollinger Bands',name='Bollinger Bands'))
figure['data'].append(dict(x=dataframe.index,y=lower_band,type='scatter',yaxis='y2',
                            line=dict(width=1),
                            marker=dict(color='#ccc'), hoverinfo='none',
                            legendgroup='Bollinger Bands',showlegend=False))
# ----------------------------------------

plt.iplot(figure, filename='candlestick',validate=True)

如果需要更多信息,請告訴我

*** 回答 ***

import chart_studio.plotly as plt需要一些時間的在線兼容性,所以為了解決這個問題,我將導入更改為: from plotly.offline import plot

然后為了能夠看到這個情節,我改變了最后一行: plt.iplot(figure,filename='candlestick',validate=True)
到:

plot(figure, filename = 'candlestick-test-3.html', validate = False )

因此,圖表將在您的瀏覽器中創建和打開!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM