Python编程:从入门到实践-项目2-数据可视化2-生成数据-Demo
程序员文章站
2022-03-22 16:49:50
...
前记:
python的项目文件使之不普通就必须加一个__init__.py的文件,这样解释程序才能正确识别以下的模块,不然会导致出错,python基本的import 语句:from [..\xxxxx\.] xx import xx [,x1,x2] as myname ;
项目目录:
D:.
│ test.txt
│ __init__.py
│
├─RandWalk
│ │ random_walk.py
│ │ rw-visual.py
│ │ __init__.py
│ │
│ └─__pycache__
└─zhi_sai_zi
│ dice_visual.svg
│ dice_visual_two.svg
│ die-visual
│ die.py
│ die_visual.py
│ two_die.py
│ __init__.py
│
└─__pycache__
预览:生成的可视化文件,格式:svg
rw-visual.py
'''rw_visual'''
import matplotlib.pyplot as plt
from Generate_data.RandWalk.random_walk import randomwalk
while True:
rw = randomwalk()
rw.fill_walk()
#设置窗口的大小,像素的大小
plt.figure(dpi = 128,figsize=(10, 6))
points_sum_num = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=points_sum_num, cmap =plt.cm.Blues,
edgecolors='none',s=15)
#突出起始点和终结点,也就是重新绘制
plt.scatter(0, 0, c='green', edgecolors='none', s=100)
plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none',
s=100)
#设置坐标轴的可见度
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
keep_on = input("Make another?(y/n):")
if keep_on =='n':
break
产生数据的基础代码,学会如何生成随机数据:
'''randomwalk''' from random import choice class randomwalk(): def __init__(self,num_points=500000): """初始化随机漫步的属性""" self.num_points = num_points #所有的随机漫步初始为(0,0) self.x_values = [0] self.y_values = [0] def fill_walk(self): """jisuan suoyou baohan de dian""" while len(self.x_values)<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 #计算下一个点 next_x = self.x_values[-1] +x_step next_y = self.y_values[-1] +y_step self.x_values.append(next_x) self.y_values.append(next_y)
此时我的数据量已经很大了,开始测试运行可以小一些,不然很卡:
下面是进一步的条状图分析生成的代码:
'''two die'''
from .die import Die
import pygal
# 创建一个D6和一个D10
die_1 = Die()
die_2 = Die(10)
# 掷骰子多次,并将结果存储在一个列表中
results = []
for roll_num in range(50000):
result = die_1.roll() + die_2.roll()
results.append(result)
# 分析结果
frequencies = []
for result in range(1, die_1.sides+1+die_2.sides):
frequency = results.count(result)
frequencies.append(frequency)
print(frequencies)
# 可视化结果
hist = pygal.Bar()
hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
'13', '14', '15', '16']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6 + D10', frequencies)
hist.render_to_file('dice_visual_two.svg')
die_visual生成图形
from Generate_data.zhi_sai_zi import Die
import pygal
die = Die()
res = []
for roll_num in range(100):
res_ = die.roll()
res.append(res_)
print(str(res)+'\n-----------Results---------------')
frequencys = []
for i in range(1,die.sides+1):
frequency = res.count(i)
frequencys.append(frequency)
print(frequencys)
hist = pygal.Bar()
hist.title = "result of 100 times rolling the sai-zi"
hist.x_labels = ['1','2','san','3','si','5','6']
hist.y_title = "sai-zi jie guo"
hist.x_title = "the bunber"
hist.add('D6',frequencys)
hist.render_to_file('die-visual')
下面一节是关于如何清洗和分析下载的数据,就是一些json,exel常用的文件格式的分析;
原著作者的网址可以参见:
想要书的资源关注我微信公众号嘛,也想涨涨人气和各路小仙吹吹牛交交友;
上一篇: mysql错误代码查询
下一篇: mysql如何设置默认编码为utf-8