python-pygal画图
pygal是个非常简单且功能丰富的py画图包,折线图、柱状图、饼图等常见和不常见的图像都可轻松实现。 解决 中文字体问题、Linux系统乱码问题,输出PNG文件问题安装: 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,16.6,25,31,36.4,45.5,46.3,42.8,37.1]) line_chart.add('Chrome',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,5.8,6.7,6.8,7.5]) chart.render() # Return the svg as bytes
pygal对输出图片,及中文不够友好,需要引入其他依赖包。 如果是linux环境安装cairo容易失败,可以下载对应的系统包进行安装: linux系统上输出 png会有乱码: 如下为我的一段代码,将字体改成微软,同时输出png: ##################### # 折线图: # 兼容linux 系统 png文字乱码: # windows C:WindowsFonts 下的微软雅黑.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,1)',foreground='rgba(0,.87)',foreground_strong='rgba(0,foreground_subtle='rgba(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,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',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,[1]) chart.render_to_png(save_path) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |