【发布时间】:2020-05-15 10:24:08
【问题描述】:
我有一些每日时间序列 JSON 数据,这些数据涵盖同一文件中的多个站点(底部 JSON 中的单个条目示例)。
我想使用 Bokeh 绘制这些,每个站点的时间序列(按“system_name”分类/分组)作为同一图上的不同颜色的线。如何获得每行的情节?当前的方法是尝试使用multi_line - 是否应该只是使用for 循环的p.line?
非常感谢指导/指针。
import json
from datetime import datetime
from pandas.io.json import json_normalize
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
output_file('wyndham.html')
with open('wyndham_data.txt', 'r') as f:
a = json.load(f)
res = json_normalize(a['features'])
gby = res.groupby('properties.system_name')
for key, item in gby:
g = item.sort_values(by='properties.date_stamp') **<<<works to here**
source = ColumnDataSource(dict(x = g[['properties.date_stamp']],
y = g[['properties.energy_prod(KWh)']]))
p = figure()
p.multi_line(x, y, source=source)
show(p)
示例 JSON:
{
"type" : "FeatureCollection",
"name" : "wyndham-solar-energy-production.json",
"features" : [
{
"type" : "Feature",
"geometry" : null,
"properties" : {
"system_id" : "9386741",
"system_name" : "Yerambooee Community Centre ",
"date_stamp" : "2018-08-01",
"energy_prod(KWh)" : 51.5,
"energy_life(MWh)" : null,
"C02 (Kg)" : 47.41,
"KWp" : 18.2,
"performance" : 2.8,
"lat" : -37.8587717,
"lon" : 144.7100923,
"date_installed" : "2017-07-27"
}
}, ...
【问题讨论】:
-
欢迎堆栈溢出!要获得最优质的答案,请尝试缩小范围。关于如何将 json 数据读入 pandas 的问题非常适合堆栈溢出(尽管已经有很多关于该主题的问题)。另外,您可以询问如何创建散景图。关于可视化此类数据的最佳策略的问题可能更多是堆栈交换网络的Software Recommendations 站点的主题。
标签: pandas time-series categories bokeh