python数据可视化--matplotlib进行随机漫步
2022-04-17 本文已影响0人
Z_bioinfo
使用python模拟随机漫步数据,再使用matplotlib以引人注目的方式可视化
随机漫步是这样行走的路径,每次行走都完全是随机的,没有明确的方向,结果是由一系列随机决策决定的
#创建名为RandomWalk()类,模拟随机漫步
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])#向右走表示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)
#绘制随机漫步图
import matplotlib.pyplot as plt
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_value, rw.y_value, s =15)
plt.show()
image.png
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])#向右走表示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)
模拟多次随机漫步
import matplotlib.pyplot as plt
while True:
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_value, rw.y_value, s =15)
plt.show()
keep_running = input('make another walk?(y/n): ')
if keep_running == 'n':
break
image.png
make another walk?(y/n): y
image.png
make another walk?(y/n):
设置随机漫步图的样式
1.给点着色
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])#向右走表示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)
###模拟多次随机漫步
import matplotlib.pyplot as plt
while True:
rw = RandomWalk()
rw.fill_walk()
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_value, rw.y_value, c = point_numbers, cmap = plt.cm.Blues, s =15)
plt.show()
keep_running = input('make another walk?(y/n): ')
if keep_running == 'n':
break
image.png
make another walk?(y/n): y
image.png
make another walk?(y/n): n
重新绘制起点和中点
让起点和中点变大,并显示为不同的颜色
###设置随机漫步图的样式
#####1.给点着色
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])#向右走表示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)
###模拟多次随机漫步
import matplotlib.pyplot as plt
while True:
rw = RandomWalk()
rw.fill_walk()
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_value, rw.y_value, c = point_numbers, cmap = plt.cm.Blues, s =15)
#突出起点和中点
plt.scatter(0, 0, c = 'green', cmap = plt.cm.Blues, s =100)
plt.scatter(rw.x_value[-1], rw.y_value[-1], c = 'red', cmap = plt.cm.Blues, s =100)
plt.show()
keep_running = input('make another walk?(y/n): ')
if keep_running == 'n':
break
image.png
make another walk?(y/n): y
image.png
make another walk?(y/n): n
隐藏坐标轴,plt.axes()函数
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])#向右走表示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)
###模拟多次随机漫步
import matplotlib.pyplot as plt
while True:
rw = RandomWalk(5000)
rw.fill_walk()
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_value, rw.y_value, c = point_numbers, cmap = plt.cm.Blues, s =15)
#突出起点和中点
plt.scatter(0, 0, c = 'green', cmap = plt.cm.Blues, s =100)
plt.scatter(rw.x_value[-1], rw.y_value[-1], c = 'red', cmap = plt.cm.Blues, s =100)
#隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
keep_running = input('make another walk?(y/n): ')
if keep_running == 'n':
break
image.png
make another walk?(y/n): n
增加点数
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])#向右走表示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)
###模拟多次随机漫步
import matplotlib.pyplot as plt
while True:
rw = RandomWalk(50000)#增加点数
rw.fill_walk()
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_value, rw.y_value, c = point_numbers, cmap = plt.cm.Blues, s =15)
#突出起点和中点
plt.scatter(0, 0, c = 'green', cmap = plt.cm.Blues, s =100)
plt.scatter(rw.x_value[-1], rw.y_value[-1], c = 'red', cmap = plt.cm.Blues, s =100)
#隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
keep_running = input('make another walk?(y/n): ')
if keep_running == 'n':
break
image.png
make another walk?(y/n): n
调整尺寸以适合屏幕
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])#向右走表示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)
###模拟多次随机漫步
import matplotlib.pyplot as plt
while True:
rw = RandomWalk(50000)#增加点数
rw.fill_walk()
#设置绘图窗口尺寸,长宽以及分辨率
plt.figure(figsize=(10, 6), dpi=128)
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_value, rw.y_value, c = point_numbers, cmap = plt.cm.Blues, s =15)
#突出起点和中点
plt.scatter(0, 0, c = 'green', cmap = plt.cm.Blues, s =100)
plt.scatter(rw.x_value[-1], rw.y_value[-1], c = 'red', cmap = plt.cm.Blues, s =100)
#隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
keep_running = input('make another walk?(y/n): ')
if keep_running == 'n':
break
image.png
make another walk?(y/n): n