如题,用的是matplotlib.pyplot。
单独拎出来能跑出图,放在while里面就不出图了,就卡在那个界面动不了了。
没报错程序,就是像图三那样不出结果,一直显示无响应。
秋梨膏
无响应就对了呀
图是属于UI控件
你总要给它留出时间执行绘图呀
你的while把主线程卡死了,它哪有时间绘图呢
解决办法:
1.要么换成timer定时调用,而放弃死循环
2.要么在每次循环结束调用强制UI刷新的指令
3.要么把死循环放到子线程里,然后用委托调用UI绘图
那是正在绘制图片吧 多等一会应该就能出来了
import matplotlib.pyplot as plt;
from random import choice
class RandomWa1k:
# 生成随机漫步数据
def __init__(seLf, num_points=5000):
seLf.num_points = num_points;
# 所有随机漫步都M(0,0)开始
seLf.x_values = [0];
seLf.y_values = [0];
def fi11_walk(seLf):
# 生成指定数量的随机漫步点
# 不断漫步,直到列表达到指定的长度
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;
# 计算下一次点的x值和v值
X = seLf.x_values[-1] + x_step;
y = seLf.y_values[-1] + y_step;
y_step = y_direction * y_distance;
# 防止原地踏步
if x_step == 0 and y_step == 0:
continue;
# 计算下一个点的x值和y值
x = seLf.x_values[-1] + x_step
y = seLf.y_values[-1] + y_step
seLf.x_values.append(x);
seLf.y_values.append(y);
# 创建一个Random实例
# rw=Randomwalk();
# rw.fill_walk();
# 将所有点都绘制出来
# plt.style.use('classic')
# fig,ax=plt.subplots();
# point_numbers=range(rw.num_points);
# ax.scatter(rw.x_values, rw.y_values, C=point_numbers, cmap=plt.cm.Blues,
# edgecolors='none‘,$=5);
# plt.show();
while True:
# 创建一个Random实例
rw = RandomWa1k()
rw.fi11_walk();
# 将所有点都绘制出来
plt.style.use('classic');
fig, ax = plt.subplots();
ax.scatter(rw.x_values, rw.y_values, s=5);
plt.show()
keep_running = input("Make another waLk?(y/n) Answer:")
if keep_running == 'n':
break;
import matplotlib.pyplot as plt;
from random import choice
class RandomWa1k:
# 生成随机漫步数据
def __init__(seLf, num_points=5000):
seLf.num_points = num_points;
# 所有随机漫步都M(0,0)开始
seLf.x_values = [0];
seLf.y_values = [0];
def fi11_walk(seLf):
# 生成指定数量的随机漫步点
# 不断漫步,直到列表达到指定的长度
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;
# 计算下一次点的x值和v值
X = seLf.x_values[-1] + x_step;
y = seLf.y_values[-1] + y_step;
y_step = y_direction * y_distance;
# 防止原地踏步
if x_step == 0 and y_step == 0:
continue;
# 计算下一个点的x值和y值
x = seLf.x_values[-1] + x_step
y = seLf.y_values[-1] + y_step
seLf.x_values.append(x);
seLf.y_values.append(y);
# 创建一个Random实例
# rw=Randomwalk();
# rw.fill_walk();
# 将所有点都绘制出来
# plt.style.use('classic')
# fig,ax=plt.subplots();
# point_numbers=range(rw.num_points);
# ax.scatter(rw.x_values, rw.y_values, C=point_numbers, cmap=plt.cm.Blues,
# edgecolors='none‘,$=5);
# plt.show();
while True:
# 创建一个Random实例
rw = RandomWa1k()
rw.fi11_walk();
# 将所有点都绘制出来
plt.style.use('classic');
fig, ax = plt.subplots();
ax.scatter(rw.x_values, rw.y_values, s=5);
plt.show()
keep_running = input("Make another waLk?(y/n) Answer:")
if keep_running == 'n':
break;
给你找了一篇非常好的博客,你可以看看是否有帮助,链接:【Python】使用matplotlib.pyplot实现画折线图的一个实用示例