你所感受到的壓力都是來自己不努力不積極而又不甘于現狀的恐慌
繪制條形圖
chart=pygal.Bar(style=**,x_label_rotation=角度,show_legend=False/True)
chart是pygal的執行個體,下文以chart為例。
注:Bar首字母必須大寫;style為樣式,x_label_rotation旋轉角度(順時針旋轉);show_legend是否顯示圖例
增加标題
添加X軸标簽
chart.x_labels=清單名
注:此處與matplotlib中chart.xlabel()不同。
pygal圖表配置對象
#可視化
my_style=LS('#333366',base_style=LCS)
#表格式配置檔案
my_config=pygal.Config()#配置對象
my_config.x_label_rotation=- #x軸标簽旋轉角度
my_config.show_legend=False #是否展示圖例
my_config.title_font_size= #圖表标題字型大小
my_config.label_font_size= #副标簽字型大小
my_config.major_label_font_size= #主标簽字型大小
my_config.truncate_label= #将裁剪較長字元到15個
my_config.show_y_guides=False #是否展示圖示中水準線
my_config.width= #自定義圖表寬度
chart=pygal.Bar(my_config,style=my_style) #繪制條形圖
chart.title='Most-Starred Python Projects on Github' #圖表名稱
chart.x_labels=names #圖表X軸标簽
chart.add('',stars) #增加資料系列
chart.render_to_file('python_repos.svg')
注:第14行代碼my_config與style的順序不能颠倒,my_config必須在前,窦否則将會報錯。
添加自定義工具提示
在pygal中,将滑鼠指向條形将顯示它表示的資訊,這稱為工具提示。
#提取倉庫名稱及其星數
names,plot_dicts=[],[] #兩者都是清單,隻不過plot_dicts是字典清單
for repo_dict in repo_dicts:
name=repo_dict['name']
names.append(name)
plot_dict={ #字典清單
'value':repo_dict['stargazers_count'], #增加y值
'label':str(repo_dict['description']) #增加描述
}
plot_dicts.append(plot_dict)
#可視化
my_style=LS('#333366',base_style=LCS)
chart=pygal.Bar(style=my_style,x_label_rotation=,label_legend=False)
chart.title='chart Bar pygal'
chart.x_labels=names
chart.add('',plot_dicts)
chart.render_to_file('python.svg')
圖表中添加可點選的連結
#提取倉庫名稱及其星數
names,plot_dicts=[],[] #兩者都是清單,隻不過plot_dicts是字典清單
for repo_dict in repo_dicts:
name=repo_dict['name']
names.append(name)
plot_dict={ #字典清單
'value':repo_dict['stargazers_count'], #增加y值
'label':str(repo_dict['description']), #增加描述
'xlink':repo_dict['html_url'] #增加超連結
}
plot_dicts.append(plot_dict)
#可視化
my_style=LS('#333366',base_style=LCS)
chart=pygal.Bar(style=my_style,x_label_rotation=,label_legend=False)
chart.title='chart Bar pygal'
chart.x_labels=names
chart.add('',plot_dicts)
chart.render_to_file('python1.svg')
使用API抓取GitHub
import requests
import json
import pygal
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
#執行API調用并儲存相應
url='https://api.github.com/search/repositories?q=language:python&sort=stars'
r=requests.get(url)
print("Status_code: ",r.status_code)
#将API響應儲存在一個變量中
response_dict=r.json()
#處理結果
print(response_dict.keys())
#響應個數
print("total_count:",response_dict['total_count'])
#探索有關倉庫的資訊
repo_dicts=response_dict['items']
print("Repositories returned:",len(repo_dicts)) #倉庫的個數
#研究第一個倉庫
repo_dict=repo_dicts[]
print("\nKeys:",len(repo_dict))
#提取倉庫名稱及其星數
names,plot_dicts=[],[] #兩者都是清單,隻不過plot_dicts是字典清單
for repo_dict in repo_dicts:
name=repo_dict['name']
names.append(name)
plot_dict={ #字典清單
'value':repo_dict['stargazers_count'], #增加y值
'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=- #x軸标簽旋轉角度
my_config.show_legend=False #是否展示圖例
my_config.title_font_size= #圖表标題字型大小
my_config.label_font_size= #副标簽字型大小
my_config.major_label_font_size= #主标簽字型大小
my_config.truncate_label= #将裁剪較長字元到15個
my_config.show_y_guides=False #是否展示圖示中水準線
my_config.width= #自定義圖表寬度
chart=pygal.Bar(my_config,style=my_style) #繪制條形圖
chart.title='Most-Starred Python Projects on Github' #圖表名稱
chart.x_labels=names #圖表X軸标簽
chart.add('',plot_dicts) #增加資料系列
chart.render_to_file('python_repos.svg')