从两个列表创建一个 json,以在 Javascript 中制作堆叠条形图
list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02-13', '2019-02-12', '2019-02-12', '2019-02-11', '2019-02-08', '2019-02-07', '2019-02-06', '2019-02-05', '2019-02-05', '2019-02-02', '2019-02-02', '2019-01-29', '2019-01-28', '2019-01-24', '2019-01-24', '2019-01-24', '2019-01-23', '2019-01-16', '2019-01-16', '2019-01-14', '2019-01-10', '2019-01-10', '2019-01-09', '2019-01-06', '2019-01-05', '2019-01-05', '2019-01-01']
list2 =['Type Error', 'Type Error', 'Type Error', 'Type Error', 'Segmenatation', 'Type Error', 'Type Error', 'Heap Failure', 'Heap Failure', 'Type Error', 'I/O Error', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error', 'Type Error', 'Heap Failure', 'Type2 Error', 'Heap I/O', 'CPU Recovery Error', 'Type Error', 'Type Error', 'CPU Recovery Error', 'CPU Recovery Error', 'Heap I/O', 'Type2 Error', 'Heap Failure', 'I/O Error', 'Heap I/O', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error']
由此,我需要在下面提供的链接 https://plot.ly/javascript/bar-charts/#stacked-bar-chart 中绘制图表
我尝试使用默认的字典并获取单键多个值,但链接中的 json 非常不同。我正在使用 Python 编写脚本
请帮助我。
尝试过,但没有结果
from collections import defaultdict
v = defaultdict(list)
for i in range(len(list1)):
v[list1[i]].append(list2[i])
最佳答案
对于堆叠条形图,Plotly 期望每种数据类型都位于单独的跟踪中。在您的情况下,您需要为每种错误类型创建一个条形图(假设您想要 x 轴上的日期,y 轴是错误计数)。
对于小列表,您可以循环遍历它们并搜索匹配项。对于更大的数据集,Pandas(或类似的东西)应该具有更高的性能并且更易于使用。
import collections
import plotly
list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02-13', '2019-02-12', '2019-02-12', '2019-02-11', '2019-02-08', '2019-02-07', '2019-02-06', '2019-02-05', '2019-02-05', '2019-02-02', '2019-02-02', '2019-01-29', '2019-01-28', '2019-01-24', '2019-01-24', '2019-01-24', '2019-01-23', '2019-01-16', '2019-01-16', '2019-01-14', '2019-01-10', '2019-01-10', '2019-01-09', '2019-01-06', '2019-01-05', '2019-01-05', '2019-01-01']
list2 =['Type Error', 'Type Error', 'Type Error', 'Type Error', 'Segmenatation', 'Type Error', 'Type Error', 'Heap Failure', 'Heap Failure', 'Type Error', 'I/O Error', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error', 'Type Error', 'Heap Failure', 'Type2 Error', 'Heap I/O', 'CPU Recovery Error', 'Type Error', 'Type Error', 'CPU Recovery Error', 'CPU Recovery Error', 'Heap I/O', 'Type2 Error', 'Heap Failure', 'I/O Error', 'Heap I/O', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error']
assert len(list1) == len(list2)
traces = []
# get a set of unique errors
for error in sorted(list(set(list2))):
errors = collections.defaultdict(int)
# check this particular error occurred on this date
for ix, d in enumerate(list1):
if list2[ix] == error:
errors[d] += 1
traces.append(plotly.graph_objs.Bar(x=list(errors.keys()),
y=list(errors.values()),
name=error))
layout = plotly.graph_objs.Layout(
barmode='stack'
)
fig = plotly.graph_objs.Figure(data=traces, layout=layout)
plotly.offline.plot(fig)
关于javascript - 从两个列表创建一个 json 以在 Javascript 中制作堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56512564/