使用C#在GNU PLOT中绘制图形

3
我可以协助你进行翻译。以下是需要翻译的内容:

我有一个dat文件,名为insert.dat

文件内容如下:

1000    0.002322044 0.00291182
5000    0.000103257 0.000458963
10000   2.50E-05    0.000172019
20000   6.18E-06    6.03E-05
40000   2.51E-06    2.65E-05
60000   1.65E-06    1.71E-05
80000   1.21E-06    1.23E-05
100000  1.01E-06    9.97E-06

当我打开gnuplot.exe并键入plot insert.dat with lines时,我得到了适当的输出,但是当我编写以下C#代码时:
private  void GNUPlot()
{
    string pgm = @"E:\gnuplot\bin\gnuplot.exe";

    Process extPro = new Process();
    extPro.StartInfo.FileName = pgm;
    extPro.StartInfo.UseShellExecute = false;
    extPro.StartInfo.Standardization = true;
    extPro.Start();

    StreamWriter gnupStWr = extPro.StandardInput;
    gnupStWr.WriteLine("plot \"insert.dat\" with lines ");
    gnupStWr.Flush();
}

我收到了以下警告:
warning: Skipping unreadable file "insert.dat"

当我替换

时,
gnupStWr.WriteLine("plot \"insert.dat\" with lines ");

with

gnupStWr.WriteLine("plot sin(x) ");

我得到了所需的Sin(x)图形输出。
insert.dat在gnuplot的当前目录中。我想要绘制insert.dat文件的数据。

你尝试过使用单引号 'insert.dat' 吗? - Anders Gustafsson
编写一个 plot.p 脚本并执行 gnuplot plot.p。 - Exceptyon
@AndersGustafsson,是的,我也尝试了“insert.dat”。它仍然无法工作。它会给出相同的警告。 - guddi
@Exceptyon 请解释一下如何编写... - guddi
1
@guddi:我的意思是,如果你从命令行执行“gnuplot script.p”,gnuplot将执行脚本文件中的所有行。因此,我建议从C#编写脚本文件,并使用第一个参数作为脚本文件名来调用gnuplot... - Exceptyon
@Exceptyon 是的,谢谢 :) 我现在明白了。 - guddi
1个回答

3
问题似乎是gnuplot无法找到请求的文件。进程的工作目录未设置为gnuplot目录,而可能是调用gnuplot的应用程序的目录。
编辑尝试在extPro.Start()命令之前的代码中添加以下任一行:
extPro.StartInfo.WorkingDirectory = @"E:\gnuplot\bin";

或者

Environment.CurrentDirectory = @"E:\gnuplot\bin";

如果这不起作用,可能是因为您的应用程序无法读取该目录。请将您的insert.dat文件放置在任何客户端应用程序都可以访问的目录中。
顺便说一下,我也不认识您正在使用的Standardization属性?那行代码应该改成:
extPro.StartInfo.RedirectStandardInput = true;

@guddi 我已经更新了我的回答,提供了一些额外的信息。特别地,请确保你的应用程序有对 gnuplot bin 文件夹的读取权限。你收到的错误消息通常是文件找不到(或无法访问)的情况,plot 命令本身似乎没问题。 - Anders Gustafsson
谢谢 :) 我明白了。现在它可以工作了。我把脚本写在 file.gp 文件中,并将该文件作为参数传递 [链接] (http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/50df756e-3a57-4554-ada7-734eccb14b33)。 - guddi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接