Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行程序时未显示图形(打印)_Python_Python 3.x_Charts_Plotly - Fatal编程技术网

Python 运行程序时未显示图形(打印)

Python 运行程序时未显示图形(打印),python,python-3.x,charts,plotly,Python,Python 3.x,Charts,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.DataRe

当我运行我的程序时,它不会抛出任何错误,但是它似乎作为一个无限循环运行,永远不会完成执行或在任何地方向我显示图形输出,而预期的输出应该是一个带有烛台图、多行和卷条形图的图形:

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)
如果需要更多信息,请告诉我

***答案***

导入图表\u studio.plotly作为plt
需要一些时间的在线兼容性,因此为了解决这一问题,我将导入更改为:
从plotly.offline导入plot

然后为了能看到这个图,我把最后一行从:
plt.iplot(图,filename='candlestick',validate=True)

致:

plot(图,文件名='candlestick-test-3.html',validate=False)


因此,图形将在浏览器中创建和打开

你试过把
plt.show()
作为你代码的最后一行吗?是的,我试过了,但没有在代码的结尾处找到它挂起的地方。我这样做了,它平稳运行,直到
plt.iplot(图,filename='candlestick',validate=True)
你做过
打印(图)
并查看字典以确认所有内容的格式是否正确?