多线图:坚持使用 json-dict 格式的线条

Multi-Line Graph: Stuck with the json-dict format for the lines

你好,我完全不熟悉使用 Python 进行数据可视化,我有这个 json 回复:

{
"max365": 83.87,
"current365": 83.87,
"min365": 75.29,

"max180": 76.94,
"current180": 76.94,
"min180": 56.43,

"max90": 98.66,
"current90": 98.66,
"min90": 63.29,

"max30": 138.14,
"current30": 136,
"min30": 66.77,

"max14": 156.93,
"current14": 122.88,
"min14": 72.56,

"max7": 168.9,
"current7": 122.68,
"min7": 74.08,

"max0": 267.5,
"current0": 81.28,
"min0": 36.07 }

maxcurrentmin 是我想在多线图上绘制的线,但我正在努力处理此 [=28= 中的数据] 格式与响应。

我在这里添加了一个图表的屏幕截图,我实际上是在尝试对其进行逆向工程:

我看过一些关于通用线图的有用帖子,但我这里的问题主要是将所有 max/current/mins 链接到它们自己的线上,而我有 0/7/14/30/90/180/ 365 grouping/intersecting 他们每个人都在回复中。

希望我已经解释得够清楚了。任何帮助将不胜感激。

Python 标准库有一个 json module,你只需要导入 loads 方法,稍微修改一下数据。

… 这是代码——我想强调的是,我们需要拆分标签以获取真实标签和序列信息(数字是时间吗?我做了一个有根据的猜测),所以对于每个标签,我们构建一个列表列表,每个元素一个时间和一个值,然后我们对每个标签列表中的列表进行排序,最后我们绘制三行。

 from matplotlib.pyplot import subplots
 from json import loads
 
 def split_num(s):
     num = []
     for c in reversed(s):
         if c.isdigit():
             num += c
         else:
             break
     if num:
         return s[:-len(num)], ''.join(reversed(num))
     else:
         return s, ''
 
 json = '''{
     "max365": 83.87,"current365": 83.87,"min365": 75.29,
     "max180": 76.94,"current180": 76.94,"min180": 56.43,
     "max90": 98.66,"current90": 98.66,"min90": 63.29,
     "max30": 138.14,"current30": 136,"min30": 66.77,
     "max14": 156.93,"current14": 122.88,"min14": 72.56,
     "max7": 168.9,"current7": 122.68,"min7": 74.08,
     "max0": 267.5,"current0": 81.28,"min0": 36.07 }'''
 jdict = loads(json)
 
 data = {}
 for k in jdict:
     name, num = split_num(k)
     data[name] = data.setdefault(name, []) + [[int(num), float(jdict[k])]]
 for k in data: data[k] = sorted(data[k])
 
 fig, ax = subplots()
 for k in data:
     ax.plot(*zip(*data[k]), label=k)
 ax.legend()
 fig.show()