pygal是个非常简单且功能丰富的py画图包,折线图、柱状图、饼图等常见和不常见的图像都可轻松实现。
解决 中文字体问题、Linux系统乱码问题,输出PNG文件问题
安装:
pip install pygal
文档特别详细:
http://www.pygal.org/en/stable/documentation/index.html
如下为官网折线图实例:
line_chart = pygal.Line()
line_chart.title = 'Browser usage evolution (in %)'
line_chart.x_labels = map(str, range(2002, 2013))
line_chart.add('Firefox', [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1])
line_chart.add('Chrome', [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3])
line_chart.add('IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])
line_chart.add('Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5])
chart.render() # Return the svg as bytes
- 设置x轴数据:line_chart.x_labels
- y轴数据:line_chart.add
- 绘图:line_chart.render()
- 保存输出:
bytes:chart.render() # Return the svg as bytes
svg文件:chart.render_to_file('/tmp/chart.svg')
png图片:chart.render_to_png('/tmp/chart.png')
pygal对输出图片,及中文不够友好,需要引入其他依赖包。
pip install webencodings
pip install tinycss2
pip install defusedxml
pip install cssselect2
pip install cairocffi
pip install CairoSVG
如果是linux环境安装cairo容易失败,可以下载对应的系统包进行安装:
https://launchpad.net/ubuntu/+source/pycairo/
我的ubuntu环境下载的:
python3-cairo_1.16.2-1_amd64.deb
安装:dpkg -i python3-cairo_1.16.2-1_amd64.deb
linux系统上输出 png会有乱码:
解决:windows的 C:\Windows\Fonts 下的微软雅黑.ttf文件copy到 linux的/usr/share/fonts目录
如下为我的一段代码,将字体改成微软,同时输出png:
#####################
# 折线图:
# 兼容linux 系统 png文字乱码:
# windows C:\Windows\Fonts 下的微软雅黑.ttf文件copy到 linux/usr/share/fonts目录
#####################
import pygal
import cairo
from pygal.style import Style
from datetime import datetime
colors_dict = {
1:'#009688', #
2:'#F44336', #
3:'#3F51B5', #
}
def draw_line(data, x_label, y_title, title,colors_list):
colors = []
for idx in colors_list:
if idx in colors_dict:
colors.append(colors_dict[idx])
else:
colors.append(colors_dict[1])
colors = tuple(colors)
custom_style = Style(
background='#FFFFFF',
plot_background='#FFFFFF',
value_background='rgba(229, 229, 229, 1)',
foreground='rgba(0, 0, 0, .87)',
foreground_strong='rgba(0, 0, 0, 1)',
foreground_subtle='rgba(0, 0, 0, .54)',
# Monospaced font is highly encouraged
font_family=(
'"微软雅黑","华文细黑",Arial, Helvetica, sans-serif'),
label_font_family=None,
major_label_font_family=None,
value_font_family=None,
value_label_font_family=None,
tooltip_font_family=None,
title_font_family=None,
legend_font_family=None,
no_data_font_family=None,
label_font_size=20,
major_label_font_size=20,
value_font_size=20,
value_label_font_size=20,
tooltip_font_size=14,
title_font_size=30,
legend_font_size=16,
no_data_font_size=64,
# Guide line dash array style
guide_stroke_dasharray='6,1',
major_guide_stroke_dasharray='6,1',
opacity='.7',
opacity_hover='.8',
stroke_opacity='.8',
stroke_opacity_hover='.9',
transition='150ms',
colors=colors,
value_colors=(),
ci_colors=())
chart = pygal.Line(fill=True, style=custom_style,title=title,y_title=y_title,
x_label_rotation=20, show_legend=False,width=1600,print_values=True,
print_zeroes=False, print_values_position='top',
show_minor_x_labels=False)
chart.x_labels = x_label
chart.x_labels_major = x_label[::5]
for i in data:
chart.add(i[0], i[1])
return chart
if __name__=="__main__":
data = [('IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])]
x_label = ['2019-07-07', '2019-07-08', '2019-07-12', '2019-07-14', '2019-07-15','2019-07-16', '2019-07-17', '2019-07-18', '2019-07-19', '2019-07-20', '2019-07-21']
print(x_label)
y_title = "嘿嘿嘿"
title = "哈哈哈哈"
save_path = 'test.png'
chart = draw_line(data, x_label, y_title, title,[1])
chart.render_to_png(save_path)