How to Start a Graph Drawing Program in Another Program

Asked 2 years ago, Updated 2 years ago, 44 views

As per the title, it doesn't work.

graph_test.py

import numpy as np
import matplotlib.pyplot asplt

defgraph_test1():   
    plt.title('test')
    plt.xlabel ("x")
    plt.ylabel("y")        
    array_x = np.range(0,5,1)
    array_y = [1, 2, 3, 4, 5] 
    plt.plot(array_x,array_y,label="value")
    plt.legend()
    plt.show()

defgraph_test2():   
    plt.title('test')
    plt.xlabel ("x")
    plt.ylabel("y")        
    array_x = np.range(0,5,1)
    array_y = [5,4,3,2,1] 
    plt.plot(array_x,array_y,label="value")
    plt.legend()
    plt.show()

if__name__=='__main__':
    graph_test1()
graph_test2()

start_test.py
I tried two boot methods, but neither of them worked.

import subprocess

output=subprocess.check_output(['python','graph_test.py'])
print("")
print(output.decode())

a=subprocess.call("python%s"%'graph_test.py')  
print(a)

Run Results
How do I output the graph correctly?

Figure (640x480)
Figure (640x480)

0

python python3

2022-09-30 21:35

1 Answers

There is no problem with the code in the question. The Figure(640x480) display is the default Figure object in matplotlib, so the output is good and successful.It seems to be an environmental problem that the graph is not output.

When you call Python code from Python, you should import it without subprocess.start_test.py looks like this:

import graph_test

graph_test.graph_test1()
graph_test.graph_test2()

subprocess is environmentally dependent. If you use subprocess, you should understand the process generation in the Popen constructor section of the official document subprocess page.For example, subprocess.call("python%s"%'graph_test.py') will fail on POSIX and should be subprocess.call("python%s"%'graph_test.py', shell=Tru).


2022-09-30 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.