如何在plotly和numpy中操作json?

问题描述 投票:0回答:1

我正在尝试使用这种json数据以图形方式绘制图形。我已经打开了json文件,并将数据加载到“ data”变量中。下面的代码随机生成指向图形的点。如何在x轴上处理数据,以便在x轴上显示时间跨度,在y轴上显示值。

import plotly.graph_objects as go

import numpy as np
import json

with open('test.json') as json_file:
    data = json.load(json_file)



fig = go.Figure()

trace_num = 1
point_num = 5000
for i in range(trace_num):
    fig.add_trace(
        go.Scattergl(
                x = np.linspace(0, 1, point_num),
                y = np.random.randn(point_num)+(i*5)
        )
    )

fig.update_layout(showlegend=False)

fig.show()
{
    "SpotKey": "79",
    "SpotName": "ELIX",
    "Denomination": "eur/mwh",
    "Elements": [
      {
        "Date": "2017-01-01T00:00:00",
        "Base": 36.8696,
        "Peak": 36.0125,
        "TimeSpans": [
          {
            "TimeSpan": "00:00-01:00",
            "Value": 46.43
          },
          {
            "TimeSpan": "01:00-02:00",
            "Value": 42.43
          }
        ]
      },
      {
        "Date": "2017-01-02T00:00:00",
        "Base": 53.7413,
        "Peak": 63.0317,
        "TimeSpans": [
          {
            "TimeSpan": "00:00-01:00",
            "Value": 41.18
          },
          {
            "TimeSpan": "01:00-02:00",
            "Value": 37.34
          }
        ]
      }
    ]
}

这里的复杂之处在于日期,随着日期的进行,我需要将日期添加到时间上吗?

python numpy plotly
1个回答
0
投票
X, Y = [], [] for item in data['Elements']: for sub_item in item['TimeSpans']: X.append(sub_item['TimeSpan']) Y.append(sub_item['Value'])
© www.soinside.com 2019 - 2024. All rights reserved.