简体   繁体   English

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

[英]How to plot multiple graphs in one chart using pygal?

I'm trying to plot multiple series with two measurements (so it's actually num_of_time_series x 2 graphs) in one figure using pygal. 我试图在一个图中使用pygal绘制多个系列,其中包含两个测量值(因此它实际上是num_of_time_series x 2个图形)。 For instance, suppose mt data is: 例如,假设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']

and the graph rendering code is that: 图形呈现代码是:

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()

The Current result is that . 当前结果是

The problem in the code above is that it's unclear which graph represents measurement 1 and which represents measurement 2. Second, I would like to see each component in a different color(or shape). 上面代码中的问题是,不清楚哪个图表代表测量1,哪个代表测量2.其次,我希望看到每个组件的颜色(或形状)不同。

This graph aims to compare one component versus the two others, and to see the correlation between measurement 1 and 2. 该图旨在比较一个组件与另外两个组件,并查看测量1和2之间的相关性。

Thanks for the help guys! 谢谢你的帮助!

I figured out how to distinguish the compared component by a dashed line. 我想出了如何用虚线区分比较的组件。 The code should look like that: 代码看起来应该是这样的:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM