当我使用plotly和datetime一起使用时,它显示了错误的时间,例如8/12/2017 10:00am,但是plotly的绘图显示为8/12/2017 2:00am。
我想要绘制蜡烛图,其中datetime作为x轴,并且datetime的初始化如下:
并使用以下方式的plotly:
我已经检查过日期时间的初始化,例如8/12/2017 10:00am(即datetime.datetime(2017,12,8,10,0,0,0)),但是Plotly的图表显示为8/12/2017 2:00am。
我想要绘制蜡烛图,其中datetime作为x轴,并且datetime的初始化如下:
today = datetime.datetime.now()
self.start_time = today.replace(second=0, microsecond=0)
num_data_points = 90
# define OHLC data variable for plotting candles
self.open_data = [0] * num_data_points
self.high_data = [0] * num_data_points
self.low_data = [0] * num_data_points
self.close_data = [0] * num_data_points
self.time_interval = datetime.timedelta(minutes=time_interval)
self.datetime_data = [self.start_time+datetime.timedelta(minutes=i) for i in range(num_data_points)]
并使用以下方式的plotly:
layout = go.Layout(
plot_bgcolor = '#fafafa',
yaxis = dict( domain = [0, 0.2], showticklabels = True ),
yaxis2 = dict( domain = [0.25, 0.8], range=[self.y_min, self.y_max] ),
legend = dict( orientation = 'h', y=0.9, x=0.3, yanchor='bottom' ),
margin = dict( t=40, b=40, r=40, l=40 ),
boxgroupgap = 0.0,
boxgap = 0.0,
title='Volume: {0}, Avg: {1:.2f}, Std: {2:.3f}'.format(
self.volumn_sum,
self.price_avg,
self.price_std,
)
)
# print(self.datetime_data)
# datetime_data = np.array(self.datetime_data, dtype='M8[us]')
datetime_data = pd.Series(self.datetime_data).tz_convert('Hongkong')
print('='*30, '\n', datetime_data)
# ADD candlestick
candle_trace = go.Candlestick(
open = self.open_data,
high = self.high_data,
low = self.low_data,
close = self.close_data,
x = datetime_data,
yaxis = 'y2',
name = 'Candlestick',
# increasing = dict(line=dict(color=INCREASING_COLOR)),
# decreasing = dict(line=dict(color=DECREASING_COLOR)),
)
fig = go.Figure(data=data, layout=layout)
plot(fig, 'stock plot')
我已经检查过日期时间的初始化,例如8/12/2017 10:00am(即datetime.datetime(2017,12,8,10,0,0,0)),但是Plotly的图表显示为8/12/2017 2:00am。
我尝试了以下方法,但都没有成功: 1. 添加os.environ ['TZ'] ='Hongkong' 2. datetime_obj = datetime_obj.replace(tzinfo = pytz.timezone('Hongkong')
感谢您的关注,我非常感激任何帮助和讨论。
time.strftime("%Z", time.gmtime())
) - CristiFati