How to save plots from multiple python scripts using an interactive C# process command?
<p>I've been trying to save plots(multiple) from different scripts using an interactive C# process command.</p>

<p><strong>Objective:</strong> My aim is to save plots by executing multiple scripts in a single interactive python shell and that too from C#.</p>

<p>Here's the code which I've tried from my end.</p>

<p><strong>Code snippet:</strong></p>

<pre><code>class Program
{
    static string data = string.Empty;
    static void Main(string[] args)
    {
        Process pythonProcess = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "/c" + "python.exe -i");
        startInfo.WorkingDirectory = "C:\\python";
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        pythonProcess.StartInfo = startInfo;
        pythonProcess.ErrorDataReceived += Process_ErrorDataReceived;
        pythonProcess.OutputDataReceived += Process_OutputDataReceived;
        pythonProcess.Start();
        pythonProcess.BeginErrorReadLine();
        pythonProcess.BeginOutputReadLine();
        Thread.Sleep(5000);

        while (data != string.Empty)
        {
            if (data == "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.")
            {
                pythonProcess.StandardInput.WriteLine("execfile('script1.py')");
                pythonProcess.StandardInput.WriteLine("execfile('script2.py')");
                break;
            }
        }
        pythonProcess.WaitForExit();
    }

    private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        data = e.Data;
    }

    private static void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        data = e.Data;
    }
}
</code></pre>

<p><strong>Scripts:</strong></p>

<p><em>script1.py</em></p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.savefig('script1.png')
</code></pre>

<p><em>script2.py</em></p>

<pre><code>import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.savefig('script2.png')
</code></pre>

<p>From the above C# code, you could see that I've tried to execute both the scripts. But, during the execution, the plot from the first script (<em>script1.py</em>) alone gets saved, whereas the second one (<em>script2.py</em>) doesn't. </p>

<p>All I need to know is, how come the second script plot doesn't gets saved and what's the reason behind this ?</p>

<p>Thanks in advance :)</p>

<p>Since you are opening a python shell and then sending it the commands:</p>

<pre><code>execfile('script1.py')
execfile('script2.py')
</code></pre>

<p>Immediately one after the other I suspect that the second command is being sent during time taken to load <code>numpy</code> and <code>matplotlib</code> - you need to check the output from the first <code>execfile</code> to make sure that it has <strong>finished</strong> before issuing the second, or you need to use a different thread for each - at the moment you have a single thread attempting to perform two separate actions almost at once.</p>

<p>Note that once you have finished with your python shell it is a <strong>very</strong> good idea to exit it by sending <code>exit()</code> to the process stdin and calling <code>process.WaitForExit()</code>.</p>

<p>Also note that using a shell to call python code and execute external threads is classed as a security vulnerability in addition to being slow, reliant on the user having python installed, etc., you might be better off looking at using <a href="http://ironpython.net/" rel="nofollow">IronPython</a> - but the only actively maintained plotting library that <strong><em>I</em></strong> was able to find is the <em>non-free</em> <a href="http://ilnumerics.net/visualization-engine.html" rel="nofollow">ILNumerics Visualization Engine</a>.</p>

c# python plot command-line python.net