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

问题描述 投票:9回答:1

我正在尝试使用两个测量来绘制多个系列(所以它实际上是使用pygal在一个图中的num_of_个时间序列* 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()

目前的结果是that

上面代码中的问题是,不清楚哪个图表属于测量1,哪个图表属于测量2.其次,我希望在两个测量中看到颜色(或形状)中的每个成分(现在看起来像他们根本没有关系。例如component1 -pink,component2-green,component3-yellow

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

我希望我很清楚。

谢谢你的帮助!

python python-2.7 charts data-science pygal
1个回答
0
投票

我想出了如何用虚线区分比较的组件。代码看起来应该是这样的:

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()
© www.soinside.com 2019 - 2024. All rights reserved.