Python 学习 - 可视化数据操作(一)

  GitHub:https://github.com/liqingwen2015/my_data_view

目录

  • 折线图
  • 散点图
  • 随机漫步
  • 骰子点数概率
  • 文件目录

折线图

  cube_squares.py

  1. import matplotlib.pyplot as plt
  2.  
  3. x_values=list(range(1, 5000))
  4. y_values=[pow(x, 3) for x in x_values]
  5.  
  6. plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolor='none', s=40)
  7.  
  8. # 设置标题和样式
  9. plt.title("Square Numbers", fontsize=24)
  10. plt.xlabel("Value", fontsize=14)
  11. plt.ylabel("Square of Value", fontsize=14)
  12.  
  13. # 设置刻度标记的大小
  14. plt.tick_params(axis='both', which='major', labelsize=14)
  15.  
  16. plt.show()

  mpl_squares.py

  1. # 简单的折线图
  1. import matplotlib.pyplot as plt
  2.  
  3. input_values=[1, 2, 3, 4, 5 ]
  4. squares = [1, 4, 9, 16, 25]
  5.  
  6. # 绘制线条的粗细
  7. plt.plot(input_values, squares, linewidth=5)
  8.  
  9. # 设置图表标题,并给坐标轴加上标签
  10. plt.title("Square Numbers", fontsize=24)
  11. plt.xlabel("Value", fontsize=14)
  12. plt.ylabel("Square of Value", fontsize=14)
  13.  
  14. # 设置刻度标记的大小,axis='both' 表示指定的实参影响 x 轴和 y 轴上的刻度
  15. plt.tick_params(axis='both', labelsize=14)
  16.  
  17. plt.show()

散点图

  scatter_squares.py

  1. # 散点图
  2.  
  3. import matplotlib.pyplot as plt
  4.  
  5. x_values = list(range(1, 1001))
  6. y_values = [x**2 for x in x_values]
  7.  
  8. # c:颜色
  9. #plt.scatter(x_values, y_values, c='red', edgecolor='none', s=40)
  10. #plt.scatter(x_values, y_values, c=(0, 0, 8), edgecolor='none', s=40)
  11. plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolor='none', s=40)
  12.  
  13. # 设置标题和样式
  14. plt.title("Square Numbers", fontsize=24)
  15. plt.xlabel("Value", fontsize=14)
  16. plt.ylabel("Square of Value", fontsize=14)
  17.  
  18. # 设置刻度标记的大小
  19. plt.tick_params(axis='both', which='major', labelsize=14)
  20.  
  21. plt.show()
  22.  
  23. # 保存图表
  24. #plt.savefig('squared_plot.png', bbox_inches='tight')

随机漫步

  random_walk.py

  1. from random import choice
  2.  
  3. class RandomWalk():
  4.  
  5. def __init__(self, num_points=5000):
  6. # 初始化随机漫步的属性
  7. self.num_points = num_points
  8.  
  9. # 所有随机漫步都始于(0, 0)
  10. self.x_values = [0]
  11. self.y_values = [0]
  12.  
  13. def fill_walk(self):
  14.  
  15. # 不断漫步,直到列表达到指定的长度
  16. while len(self.x_values) < self.num_points:
  17. x_step = self.get_step();
  18. y_step = self.get_step();
  19.  
  20. # 拒绝原地踏步
  21. if x_step == 0 and y_step == 0:
  22. continue
  23.  
  24. # 计算下一个点的 x 和 y 值
  25. next_x = self.x_values[-1] + x_step
  26. next_y = self.y_values[-1] + y_step
  27.  
  28. self.x_values.append(next_x)
  29. self.y_values.append(next_y)
  30.  
  31. def get_step(self):
  32. # 决定前进方向以及沿这个方向前进的距离
  33. direction = choice([1, -1]) # 随机选 1 或 -1
  34. distance = choice([0, 1, 2, 3, 4]) # 随机选 0, 1, 2, 3, 4
  35.  
  36. return direction * distance # 正数:右移,负数:左移

  rw_visual.py

  1. import matplotlib.pyplot as plt
  2.  
  3. from 随机漫步.random_walk import RandomWalk
  4.  
  5. while True:
  6. # 创建一个 RandomWalk 实例,并将其包含的点都绘制出来
  7. rw = RandomWalk(5000)
  8. rw.fill_walk()
  9.  
  10. point_numbers = list(range(rw.num_points))
  11. plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolors='none', s=1)
  12.  
  13. # 设置绘图窗口的尺寸
  14. #plt.figure(dpi=128, figsize=(10, 6))
  15.  
  16. # 突出起点和终点
  17. plt.scatter(0, 0, c='green', edgecolors='none', s=100)
  18. plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none', s=100)
  19.  
  20. #plt.plot(rw.x_values, rw.y_values, linewidth=10)
  21.  
  22. # 隐藏坐标轴
  23. plt.axes().get_xaxis().set_visible(False)
  24. plt.axes().get_yaxis().set_visible(False)
  25.  
  26. plt.show()
  27.  
  28. keep_running = input("继续?(y/n):")
  29. if keep_running == 'n':
  30. break

骰子点数概率

  die.py

  1. from random import randint
  2.  
  3. class Die():
  4. # 表示一个骰子类
  5.  
  6. def __init__(self, num_sides=6):
  7. # 6 面
  8. self.num_sides = num_sides
  9.  
  10. def roll(self):
  11. # 返回 1~6
  12. return randint(1, self.num_sides)

  

  die_visual.py

  1. import pygal
  2.  
  3. from 骰子.die import Die
  4.  
  5. # 创建一个 D6
  6. die = Die()
  7.  
  8. results = []
  9. for roll_num in range(1000):
  10. result = die.roll()
  11. results.append(result)
  12.  
  13. frequencies = []
  14. for value in range(1, die.num_sides+1):
  15. # 计算某个值出现同样的次数
  16. frequency = results.count(value)
  17. frequencies.append(frequency)
  18.  
  19. # 对结果进行可视化
  20. hist = pygal.Bar()
  21.  
  22. hist.title = "D6 1000次:"
  23. hist.x_labels = [str(num) for num in range(1, 7)] #['1', '2', '3', '4', '5', '6']
  24. hist.x_title = "结果"
  25. hist.y_title = "概率"
  26.  
  27. hist.add('D6', frequencies)
  28. hist.render_to_file('images/die_visual.svg')

  dice_visual.py

  1. import pygal
  2.  
  3. from 骰子.die import Die
  4.  
  5. # 创建 2 个 D6
  6. die_1 = Die()
  7. die_2 = Die()
  8.  
  9. results = []
  10. for roll_num in range(1000):
  11. result = die_1.roll() + die_2.roll()
  12. results.append(result)
  13.  
  14. frequencies = []
  15. max_results = die_1.num_sides + die_2.num_sides
  16. for value in range(2, max_results+1):
  17. # 计算某个值出现同样的次数
  18. frequency = results.count(value)
  19. frequencies.append(frequency)
  20.  
  21. # 对结果进行可视化
  22. hist = pygal.Bar()
  23.  
  24. hist.title = "D6 100次:"
  25. hist.x_labels = [str(num) for num in range(1, 13)] #['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
  26. hist.x_title = "结果"
  27. hist.y_title = "出现的次数"
  28.  
  29. hist.add('D6 + D6', frequencies)
  30. hist.render_to_file('images/dice_visual.svg')

  different_dice.py

  1. import pygal
  2.  
  3. from 骰子.die import Die
  4.  
  5. # 创建一个 D6 和 D10
  6. die_1 = Die()
  7. die_2 = Die(10)
  8.  
  9. results = []
  10. for roll_num in range(5000):
  11. result = die_1.roll() + die_2.roll()
  12. results.append(result)
  13.  
  14. frequencies = []
  15. max_results = die_1.num_sides + die_2.num_sides
  16. for value in range(2, max_results+1):
  17. # 计算某个值出现同样的次数
  18. frequency = results.count(value)
  19. frequencies.append(frequency)
  20.  
  21. # 对结果进行可视化
  22. hist = pygal.Bar()
  23.  
  24. hist.title = "5000 次:D6 + D10 的结果。"
  25. hist.x_labels = [str(num) for num in range(2, 17)]
  26. hist.x_title = "结果"
  27. hist.y_title = "重复出现的次数"
  28.  
  29. hist.add('D6 + D10', frequencies)
  30. hist.render_to_file('images/different_visual.svg')

文件目录

  GitHub:https://github.com/liqingwen2015/my_data_view

[Python] Python 学习 - 可视化数据操作(一)的更多相关文章

  1. python入门学习:3.操作列表

    python入门学习:3.操作列表 关键点:列表 3.1 遍历整个列表3.2 创建数值列表3.3 使用列表3.4 元组 3.1 遍历整个列表   循环这种概念很重要,因为它是计算机自动完成重复工作的常 ...

  2. Python进阶学习_连接操作Redis数据库

    安装导入第三方模块Redis pip3 install redis import redis 操作String类型 """ redis 基本命令 String set(n ...

  3. linux学习之——数据操作:添加与查询

    说明: 在linux系统中,利用搭建的服务器,编写两个页面,一个添加信息,一个展现信息: 主要涉及到:php+mysql的操作: 数据添加页面: <html> <head> & ...

  4. python基础学习之文件操作&函数

    1.文件处理相关 1.编码问题 ①python2与python3中的默认编码: py2默认使用ASCII码,py3默认使用utf-8 ②为什么会出现中文乱码,中文乱码的情况有哪些? #sys.stdo ...

  5. Python基础学习七 Excel操作

    python操作excel,python操作excel使用xlrd.xlwt和xlutils模块, xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的. ...

  6. python基础学习笔记——文件操作

    文件操作 初始文件操作 使用Python来读写文件是非常简单的操作,我们使用open()函数来打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了 根据打开方式的不同能够执行的操作 ...

  7. python自动化测试学习笔记-6excel操作xlwt、xlrd、xlutils模块

    python中通过xlwt.xlrd和xlutils操作xls xlwt模块用于在内存中生成一个xls/xlsx对象,增加表格数据,并把内存中的xls对象保存为本地磁盘xls文件; xlrd模块用于把 ...

  8. [python][django学习篇][6]操作数据库

    查询(取)数据 >>> Category.objects.all() <QuerySet [<Category: Category object>]> > ...

  9. 莫烦python教程学习笔记——数据预处理之normalization

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

随机推荐

  1. Java_流程控制

    介绍: java的流程控制结构有三种:顺序.选择.循环            顺序结构,就是从头到尾依次执行每条语句的操作. 选择结构,也称条件控制,是指根据表达式的值有选择的执行. 循环结构,也称回 ...

  2. python多版本管理工具(pyenv)

    在学习和利用python开发的很多情况下,需要多版本的Python并存.此时需要在系统中安装多个Python,但又不能影响系统自带的 Python.pyenv 就是这样一个 Python 版本管理器. ...

  3. swust oj 1012

    哈希表(链地址法处理冲突) 1000(ms) 10000(kb) 2542 / 6517 采用除留余数法(H(key)=key %n)建立长度为n的哈希表,处理冲突用链地址法.建立链表的时候采用尾插法 ...

  4. 美图App的移动端DNS优化实践:HTTPS请求耗时减小近半

    本文引用了颜向群发表于高可用架构公众号上的文章<聊聊HTTPS环境DNS优化:美图App请求耗时节约近半案例>的部分内容,感谢原作者. 1.引言 移动互联网时代,APP 厂商之间的竞争非常 ...

  5. SDL 开发实战(二):SDL 2.0 核心 API 解析

    在上一篇文章 SDL 开发实战(一):SDL介绍及开发环境配置 中,我们配置好了SDL的开发环境,并成功运行了SDL的Hello World 代码.但是可能大部分人还是读不太明白具体Hello Wol ...

  6. HttpSession的API

    //获取Session对象request.getSession()request.getSession(boolean create)//获取SessionIdgetId()//获取当前session ...

  7. [Swift]LeetCode306. 累加数 | Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  8. [Swift]LeetCode328. 奇偶链表 | Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  9. [Swift]LeetCode605. 种花问题 | Can Place Flowers

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  10. Docker for windows : 安装linux

    一.Linux 1.拉取镜像 docker pull hub.c..com/library/oraclelinux:latest 2.创建linux容器 C:\Users\K-Jso>docke ...