简体   繁体   English

运行程序时图形不显示(PLOTLY)

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

When I run my program, it does not throw any errors, however it seems to run as an infinite loop never finishing execution or showing me the graph output anywhere, when expected output should be a graph with a candlestick chart and multiple lines and volume bar chart:当我运行我的程序时,它不会抛出任何错误,但是它似乎作为一个无限循环运行,永远不会完成执行或在任何地方向我显示图形输出,当预期输出应该是带有烛台图和多条线和音量条的图形时图表:

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)

Let me know if more information is needed如果需要更多信息,请告诉我

*** ANSWER *** *** 回答 ***

import chart_studio.plotly as plt requires some time of online compatability so in order to combat this I changed the import to this: from plotly.offline import plot import chart_studio.plotly as plt需要一些时间的在线兼容性,所以为了解决这个问题,我将导入更改为: from plotly.offline import plot

Then in order to be able top see this plot, I changed the final line from: plt.iplot(figure,filename='candlestick',validate=True)然后为了能够看到这个情节,我改变了最后一行: plt.iplot(figure,filename='candlestick',validate=True)
to:到:

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

Thus the graph will be created and opened inside your browser!因此,图表将在您的浏览器中创建和打开!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM