从两个列表创建一个json以使用Javascript制作堆叠的条形图

时间:2019-06-09 06:51:24

标签: javascript python json dictionary plotly

从两个列表中创建一个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下面的链接中绘制图形

我尝试使用默认的dict并获取单键多个值,但是链接中的json却大不相同。我使用Python编写脚本

请帮助我。

尝试过,但没有结果

from collections import defaultdict
v = defaultdict(list)
for i in range(len(list1)):
  v[list1[i]].append(list2[i])

1 个答案:

答案 0 :(得分:0)

对于堆积的条形图,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)

enter image description here