c#用Gnuplot画图源码

直接调用这个类即可,需要下载个GnuPlot安装下。

复制代码
// Author: Leonardo Tazzini

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

/// <summary>
/// Tested with Gnuplot 5.2
/// </summary>
public class GnuPlot
{
    public GnuPlot()
    {
        // Default path for gnuplot
        this.Path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "gnuplot\\bin\\gnuplot.exe");
        // TODO: Check if path is valid
    }

    public GnuPlot(string path)
    {
        this.Path = path;
    }

    /// <summary>
    /// Path of gnuplot.exe
    /// </summary>
    public string Path { get; set; }

    /// <summary>
    /// Graph input CSV with GnuPlot. NumWaves is the number of channels to draw. If imageOut a PNG image is saved into out.png and displayed. 
    /// </summary>
    public void DrawGraph(string csvFileName, int numWaves, bool generatePngImage = true)
    {
        // gnuplot.exe not found
        if(!File.Exists(Path))
        {
            MessageBox.Show("gnuplot.exe not found. Please check the path.");
            return;
        }

        var gnuPlot = new Process();
        gnuPlot.StartInfo.FileName = Path;
        gnuPlot.StartInfo.CreateNoWindow = true;
        gnuPlot.StartInfo.UseShellExecute = false;
        gnuPlot.StartInfo.RedirectStandardInput = true;
        gnuPlot.Start();

        StreamWriter gnuPlotSw = gnuPlot.StandardInput;
        if (generatePngImage)
        {
            // TODO: Remove dependency from Windows Forms
            Rectangle bounds = Screen.PrimaryScreen.WorkingArea;
            gnuPlotSw.WriteLine("set terminal png size " + (bounds.Width * 0.9) + "," + (bounds.Height * 0.8));
            gnuPlotSw.WriteLine("set out 'out.png'");
        }
        gnuPlotSw.WriteLine("set style data lines");      //linespoints
        gnuPlotSw.WriteLine("set datafile separator ';'");
        //gnuPlotSw.WriteLine("set decimalsign locale; set decimalsign ','");
        gnuPlotSw.WriteLine("set xlabel 'time  (s)'");
        gnuPlotSw.WriteLine("set ylabel 'value (v)'");
        gnuPlotSw.WriteLine("set grid");
        //gnuPlotSw.WriteLine("set xtics 0,0.5");
        //gnuPlotSw.WriteLine("set ytics 0,5");

        // TODO: evaluate add "every .." to gnuplot cmd line    
        gnuPlotSw.Write("plot '" + System.IO.Path.GetFullPath(csvFileName) + "' using 1:2 title 'CH1'");
        if (numWaves == 2)
            gnuPlotSw.WriteLine(", '' using 1:3 title 'CH2'");
        else
            gnuPlotSw.Write("\n");
        gnuPlotSw.Flush();
        if (generatePngImage)
        {
            gnuPlotSw.WriteLine("exit");
            gnuPlotSw.Flush();
            gnuPlot.WaitForExit();
            Process.Start("out.png");
        }
        gnuPlot.Close();
    }


}

读吧数据存为csv,传入DrawGraph即可;数据如下:

TIME(s);CH1(v);

-600.99999949;1.5001;

-599.99999949;-0.0598999999999998;

-598.99999949;-0.0398999999999998;

-597.99999949;-0.0398999999999998;

-596.99999949;-0.0398999999999998;

-595.99999949;-0.0398999999999998;

-594.99999949;-0.0398999999999998;

-593.99999949;-0.0398999999999998;

-592.99999949;-0.0598999999999998;

-591.99999949;-0.0398999999999998;

-590.99999949;-0.0398999999999998;

-589.99999949;-0.0598999999999998;

-588.99999949;-0.0398999999999998;

-587.99999949;-0.0398999999999998;

-586.99999949;-0.0398999999999998;

-585.99999949;-0.0598999999999998;

显示示意:

相关推荐
fie88895 分钟前
MATLAB中LASSO方法的特征矩阵优化与特征选择实现
开发语言·matlab·矩阵
LilySesy10 分钟前
【SAP-MOM项目】二、接口对接(中)
开发语言·python·pandas·restful·sap·abap
零度@12 分钟前
专为 Java 开发者 整理的《Python编程:从入门到实践》前8章核心内容
java·开发语言·windows·python
nbsaas-boot17 分钟前
架构设计怎么做:一套可复用、可落地的方法论
java·开发语言·微服务
骆驼爱记录17 分钟前
Word表格题注自动设置全攻略
开发语言·c#·自动化·word·excel·wps·新人首发
wbs_scy19 分钟前
C++:智能指针完全指南(原理、用法与避坑实战,从 RAII 到循环引用)
开发语言·c++·算法
无人装备硬件开发爱好者21 分钟前
Python + Blender 5.0 几何节点全栈实战教程1
开发语言·python·blender
u01092727121 分钟前
C++中的对象池模式
开发语言·c++·算法
武超杰22 分钟前
深入理解JDBC:Java数据库连接的核心技术与实践
java·开发语言·数据库·jdbc
80530单词突击赢25 分钟前
MPPI算法:ROS下的智能控制实战
开发语言·python