You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在lightweight_chart_python中修改图表时区为EST?

解决lightweight_chart_python PolygonChart时区切换为EST的问题

核心原因

你之前尝试转换数据时区的方式无效,是因为PolygonChart的前端渲染逻辑默认使用UTC时区显示,单纯修改数据的时区属性不会影响图表的显示设置。正确的做法是直接配置图表本身的时区参数,让前端自动完成时间转换。

正规解决方案

针对Polygon接口数据(无需手动转换数据)

直接设置图表的timezone属性为'America/New_York',该时区会自动兼容EST(东部标准时间)和EDT(东部夏令时),无需手动处理夏令时切换:

from lightweight_charts import PolygonChart

if __name__ == '__main__':
    chart = PolygonChart(
        api_key='<API-KEY>',
        num_bars=200,
        limit=5000,
        live=True
    )
    # 配置图表显示时区为美国东部时间
    chart.timezone = 'America/New_York'
    chart.show(block=True)

针对自定义本地数据

如果是使用自己生成的带UTC时区的数据,需要先将时间转换为EST并移除时区信息,同时配合图表时区设置:

import pandas as pd
import pytz
from lightweight_charts import Chart

# 示例自定义数据
bar_data = pd.DataFrame({
    'time': pd.date_range('2024-01-01 13:30', periods=10, freq='1H', tz='UTC'),
    'open': [100, 101, 102]*3 + [103],
    'high': [101, 102, 103]*3 + [104],
    'low': [99, 100, 101]*3 + [102],
    'close': [100.5, 101.5, 102.5]*3 + [103.5]
})

# 转换为EST时区并移除时区标记
est_tz = pytz.timezone('US/Eastern')
bar_data['time'] = bar_data['time'].dt.tz_convert(est_tz).dt.tz_localize(None)

# 初始化图表并设置时区
chart = Chart(timezone='America/New_York')
chart.set(bar_data)
chart.show(block=True)

内容的提问来源于stack exchange,提问作者user1144251

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:12:06