from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS names, stars, plot_dicts = [], [], [] for repo_dict in repo_dicts: names.append(repo_dict['name']) stars.append(repo_dict['stargazers_count']) plot_dict = { 'value': repo_dict['stargazers_count'], 'label': str(repo_dict['description']), 'xlink': repo_dict['html_url'], } plot_dicts.append(plot_dict) # 可视化 my_style = LS('#333366', base_style=LCS) my_config = pygal.Config() my_config.x_label_rotation = 45 my_config.show_legend = False 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(my_config, style=my_style) chart.title = 'Most-Starred Python Project on Github' chart.x_labels = names # chart.add('', stars) chart.add('', plot_dicts) chart.render_to_file('python_repos.svg')
names = [] plot_dicts = [] for repo_dict in repo_dicts: names.append(repo_dict["name"]) plot_dict = { "value": repo_dict["stargazers_count"], "lable": repo_dict["description"], "xlink": repo_dict["html_url"] } plot_dicts.append(plot_dict) print(plot_dict) #数据可视化 my_style = LS('#333366', base_style=LCS) #设置基色为深蓝色,并以LCS作为基本样式 my_config = pygal.Config() #创建config的实例,通过修改config的属性来 my_config.x_label_rotation = 45 #x轴逆时针旋转45度 my_config.show_legend = False #隐藏图例 my_config.show_minor_y_labels = True my_config.title_font_size = 24 #设置图表标题的字体大小 my_config.lable_font_size = 14 #副标前 my_config.major_lable_font_size = 18 #主标签 my_config.truncate_label = 15 #截断字符串,可能由于x标签太长,因此采取截断处理 my_config.show_y_guides = False #不显示y轴的虚线 my_config.width = 1000 #设置宽度,从分利用浏览器页面 my_config.dynamic_print_values = True my_config.explicit_size = False #给出了图表明确的大小 my_config.height = 600 my_config.include_x_axis = True my_config.inner_radius = 3 my_config.inverse_y_axis = False #颠倒y轴的数据
# if repo_dict['description'].encode('utf-8').find('—'): # 默认ascii编码,不encode一下,会找不到— # repo_dict['description'] = repo_dict['description'].encode('utf-8').replace('—', '--') plot_dict = { 'value': repo_dict['stargazers_count'], 'label': repo_dict['description'] or '', #或者写成三元表达式https://segmentfault.com/q/1010000012917291 'fork': repo_dict['forks_count'], # 自定义的key没能显示出来 'xlink': repo_dict['html_url'], } plot_dicts.append(plot_dict) # 可视化 my_style = LS('#333366', base_style=LCS) my_config = pygal.Config() # 定制图的外观 my_config.x_label_rotation = 45 my_config.show_legend = False my_config.title_font_size = 24 my_config.label_font_size = 14 # 副标签字大小 my_config.major_label_font_size = 18 # 主标签字大小 my_config.truncate_label = 15 # 将较长项目名缩短为15个字符(鼠标移上去会完整显示) my_config.show_y_guides = False # y轴参考虚线 my_config.width = 1000 # 图表宽度 chart = pygal.Bar(my_config, style=my_style) # legend即左侧小方块 chart.title = 'Most-Starred Python Projects on GitHub' chart.x_labels = names chart.add('', plot_dicts) # 就是左边的小方块后的字,为空即不显示 chart.render_to_file('diagram/python_repos.svg')
names = [] plt_dicts = [] for repo_dict in repo_dicts: names.append(repo_dict['name']) plt_dict = { 'value': repo_dict['stargazers_count'], 'label': str(repo_dict['description']), # 添加连接 'xlink': repo_dict['html_url'] } plt_dicts.append(plt_dict) # 可视化 my_style = LS('#333366', base_style=LCS) my_config = pygal.Config() # 用于定制图表的外观 my_config.x_label_rotation = 45 # 标签绕 x 轴旋转 45 度 my_config.show_legend = False # 隐藏图例 my_config.title_font_size = 24 # 设置图表标题的字体大小 my_config.label_font_size = 14 # 设置图副标签的字体大小 my_config.major_label_font_size = 18 # 设置主标签的字体大小 my_config.truncate_label = 15 # 仅显示 15 个字符 my_config.show_y_guides = False # 隐藏图表中的水平线 my_config.width = 1000 # 设置自定义宽度 chart = pygal.Bar(my_config, style=my_style) chart.title = 'Most-Starred Python projects on Github' chart.x_labels = names chart.add('', plt_dicts) chart.render_to_file('Python_repos.svg')
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False) chart.title = 'Python Projects' chart.x_labels = ['httpie', 'django', 'flask'] plot_dicts = [ {'value': 16101, 'label': 'Description of httpie.'}, {'value': 15028, 'label': 'Description of django.'}, {'value': 14798, 'label': 'Description of flask.'}, ] chart.add('', plot_dicts) chart.render_to_file('bar_descriptions.svg') my_config = pygal.Config()#instance of pygals config class my_config.x_label_rotation = 45 my_config.show_legend = False#plotting only one series on chart my_config.title_font_size = 24 my_config.label_font_size = 14 my_config.major_label_font_size = 18 my_config.truncate_label = 15#shorten longer names to 15 characters my_config.show_y_guides = False#hide horizontal lines my_config.width = 1000 chart = pygal.Bar(my_config, style=my_style) chart.title = 'Most-Starred Python Projects on GitHub' chart.x_labels = names chart.add('', plot_dicts) chart.render_to_file('python_repos.svg')
else: desc = '' plot_dict = { 'value': repo_dict['stargazers_count'], 'label': desc, # label标签表示自定义柱状图中的工具提示 'xlink': repo_dict['html_url'], # 在图表中添加柱状图可点击的链接, 这里也应该做一个是否为空的判断 } plot_dicts.append(plot_dict) my_style = LS('#333366', base_style=LCS) # 定义一种绘图的基色 # style指定绘图的基色, x_label_rotation指定x坐标轴标签旋转度数, show_legend决定是否显示图例 # chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False) # 将图表的所有配置集中到config类 my_config = pygal.Config() # 定义config对象, 用于保存图表的修改 my_config.x_label_rotation = 45 # x轴的标签旋转45度 my_config.show_legend = False # 不显示图例 # 在这个图表中, 副标签是x轴上的项目名以及y轴上的大部分数字. # 主标签是y轴上为5000整数倍的刻度; 这些标签应更大, 以与副标签区分开来 my_config.title_font_size = 24 # 图表的标题为24号字体 my_config.label_font_size = 14 # 图表的副标题为14号字体, 副标题? 不是标签? my_config.major_label_font_size = 18 # 图表主标签为18号字体 my_config.truncate_label = 15 # 将较长的项目名缩短为15个字符 my_config.show_y_guides = False # 隐藏图表中的水平线 my_config.width = 1000 # 自定义图表的宽度, 让图表更充分的利用浏览器空间 chart = pygal.Bar(my_config, style=my_style) chart.title = 'GitHub上最多星标的Python项目'