【python】matplotlib动态显示详解

  

  

python在绘图的时候,需要开启交互模式。核心代码如下:

        plt.ion ();#开启交互模式成功的关键函数   无花果=plt.figure (1);      因为我在范围(100):   filepath=" E:/模型/weights-improvement——“+ str (i + 1) +“.hdf5”;   model.load_weights (filepath);   #测试数据   x_new=np。linspace(低,大学出版社,1000年);   y_new=getfit(模型、x_new);   #显示数据   plt.clf ();   plt.plot (x, y);   plt。散射(x_sample y_sample);   plt.plot (x_new y_new);      ffpath=" E:/一/" + str (i) +“jpg”;   plt.savefig (ffpath);      plt.pause(0.01) #暂停0.01秒      ani=animation.FuncAnimation (plt.figure(2),更新范围(100),init_func=init,间隔=500);   ani.save (“E:/test.gif”,作家=罢硗贰?;      plt.ioff() #关闭交互模式   之前      

  

已知下面采样自罪函数的数据:

  

              x   y         1   0.093   -0.81         2   0.58   -0.45         3.   1.04   -0.007         4   1.55   0.48         5   2.15   0.89         6   2.62   0.997         7   2.71   0.995         8   2.73   0.993         9   3.03   0.916         10   3.14   0.86         11   3.58   0.57         12   3.66   0.504         13   3.81   0.369         14   3.83   0.35         15   4.39   -0.199         16   4.44   -0.248         17   4.6   -0.399         18   5.39   -0.932         19   5.54   -0.975         20.   5.76   -0.999            

  

,通过一个简单的三层神经网络训练一个罪函数的拟合器,并可视化模型训练过程的拟合曲线。

  

【python】matplotlib动态显示详解

  

  

主要做的事情是定义一个三层的神经网络,输入层节点数为1,隐藏层节点数为10,输出层节点数为1 .

        进口数学;   进口随机;   从matplotlib进口pyplot plt   从keras。模型导入顺序   从keras.layers。核心进口密集   从keras。优化进口亚当   进口numpy np   从keras。回调函数导入ModelCheckpoint   进口操作系统         #采样函数   def样本(低,num):   data=https://www.yisu.com/zixun/[];   因为我在范围(num):   #采样   tmp=随机的。制服(低);   data.append (tmp);   data.sort ();   返回数据;      #罪函数   def func (x):   y=[];   因为我在范围(len (x)):   tmp=数学。sin (x[我]- math.pi/3);   y.append (tmp);   返回y;      #获取模型拟合结果   def getfit(模型中,x):   y=[];   因为我在范围(len (x)):   tmp=模型。预测([x[我]],10);   y.append (tmp [0] [0]);   返回y;      #删除同一目录下的所有文件   def del_file(路径):   ls=os.listdir(路径)   因为我在ls:   c_path=os.path。加入(路径,我)   如果os.path.isdir (c_path):   del_file (c_path)   其他:   os.remove (c_path)      if __name__==癬_main__”:   路径=癊:/模型/?   del_file(路径);      低=0;=2 * math.pi;   x=np。linspace(低,大学出版社,1000年);   y=func (x);      #数据采样   # x_sample=样本(低,,20);   x_sample=(0.09326442022999694, 0.5812590520508311, 1.040490143783586, 1.5504427746047338, 2.1589557183817036, 2.6235357787018407, 2.712578091093361, 2.7379109336528167, 3.0339662651841186, 3.147676812083248, 3.58596337171837, 3.6621496731124314, 3.81130899864203, 3.833092859928872, 4.396611340802901, 4.4481080339256875, 4.609657879057151, 5.399731063412583, 5.54299720786794, 5.764084730699906);   y_sample=func (x_sample);      #回调   filepath=" E:/模型/weights-improvement——{时代:00 d} .hdf5”;   检查点=ModelCheckpoint (filepath verbose=1, save_best_only=False,模式=癿ax”);   callbacks_list=(关卡);      #建立顺序神经网络层次模型   模型=顺序();   模型。add(密度(10,input_dim=1, init=爸品?激活=' relu '));   模型。添加(密度(1,init=爸品?激活=双曲正切));   亚当=亚当(lr=0.05);   model.compile (=癿ean_squared_error”损失,优化器=亚当,指标=[“准确性”]);   模型。fit (x_sample、y_sample nb_epoch=1000, batch_size=20,回调=callbacks_list);      #测试数据   x_new=np。linspace(低,大学出版社,1000年);   y_new=getfit(模型、x_new);      #数据可视化   plt.plot (x, y);   plt。散射(x_sample y_sample);   plt.plot (x_new y_new);      plt.show ();   

【python】matplotlib动态显示详解