10

我正在尝试使用 pygal 在一个图中绘制具有两个测量值的多个系列(因此它实际上是 num_of_time_series x 2 个图)。例如,假设 mt 数据是:

from collections import defaultdict

measurement_1=defaultdict(None,[
  ("component1", [11.83, 11.35, 0.55]), 
  ("component2", [2.19, 2.42, 0.96]),
  ("component3", [1.98, 2.17, 0.17])])

measurement_2=defaultdict(None,[
  ("component1", [34940.57, 35260.41, 370.45]),
  ("component2", [1360.67, 1369.58, 2.69]),
  ("component3", [13355.60, 14790.81, 55.63])])

x_labels=['2016-12-01', '2016-12-02', '2016-12-03']

图形渲染代码是:

from pygal import graph
import pygal
def draw(measurement_1, measurement_2 ,x_labels):
  graph = pygal.Line()
  graph.x_labels = x_labels

  for key, value in measurement_1.iteritems():
      graph.add(key, value)
  for key, value in measurement_2.iteritems():
      graph.add(key, value, secondary=True)

  return graph.render_data_uri()

当前的结果.

上面代码中的问题是不清楚哪个图表代表测量 1,哪个代表测量 2。其次,我希望看到每个组件具有不同的颜色(或形状)。

该图旨在比较一个组件与其他两个组件,并查看测量 1 和 2 之间的相关性。

谢谢你们的帮助!

4

1 回答 1

1

我想出了如何用虚线区分比较的组件。代码应如下所示:

from pygal import graph
import pygal

def draw(measurement_1, measurement_2 ,x_labels):
  graph = pygal.Line()
  graph.x_labels = x_labels

  for key, value in measurement_1.iteritems():
     ##
     if "component1":
        graph.add(key, value, stroke_style={'width': 5, 'dasharray': '3, 6', 'linecap': 'round', 'linejoin': 'round'})
     else:
     ##
        graph.add(key, value)
  for key, value in measurement_2.iteritems():
      graph.add(key, value, secondary=True)

  return graph.render_data_uri()
于 2017-12-18T13:03:16.393 回答