如何使用pygal在一个图表中绘制多个图形?

10

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

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之间的相关性。

感谢大家的帮助!


如果您已经找到了问题的答案,请将其放在下面的“答案”部分中,而不是在问题段落本身中。 - Muhammad Nizami
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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接