How to add correct a second screen in matplotlib

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alekssky
    New Member
    • Feb 2020
    • 1

    How to add correct a second screen in matplotlib

    Hello! I would to have 2 screens to display 2 figures.

    But i don't see changes in my second figure but if i change the scale of the screen of my second figure --- then changes are shown.

    Code:
    # -*- coding: utf-8 -*-
    """
    Created on Mon Feb  3 02:31:13 2020
    
    """
    
    import matplotlib.pyplot as plt
     
    import networkx as nx
    
    """ Initializing of the screen-objects, each of which draws its own:"""
     
    fig = plt.figure(1)
    fig_G = plt.figure(2)
    G = nx.Graph()  # variable for creating a graph
    
    """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:"""
     
    import numpy as np
     
    X = np.array(sorted([np.random.uniform(0, np.pi) for i in range(100)]))
     
    """Suppose, i have i functions, that compute array Y. Input of each of this functions is array X:"""
     
    def our_Y_sin(X):
        return np.sin(X)
     
    def our_Y_cos(X):
        return np.cos(X)
     
     
    """Functions, that rendering figures:"""
     
    """First, graf1 work with networkx ([url]https://networkx.github.io/documentation/stable/index.html[/url]) и строит граф G:"""
     
    
     
    def graf1():
        global G, fig_G
        fig_G.add_subplot(111)
        pos = nx.spring_layout(G)   # coordinates of vertexes
        edges, colors = zip(*nx.get_edge_attributes(G,'weight').items()) # color the edges in dependeing of its weight
        nx.draw(G, pos, edgelist=edges, edge_color=colors, with_labels=True)
        labels = nx.get_edge_attributes(G, 'weight')
        nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
     
    """Second function is graf2. Suppose, it draw 2 charts:"""
     
     
    def graf2(X, double_arr_Y):
        global fig
        ax = fig.add_subplot(111)
        ax.clear() # need to clear the figure, cause values are changing on each iteration in main cycle (line 78)
        ax.plot(X, double_arr_Y[0], marker='o',color = 'blue')
        ax.plot(X, double_arr_Y[1], marker='+',)
     
     
    """update --- need to clear second figure:"""
     
    def update():
        global G
        plt.pause(0.9)
        G.clear() # remove graph connectivity objects --- vertexes, edges, cause graph is changing in each iteration of main cycle (line 79)
        plt.clf()
     
    def Plt(arr1x, arr2Y):
        graf1() # prepare data for drawing the figure 1
        graf2(arr1x, arr2Y) # prepare data for drawing the figure 2
        plt.show()
        update()
     
    """Function, that creating a pretty simple graph --- variable G:"""
     
    def path(G):
        G.add_edge("Stockholm", "Oslo", weight=100)
        G.add_edge("Prague", "Stockholm", weight=50)
        G.add_edge("Prague", "Berlin", weight=25)
        return G
     
     
    Y = [our_Y_sin(X), our_Y_cos(X)]# data for drawing the figure 2
     
    """Next, the main cycle, that rendering 2 figures and oops, second figure don't display on the screen:"""
     
    for i in range(100):
        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) 
        noise = np.array([np.random.uniform(-5,5) for i in range(100)])
        Y[0] = Y[0] + noise
        Plt(X, Y)
    I attached this python-script

    Thanks for attention!
    Attached Files
Working...