运行程序时图表不显示(PLOTLY)
Posted
技术标签:
【中文标题】运行程序时图表不显示(PLOTLY)【英文标题】:Graph Not Showing Up when running program (PLOTLY) 【发布时间】:2021-02-28 08:04:04 【问题描述】:当我运行我的程序时,它不会抛出任何错误,但它似乎作为一个无限循环运行,永远不会完成执行或在任何地方向我显示图形输出,而预期输出应该是带有烛台图和多条线的图形和音量条形图:
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)
如果需要更多信息,请告诉我
【问题讨论】:
你试过plt.show()
作为你代码的最后一行吗?
是的,我有,但最后没有到达
将打印语句放在您的代码中以找到它挂起的位置
我做到了,它运行顺利,直到plt.iplot(figure, filename='candlestick',validate=True)
您是否完成了print(figure)
并查看了字典以确认所有内容的格式都正确?
【参考方案1】:
*** 回答 ***
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 )
因此图表将在您的浏览器中创建并打开!
【讨论】:
以上是关于运行程序时图表不显示(PLOTLY)的主要内容,如果未能解决你的问题,请参考以下文章
为啥通过 django 数据显示图表时 Plotly 显示空白?