4

When I am using the plotly with datetime, it shows the wrong time, e.g. 8/12/2017 10:00am, but the plotly's plot show is 8/12/2017 2:00am instead.

I want to plot the candlestick with the datetime as the x-axis, and the datetime is initialized as followed:

    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)]

and using the plotly as following:

    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')

I have checked the initiation of the datetime correctly, e.g. 8/12/2017 10:00am (i.e. datetime.datetime(2017, 12, 8, 10, 0, 0, 0),) but the plotly's plot show is 8/12/2017 2:00am instead.

Plotly Result

I have tried the following methods but none works for me: 1. adding os.environ['TZ'] = 'Hongkong' 2. datetime_obj = datetime_obj.replace(tzinfo=pytz.timezone('Hongkong')

Thanks for your attention and I appreciate any help and discussion.

Vincent
  • 955
  • 2
  • 15
  • 32
  • So, you are located in HongKong (_HKT_) but you want the _GMT_ time (-8) to be displayed.What is your local timezone? (`time.strftime("%Z", time.gmtime())`) – CristiFati Dec 08 '17 at 08:57
  • 2
    Looks like this [issue](https://github.com/plotly/plotly.py/issues/209). – broomrider Dec 12 '17 at 01:40

1 Answers1

7

When you use datetime, Plotly always shows it in UTC time. As far as I could find, they never created an option for you to pass to modify that behavior.

However, I found out you if you pass the x-axis data as strings, Plotly converts it to datetime and ends up displaying it the way you have passed it. x = datetime_data.astype('str') should do the job.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
atatude
  • 71
  • 1
  • 2