Matplotlib reuse rate generated by another script

I am using Matplotlib on macOS with Sulime text. I use Python 3.5and Matplotlib 2.0.

When I work on a figure, I usually have a script that processes the data and saves the figure in a .pdf file with plt.savefig(). Then I use Skim(pdf viewer) to update the file every time I modify and run the script. This allows me to set my working layout as clean: there is one window for the script and one window for the shape, which is automatically updated.

I would like to keep the same layout, but using Matplotlib numbers (because they are interactive). I am looking for a way to use plt.show(), but always in the same figure that was created when the script was first run.

For instance:

1. First run

import matplotlib.pyplot as plt
import numpy as np 

fig, ax = plt.figure()    

noise = np.random.rand(1, 100)
ax(noise)
plt.show()

2. The following runs

import matplotlib.pyplot as plt
import numpy as np 

# This is the super command I am looking for
fig = plt.get_previous_run_figure()
ax = fig.axes

noise = np.random.rand(1, 100)
ax.plot(noise)
plt.draw()

In this case, of course, I would have to run the first script run separately from the main script. Does anyone know if this is possible?

+4
source share
3 answers

You want multiple consecutive python sessions to share a common Matplotlib window. I do not see the possibility of sharing these windows with separate processes, especially when the original owner can stop working at any given time.

- , PDF, , python.

. / , matplotlib: matplotlib.pyplot

script matplotlib , plt.show():

import matplotlib.pyplot as plt
import numpy as np
import pickle

ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
pickle.dump(ax, file('myplot.pickle', 'w'))

python, plt.show(). script , :

import matplotlib.pyplot as plt
import pickle

while True:
   ax = pickle.load(file('myplot.pickle'))
   plt.show()

Alternative

python Ipython, script. , , , .

import matplotlib.pyplot as plt

fig = plt.figure(0)
fig.clf()

plt.show()
+3

, . ( , .)

, , tk pyqt FigureCanvas. .

script , , , .

, .

0

. . Tkinter / matplotlib:

fig1 = None
if fig1:
    #if exists, clear figure 1
    plt.figure(1).clf() 
    plt.suptitle("New Fig Title", fontsize=18, fontweight='bold')
    #reuse window of figure 1 for new figure
    fig1 = plt.scatter(points.T[0], points.T[1], color='red', **plot_kwds)      
else:
    #initialize
    fig1 = plt.figure(num=1,figsize=(7, int(7*imgRatio)), dpi=80)
    plt.tick_params(axis='both', which='major', labelsize=14)
    plt.tick_params(axis='both', which='minor', labelsize=14)           
    plt.suptitle("Title", fontsize=18, fontweight='bold')
    fig1 = plt.scatter(points.T[0], points.T[1], color='red', **plot_kwds)

() plt. interactive : True matplotlibrc (. )

0

Source: https://habr.com/ru/post/1670415/


All Articles