如何在matplotlib中添加正确的第二个屏幕

你好!我会有2个屏幕来显示2位数字。
但我看不到第二张图的变化,但如果我改变第二张图的屏幕比例-那么变化就会显示出来。

选择 | 换行 | 行号
  1.  
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Feb  3 02:31:13 2020
  5.  
  6. """
  7.  
  8. import matplotlib.pyplot as plt
  9.  
  10. import networkx as nx
  11.  
  12. """ Initializing of the screen-objects, each of which draws its own:"""
  13.  
  14. fig = plt.figure(1)
  15. fig_G = plt.figure(2)
  16. G = nx.Graph()  # variable for creating a graph
  17.  
  18. """Suppose, i have data for each iteration of main cycle (Although the data is calculated at each iteration of the cycle and it all change. I will give an array X of length 100, maybe this is too much for networkorx --- I do not know:"""
  19.  
  20. import numpy as np
  21.  
  22. X = np.array(sorted([np.random.uniform(0, np.pi) for i in range(100)]))
  23.  
  24. """Suppose, i have i functions, that compute array Y. Input of each of this functions is array X:"""
  25.  
  26. def our_Y_sin(X):
  27.     return np.sin(X)
  28.  
  29. def our_Y_cos(X):
  30.     return np.cos(X)
  31.  
  32.  
  33. """Functions, that rendering figures:"""
  34.  
  35. """First, graf1 work with networkx (https://networkx.github.io/documenta...ble/index.html) и строит граф G:"""
  36.  
  37.  
  38.  
  39. def graf1():
  40.     global G, fig_G
  41.     fig_G.add_subplot(111)
  42.     pos = nx.spring_layout(G)   # coordinates of vertexes
  43.     edges, colors = zip(*nx.get_edge_attributes(G,'weight').items()) # color the edges in dependeing of its weight
  44.     nx.draw(G, pos, edgelist=edges, edge_color=colors, with_labels=True)
  45.     labels = nx.get_edge_attributes(G, 'weight')
  46.     nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
  47.  
  48. """Second function is graf2. Suppose, it draw 2 charts:"""
  49.  
  50.  
  51. def graf2(X, double_arr_Y):
  52.     global fig
  53.     ax = fig.add_subplot(111)
  54.     ax.clear() # need to clear the figure, cause values are changing on each iteration in main cycle (line 78)
  55.     ax.plot(X, double_arr_Y[0], marker='o',color = 'blue')
  56.     ax.plot(X, double_arr_Y[1], marker='+',)
  57.  
  58.  
  59. """update --- need to clear second figure:"""
  60.  
  61. def update():
  62.     global G
  63.     plt.pause(0.9)
  64.     G.clear() # remove graph connectivity objects --- vertexes, edges, cause graph is changing in each iteration of main cycle (line 79)
  65.     plt.clf()
  66.  
  67. def Plt(arr1x, arr2Y):
  68.     graf1() # prepare data for drawing the figure 1
  69.     graf2(arr1x, arr2Y) # prepare data for drawing the figure 2
  70.     plt.show()
  71.     update()
  72.  
  73. """Function, that creating a pretty simple graph --- variable G:"""
  74.  
  75. def path(G):
  76.     G.add_edge("Stockholm", "Oslo", weight=100)
  77.     G.add_edge("Prague", "Stockholm", weight=50)
  78.     G.add_edge("Prague", "Berlin", weight=25)
  79.     return G
  80.  
  81.  
  82. Y = [our_Y_sin(X), our_Y_cos(X)]# data for drawing the figure 2
  83.  
  84. """Next, the main cycle, that rendering 2 figures and oops, second figure don't display on the screen:"""
  85.  
  86. for i in range(100):
  87.     G = path(G) # graph G are drawed anew after each reset in the G.clear () statement (line 56 of the update procedure, which is called in Plt) 
  88.     noise = np.array([np.random.uniform(-5,5) for i in range(100)])
  89.     Y[0] = Y[0] + noise
  90.     Plt(X, Y)

我附上了这个python脚本
感谢您的关注!
附加的文件
File Type: zip
TEST5.zip
(1.9KB,49次浏览)

标签: python

添加新评论