I have a JavaScript script (open_hist_w_user.js) spawns a python process and passes data to a python script (create_histogram.py). The Python script creates a histogram using the Matplotlib library and displays it. The problem is that even after calling plt.close(‘all’), the plot window does not close.
Code:
*javascript; open_hisr_w_user.js*
/**
* This script is used to spawn a python process that generates a histogram for the specified username.
*
* @param {string} username - The username for which the histogram is being generated.
*
* @returns {ChildProcess} - The spawned python process.
*/
const { spawn } = require('child_process');
const username = 'some_username';
const pythonProcess = spawn('python', ['./create_histogram.py', username]);
python;create_histogram.py
import matplotlib.pyplot as plt
import sys
import time
def create_histogram(username):
"""
This function creates a histogram of data, with a specified number of bins and range, and displays it.
Parameters
----------
username : str
The username for which the histogram is being generated.
Returns
-------
None
"""
plt.close('all') # close any previous plot windows
# plot the histogram
plt.clf() # clear the previous plot
plt.hist([1, 2, 3, 4, 5], bins=3, range=(0, 5))
plt.xlabel('Total')
plt.ylabel('Frequency')
plt.title(f'Histogram of Total Column for {username}')
plt.show() # show the plot
time.sleep(5) # wait for 5 seconds
plt.close('all') # close the plot window
if __name__ == "__main__":
username = sys.argv[1]
create_histogram(username)
I tried using plt.close(‘all’) to close the plot window, but it did not work. I expected the plot window to close after some delay after the histogram was created. Before you ask why am I not just using a javascript script,I tried porting my code a few days ago using plotly and df3, but couldn’t get it working; so here we are passing data through the console.
Expected Result:
insert image of closed window here