pygal
- 本文设计的代码适用于 pygal==2.4.0
- 不是 python 3.7 的标准库,需要安装
pip install pygal
条状图
```python import pygal from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
my_style = LS(‘#233333’, base_style=LCS)
对即将创建的图表进行设置
my_config = pygal.Config() my_config.x_label_rotation = 45 # 倾斜角度,水平放置位置不够,垂直看着又有点费劲 my_config.show_legend = True # 显示左上角的标记 my_config.title_font_size = 24 my_config.label_font_size = 14 my_config.major_label_font_size = 18 my_config.truncate_label = 15 my_config.show_y_guides = False my_config.width = 1000
创建一个图表
chart = pygal.Bar(config=my_config, style=my_style) chart.title = “Python Projects” chart.x_labels = [‘django’, ‘flask’]
准备一组模拟数据
plot_dicts = [ {‘value’: 15000, ‘label’: “Description of django”, ‘xlink’: “https://github.com/django/django"}, {‘value’: 14000, ‘label’: “Description of flask”, ‘xlink’: “https://github.com/pallets/flask"} ]
chart.add(title=’some value’, values=plot_dicts) chart.render_to_file(‘result.svg’)
```