matplotlib的學習和使用
matplotlib的安裝
pip3 install matplotlib
簡單的折線圖
import matplotlib.pyplot as plt
#繪制簡單的圖表 input_values = [1,2,3,4,5] squares = [1,4,9,16,25] plt.plot(input_values,squares,linewidth=5) #設置圖表的標題 並給坐標軸加上標簽 plt.title("Square Number",fontsize=24) plt.xlabel("Value",fontsize=24) plt.ylabel("Square of Value",fontsize=14) #設置刻度標記的大小 plt.tick_params(axis='both',labelsize=14) #顯示圖表 plt.show() #保存在當前的目錄下,文件名為squares_plot.png #plt.savefig('squares_plot.png', bbox_inches='tight')

繪制簡單的散點圖
import matplotlib.pyplot as plt x_values = [1, 2, 3, 4, 5] y_values = [1, 4, 9, 16, 25] plt.scatter(x_values, y_values, s=100) #設置圖表的標題 並給坐標軸加上標簽 plt.title("Square Number",fontsize=24) plt.xlabel("Value",fontsize=24) plt.ylabel("Square of Value",fontsize=14) #設置刻度標記的大小 plt.tick_params(axis='both',labelsize=14) plt.show()

import matplotlib.pyplot as plt
#繪制散點圖並設置其樣式 x_value = list(range(1,1001)) y_value = [x**2 for x in x_value] #點的顏色 c=(0,0,1,0.5) edgecolors = 'red' 點的邊緣顏色 plt.scatter(x_value,y_value,c=y_value,cmap=plt.cm.Blues,edgecolors='none',s=40) # plt.scatter(2,4,s=200) #設置圖表的標題 並給坐標軸加上標簽 plt.title("Square Number",fontsize=24) plt.xlabel("Value",fontsize=24) plt.ylabel("Square of Value",fontsize=14) #設置刻度標記的大小 plt.tick_params(axis='both',labelsize=14) #設置每個坐標系的取值范圍 # plt.axis([0,110,0,110000]) #顯示 plt.show() #顯示並保存 #plt.savefig('pyplot_scatter.png',bbox_inches='tight')

繪制隨機漫步圖
random_walk.py
from random import choice class RandomWalk(): """一個生成隨機漫步數據的類""" def __init__(self,num_points=5000): """一個生成隨機漫步的數據的類""" self.num_points = num_points; #所有的隨機漫步都始於(0,0) self.x_value = [0] self.y_value = [0] def fill_walk(self): """計算隨機漫步包含的點""" #不斷漫步,直到列表達到指定的長度 while len(self.x_value) < self.num_points: #決定前進的方向以及沿這個方向前進的距離 x_direction= choice([1,-1]) x_distance = choice([0,1,2,3,4]) x_step = x_direction*x_distance y_direction = choice([1,-1]) y_distance = choice([0, 1, 2, 3, 4]) y_step = y_direction * y_distance #拒絕原地踏步 if x_step == 0 and y_step == 0: continue #計算下一個點的x和y值 next_x = self.x_value[-1] + x_step next_y = self.y_value[-1] + y_step self.x_value.append(next_x) self.y_value.append(next_y)
rw_visual.py
import matplotlib.pyplot as plt
#引用同級目錄下的文件 from Random_Walk.random_walk import RandomWalk #創建一個RandomWalk的實例 並將其包含的點都繪制出來 rw = RandomWalk() rw.fill_walk() print("test") point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_value,rw.y_value,c=point_numbers, cmap=plt.cm.Blues,edgecolor='none',s=15) # 突出起點和終點 plt.scatter(0, 0, c='green',edgecolors='none',s=100) plt.scatter(rw.x_value[-1], rw.y_value[-1],c='red',edgecolors='none',s=100) # 設置繪圖窗口的尺寸 # plt.figure(figsize=(10, 6)) plt.figure(dpi=128, figsize=(10, 6)) # 隱藏坐標軸 # plt.axes().get_xaxis().set_visible(False) # plt.axes().get_yaxis().set_visible(False) plt.show()
Pygal的學習和使用
安裝Pygal
pip3 install pygal
繪制簡單的直方圖
創建骰子類 die.py
from random import randint class Die(): """表示一個骰子的類""" def __init__(self,num_sides=6): """骰子默認為6面""" self.num_sides = num_sides def roll(self): """返回一個位於1和骰子面數之間的隨機值""" return randint(1,self.num_sides)
擲骰子die_visual.py
from Pygal_learn.die import Die import pygal #創建一個D6 die = Die() #擲幾次骰子 並將結果存儲在一個列表中 results = [] for roll_num in range(1000): result = die.roll() results.append(result) frequencies = [] #分析結果 for value in range(1,die.num_sides+1): frequency = results.count(value) frequencies.append(frequency) #對結果進行可視化 hist = pygal.Bar() hist.title = "Result of rolling one d6 1000 times" hist.x_labels = ['1','2','3','4','5','6'] hist.x_title = "Result" hist.y_title = "Frequency of result" hist.add("D6",frequencies) hist.render_to_file("die_visual.svg")

使用Web API
安裝requests
pip3 install requests
繪制圖表
通過抓取GitHub上受歡迎程度最高的Python項目,繪制出圖表
import requests
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("Staus code:",r.status_code) response_dict = r.json() print("Total repositories:", response_dict['total_count']) #探索有關倉庫的信息 repo_dicts = response_dict['items'] print('Repositories returned:',len(repo_dicts)) #研究第一個倉庫 # repo_dict = repo_dicts[0] # for key in sorted(repo_dict.keys()): # print(key) #研究倉庫有關的信息 # Name: macOS-Security-and-Privacy-Guide # Owner: drduh # Stars: 12348 # Repository: https://github.com/drduh/macOS-Security-and-Privacy-Guide # Description: A practical guide to securing macOS. names,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_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 my_style = LS('#333366',base_style=LCS) chart = pygal.Bar(my_config,style=my_style) chart.title = "Most-Stared Python Project on Github" chart.x_labels = names print(plot_dicts) chart.add('',plot_dicts) chart.render_to_file('python_repos.svg')

4 從json文件中提取數據,並進行可視化
4.1 數據來源:population_data.json。
4.2 一個簡單的代碼段:
- import json #導入json模版
- filename = 'population_data.png'
- with open(filename) as f:
- pop_data = json.load(f) #加載json文件數據
通過小的代碼段了解最基本的原理,具體詳情還要去查看手冊。
4.3制作簡單的世界地圖(代碼如下)
- import pygal #導入pygal
- wm = pygal.maps.world.World() #正確導入世界地圖模塊
- wm.title = 'populations of Countries in North America'
- wm.add('North America',{'ca':34126000,'us':309349000,'mx':113423000})
- wm.render_to_file('na_populations.svg') #生成svg文件
結果:
4.4 制作世界地圖
代碼段:
- import json
- import pygal
- from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS
- from country_codes import get_country_code
- # Load the data into a list.
- filename = 'population_data.json'
- with open(filename) as f:
- pop_data = json.load(f)
- # Build a dictionary of population data.
- cc_populations = {}
- for pop_dict in pop_data:
- if pop_dict['Year'] == '2010':
- country_name = pop_dict['Country Name']
- population = int(float(pop_dict['Value']))
- code = get_country_code(country_name)
- if code:
- cc_populations[code] = population
- # Group the countries into 3 population levels.
- cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {}
- for cc, pop in cc_populations.items():
- if pop < 10000000: #分組
- cc_pops_1[cc] = pop
- elif pop < 1000000000:
- cc_pops_2[cc] = pop
- else:
- cc_pops_3[cc] = pop
- # See how many countries are in each level.
- print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))
- wm_style = RS('#336699', base_style=LCS)
- wm = pygal.maps.world.World(style=wm_style) #已修改,原代碼有錯誤!
- wm.title = 'World Population in 2010, by Country'
- wm.add('0-10m', cc_pops_1)
- wm.add('10m-1bn', cc_pops_2)
- wm.add('>1bn', cc_pops_3)
- wm.render_to_file('world_population.svg')
輔助代碼段country_code.py如下:
- from pygal.maps.world import COUNTRIES
- from pygal_maps_world import i18n #原代碼也有錯誤,現已訂正
- def get_country_code(country_name):
- """Return the Pygal 2-digit country code for the given country."""
- for code, name in COUNTRIES.items():
- if name == country_name:
- return code
- # If the country wasn't found, return None.
- return None
監視API的速率限制
大多數API都存在速率限制,即你在特定時間內可執行的請求數存在限制。要獲悉你是否接近了GitHub的限制,請在瀏覽器中輸入https://api.github.com/rate_limit ,你將看到類似於下 面的響應:

