python中的figure什么意思_Python 多figure显示与循环中显示figure的问题

利用 matplotlib.pyplot 进行画图,首先要开 figure,我们必须了解是开多少个figure,是不是一起显示,或者一个figure上画几条线,各是什么颜色什么设置的问题。

import matplotlib.pyplot as plt

import numpy as np

# 定义数据

x = np.linspace(-3, 3, 50)

y1 = 2*x + 1

y2 = x**2

# 第一个figure

plt.figure() #先开figure

plt.plot(x, y1)

# 第二个figure

plt.figure(num=3, figsize=(8, 5),) # 尺寸(8,5)

# 第二个figure包含两个函数的显示 直接写在一起即可

plt.plot(x, y2)

plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--') # 虚线

# 显示图像

plt.show()

figure与循环

这里面有个问题,就是每次循环后可以先产生 figure,然后显示 pylab.show(), 这样是每一次循环后画一个 figure,但是在程序执行中必须先关掉 figure,才能接着运行后面的程序。

如果想全部循环完,再所有的图都一起显示,则需要先每次循环产生一个figure,但是不先pylab.show(),即先用循环产生所有的figure,但是先不show 出来。再在所有循环所有figure, 都产生之后,再统一show出来,这样可以提高运行的效率。

第一段code:

我写的用3层隐藏层的全连接神经网络拟合的sinx代码最后一部分,循环训练20000次,每10000次跳出,将训练的sinx与标准的sinx画在一张图上进行比较,可以看到最后 pylab.show() 在每次跳出后,这样在循环的时候每跳出一次就会显示一张图,但是显示的时候程序不会往下跑,每次都需要关掉才能继续跑,比较麻烦。

for i in range(20000):

train_x,train_y=get_train_data()

sess.run(train_op, feed_dict={x:train_x,y:train_y})

if i % 10000 == 0:

times=int(i/10000)

test_x_array=np.arange(0,2*np.pi,0.01)

test_y_array=np.zeros([len(test_x_array)])

ind = 0

for test_x in test_x_array:

test_y=sess.run(net_out, feed_dict={x:test_x,y:1})

np.put(test_y_array,ind,test_y)

ind += 1

plt.figure(times+1)

draw_correct_line()

pylab.plot(test_x_array, test_y_array, '--', label= str(times)+ 'times')

pylab.show()

第二段code:

将最后的 pylab.show() 放到最后,先循环产生多个 figure,最后所有的 figure一起显示,效率较高。

for i in range(20000):

train_x,train_y=get_train_data()

sess.run(train_op, feed_dict={x:train_x,y:train_y})

if i % 10000 == 0:

times=int(i/10000)

test_x_array=np.arange(0,2*np.pi,0.01)

test_y_array=np.zeros([len(test_x_array)])

ind = 0

for test_x in test_x_array:

test_y=sess.run(net_out, feed_dict={x:test_x,y:1})

np.put(test_y_array,ind,test_y)

ind += 1

plt.figure(times+1)

draw_correct_line()

pylab.plot(test_x_array, test_y_array, '--', label= str(times)+ 'times')

pylab.show()

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值