Domanda

I have a question regarding windows/figures in matplotlib. I'm not sure if this is possible, but would like to know if it is. Basically when I run my whole script, at the end a graph is plotted using matplotlib. In order to produce a new graph after running my script again I have to close that graph window.

Is there a way of keeping open the figure without closing it?

Let me give an example:

I would plot graph x by running my script.

I would then like to keep this graph on my screen, make a change to my script, plot the graph again so you may see the old graph and the new graph. Therefore n number of graphs may be visible.

Please note that I do NOT want to plot a new figure within my script. I simply would like to be able to see the graph, make a change and see the new graph WITHOUT having to save the graph.

EDIT:

This is the plotting secion of my code:

def plot_data(atb_mat_2, sd_index, sd_grad):#, rtsd):#, sd_index, sd_grad):
    fig = plt.figure()
    fig, (ax0, ax1, ax4, ax2, ax3) = plt.subplots(nrows=5, figsize=(15,10), num='Current Relative Method'+'  ' + path)
    ax0.plot(atb_mat_2)
    ax0.set_title('Relative Track',fontsize=11)
    ax0.set_ylim([-10,10])
    if len(sd_index)!=0:
        if len(sd_index)>1:
            for i in range(1, len(sd_index)):
                if sd_grad[i]==1:
                    ax0.axvspan(sd_index[i-1],sd_index[i], edgecolor='r', lw=None, alpha=0.1)
    ax1.plot(rtsd)
    ax1.set_title('RT Standard Deviation',fontsize=11)
    ax1.set_ylim([0,250])
    ax4.plot(abs_track_data)
    ax4.set_title('Absolute Track',fontsize=11)
    ax4.set_ylim([3000,5000])
    ax2.plot(splitpo)
    ax2.set_title('Track Split',fontsize=11)
    ax2.set_ylim([0,20])
    ax3.plot(ts)
    ax3.set_title('TS Standard Deviation',fontsize=11)
    ax3.set_ylim([0,100])

    fig.tight_layout()
    plt.show()

Thanks alot of any advice and sorry if this answer is obvious as I'm fairly new.

È stato utile?

Soluzione

You can do it using ipython.

  1. Write your script and save it as (for example) test.py. The script should create a figure, do the plotting and show the plot:

    import numpy as np
    import matplotlib.pyplot as plt 
    
    fig = plt.figure()
    x = np.linspace(-1, 1, 100)
    y = np.sin(x)
    plt.plot(x, y)
    plt.show()
    
  2. Start the ipython console using:

    ipython --pylab=qt
    

    Or whatever backend you want to use.

  3. In the ipython shell type:

    %run /path/to/the/test.py
    

    This will create a figure, and show the plot.

  4. After that change your script. For example change the 5th line to:

    x = np.linspace(-0, 2, 100)
    
  5. Repeat the %run command in the ipython shell:

    %run /path/to/the/test.py
    

    Another figure will pop up with the new plot. Old figure will be also visible (this won't remove it or replace it).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top