• Python3 数据可视化之matplotlib、Pygal、requests


    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 一个简单的代码段:

    1. import json  #导入json模版  
    2. filename = 'population_data.png'  
    3. with open(filename) as f:  
    4.      pop_data = json.load(f)  #加载json文件数据  

    通过小的代码段了解最基本的原理,具体详情还要去查看手册。

    4.3制作简单的世界地图(代码如下)

    1. import pygal  #导入pygal  
    2.   
    3. wm = pygal.maps.world.World()  #正确导入世界地图模块  
    4. wm.title = 'populations of Countries in North America'  
    5. wm.add('North America',{'ca':34126000,'us':309349000,'mx':113423000})  
    6.   
    7. wm.render_to_file('na_populations.svg')  #生成svg文件  

    结果:

    4.4 制作世界地图

    代码段:

    1. import json  
    2.   
    3. import pygal  
    4. from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS  
    5.   
    6. from country_codes import get_country_code  
    7.   
    8. # Load the data into a list.  
    9. filename = 'population_data.json'  
    10. with open(filename) as f:  
    11.     pop_data = json.load(f)  
    12.   
    13. # Build a dictionary of population data.  
    14. cc_populations = {}  
    15. for pop_dict in pop_data:  
    16.     if pop_dict['Year'] == '2010':  
    17.         country_name = pop_dict['Country Name']  
    18.         population = int(float(pop_dict['Value']))  
    19.         code = get_country_code(country_name)  
    20.         if code:  
    21.             cc_populations[code] = population  
    22.   
    23. # Group the countries into 3 population levels.  
    24. cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {}  
    25. for cc, pop in cc_populations.items():  
    26.     if pop < 10000000:                #分组  
    27.         cc_pops_1[cc] = pop  
    28.     elif pop < 1000000000:  
    29.         cc_pops_2[cc] = pop  
    30.     else:  
    31.         cc_pops_3[cc] = pop  
    32.   
    33. # See how many countries are in each level.          
    34. print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))  
    35.   
    36. wm_style = RS('#336699', base_style=LCS)  
    37. wm = pygal.maps.world.World(style=wm_style)   #已修改,原代码有错误!  
    38. wm.title = 'World Population in 2010, by Country'  
    39. wm.add('0-10m', cc_pops_1)  
    40. wm.add('10m-1bn', cc_pops_2)  
    41. wm.add('>1bn', cc_pops_3)  
    42.       
    43. wm.render_to_file('world_population.svg')  

    辅助代码段country_code.py如下:

      1. from pygal.maps.world import COUNTRIES  
      2. from pygal_maps_world import i18n      #原代码也有错误,现已订正  
      3.   
      4. def get_country_code(country_name):  
      5.     """Return the Pygal 2-digit country code for the given country."""  
      6.     for code, name in COUNTRIES.items():  
      7.         if name == country_name:  
      8.             return code  
      9.     # If the country wasn't found, return None.  
      10.     return None  

    监视API的速率限制

    大多数API都存在速率限制,即你在特定时间内可执行的请求数存在限制。要获悉你是否接近了GitHub的限制,请在浏览器中输入https://api.github.com/rate_limit ,你将看到类似于下 面的响应:

    这里写图片描述

    参考内容:《Python编程:从入门到实践》

  • 相关阅读:
    计数问题
    自定义中间件
    中间件的数据流向
    模块化
    开发属于自己的包
    中间件
    java JDK环境变量配置
    uni-app 请求 uni.request封装使用
    uni-app 自定义 简单 底部tab
    vue 过滤器 filter 的使用
  • 原文地址:https://www.cnblogs.com/meng-wei-zhi/p/8282493.html
Copyright © 2020-2023  润新知