├── .gitattributes ├── .gitignore ├── Demo.cs ├── GnuPlot.cs ├── GnuplotCSharp.csproj ├── GnuplotCSharp.sln ├── LICENSE ├── Properties └── AssemblyInfo.cs ├── README.md ├── ReadmeImages ├── contourfunc.png ├── heatmap.png ├── phase.png ├── plotOverlay.png ├── plotXY.png ├── plotY.png ├── plotf.png ├── plotf2.png ├── splot1.png ├── splot2.png ├── splot3.png ├── splotSZ.png ├── splotZZ.png ├── splotfile.png ├── splotxyz1.png └── splotxyz2.png └── splotexampledata.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /Demo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using AwokeKnowing.GnuplotCSharp; 6 | using System.Threading; 7 | 8 | class Demo 9 | { 10 | static void Main(string[] args) 11 | { 12 | //GnuPlot.Plot("sin(x) + 2", "lc rgb \"magenta\" lw 5"); 13 | //Thread.Sleep(2000); 14 | 15 | //GnuPlot.HoldOn(); 16 | //GnuPlot.Plot("cos(x) + x"); 17 | //GnuPlot.Plot("cos(2*x)", "with points pt 3"); 18 | //Thread.Sleep(2000); 19 | 20 | //GnuPlot.HoldOff(); 21 | //GnuPlot.Plot("atan(x)"); 22 | //Thread.Sleep(2000); 23 | 24 | //double[] X = new double[] { -10, -8.5, -2, 1, 6, 9, 10, 14, 15, 19 }; 25 | //double[] Y = new double[] { -4, 6.5, -2, 3, -8, -5, 11, 4, -5, 10 }; 26 | //GnuPlot.Plot(X, Y); 27 | //Thread.Sleep(2000); 28 | 29 | //string tempfolder=System.IO.Path.GetTempPath(); 30 | //GnuPlot.SaveData(X, Y, tempfolder+ "plot1.data"); 31 | //GnuPlot.Plot(tempfolder+ "plot1.data", "with linespoints pt " + (int)PointStyles.SolidDiamond); 32 | //Thread.Sleep(2000); 33 | 34 | //GnuPlot.HoldOn(); 35 | //GnuPlot.Set("xrange [-25:40]"); 36 | //GnuPlot.Set("yrange [-15:15]"); 37 | //var r = new Random(); 38 | //for (int i = 0; i < 14; i++) 39 | //{ 40 | // var Xr = new double[10]; 41 | // var Yr = new double[10]; 42 | // double rx=r.Next(-20,20); 43 | // double ry = r.Next(-10, 10); 44 | // for (int di = 0; di < 10; di++) 45 | // { 46 | // Xr[di] = rx+r.Next(-5, 5); 47 | // Yr[di] = ry+r.Next(-3, 3); 48 | // } 49 | 50 | // GnuPlot.Plot(Xr, Yr, "title 'point style "+i+"' pt " + i); 51 | // Thread.Sleep(200); 52 | //} 53 | //GnuPlot.HoldOff(); 54 | //Thread.Sleep(3000); 55 | 56 | 57 | ////splot demos 58 | //double[] z = new double[31 * 31]; 59 | //for (int x = 0; x < 31; x++) 60 | // for (int y = 0; y < 31; y++) 61 | // z[31 * x + y] = (x - 15) * (x - 15) + (y - 15) * (y - 15); 62 | //GnuPlot.Set("pm3d"); 63 | //GnuPlot.Set("autoscale"); 64 | //GnuPlot.Set("contour base"); 65 | //GnuPlot.SPlot(31,z); 66 | //Thread.Sleep(2000); 67 | 68 | //GnuPlot.HoldOn(); 69 | //GnuPlot.Set("view map"); 70 | //GnuPlot.Unset("surface"); 71 | //GnuPlot.Set("cntrparam levels 10"); 72 | //GnuPlot.Set("palette gray"); 73 | //GnuPlot.SPlot(31, z); 74 | //Thread.Sleep(2000); 75 | 76 | //Console.WriteLine("End of demo"); 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | //GnuPlot.Plot("sin(x) + 2"); 91 | 92 | //GnuPlot.Plot("sin(x) + 2", "lc rgb \"magenta\" lw 5"); 93 | 94 | //double[] Y = new double[] { -4, 6.5, -2, 3, -8, -5, 11, 4, -5, 10 }; 95 | //GnuPlot.Plot(Y); 96 | 97 | //double[] X = new double[] { -10, -8.5, -2, 1, 6, 9, 10, 14, 15, 19 }; 98 | //double[] Y = new double[] { -4, 6.5, -2, 3, -8, -5, 11, 4, -5, 10 }; 99 | //GnuPlot.Plot(X, Y); 100 | 101 | //GnuPlot.HoldOn(); 102 | //GnuPlot.Plot("cos(x) + x"); 103 | //GnuPlot.Plot("cos(2*x)", "with points pt 3"); 104 | 105 | 106 | //GnuPlot.SPlot("1 / (.05*x*x + .05*y*y + 1)"); 107 | 108 | //GnuPlot.Set("isosamples 30"); 109 | //GnuPlot.SPlot("1 / (.05*x*x + .05*y*y + 1)"); 110 | 111 | //GnuPlot.Set("isosamples 30", "hidden3d"); 112 | //GnuPlot.SPlot("1 / (.05*x*x + .05*y*y + 1)"); 113 | 114 | //GnuPlot.SPlot("splotexampledata.txt"); 115 | 116 | //double[] Z = new double[] { -4, -2.5, 1, 3, -3, -2, 3, 4, -1, 2, 6, 8 }; 117 | //GnuPlot.Set("pm3d"); 118 | //GnuPlot.SPlot(4, Z); 119 | 120 | //double[,] Z = new double[,] { {-4,-2.5,1,3}, {-3,-2,3,4}, {-1,2,6,8 } }; 121 | //GnuPlot.Set("pm3d","palette gray"); 122 | //GnuPlot.SPlot(Z,"with points pointtype 6"); 123 | 124 | //double[] X = new double[100]; 125 | //double[] Y = new double[100]; 126 | //double[] Z = new double[100]; 127 | //Random r=new Random(); 128 | //for (int i = 0; i < 100; i++) 129 | //{ 130 | // X[i] = r.Next(30) - 15; 131 | // Y[i] = r.Next(50) - 25; 132 | // Z[i] = r.Next(20) - 10; 133 | //} 134 | 135 | //GnuPlot.Set("xrange[-30:30]", "yrange[-30:30]", "zrange[-30:30]"); 136 | //GnuPlot.SPlot(X, Y, Z, "with points pointtype 8 lc rgb \"blue\""); 137 | 138 | 139 | //double[] X = new double[20]; 140 | //double[] Y = new double[20]; 141 | //double[] Z = new double[20]; 142 | //Random r = new Random(); 143 | //for (int i = 0; i < 20; i++) 144 | //{ 145 | // X[i] = r.Next(30) - 15; 146 | // Y[i] = r.Next(50) - 25; 147 | // Z[i] = r.Next(40) - 20; 148 | //} 149 | 150 | 151 | //GnuPlot.Set("dgrid3d 40,40,2"); 152 | //GnuPlot.Set("xrange[-30:30]", "yrange[-30:30]", "zrange[-30:30]"); 153 | //GnuPlot.SPlot(X, Y, Z,"with pm3d"); 154 | 155 | //GnuPlot.Unset("key"); 156 | //GnuPlot.Set("cntrparam levels 20","isosamples 50", "xr[-5:5]","yr[-6:6]"); 157 | //GnuPlot.Contour("sin(x) * cos(y)+x","lc rgb 'blue'"); 158 | 159 | double[,] Z = new double[,]{{0,0,0,1,2,2,1,0,0,0}, 160 | {0,0,2,3,3,3,3,2,0,0}, 161 | {0,2,3,4,4,4,4,3,2,0}, 162 | {2,3,4,5,5,5,5,4,3,2}, 163 | {3,4,5,6,7,7,6,5,4,3}, 164 | {3,4,5,6,7,7,6,5,4,3}, 165 | {2,3,4,5,5,5,5,4,3,2}, 166 | {0,2,3,4,4,4,4,3,2,0}, 167 | {0,0,2,3,3,3,3,2,0,0}, 168 | {0,0,0,1,2,2,1,0,0,0}}; 169 | GnuPlot.HeatMap(Z); 170 | 171 | Console.ReadKey(); 172 | 173 | } 174 | } 175 | 176 | -------------------------------------------------------------------------------- /GnuPlot.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.IO; 5 | using System.Threading; 6 | using System.Linq; 7 | 8 | namespace AwokeKnowing.GnuplotCSharp 9 | { 10 | class GnuPlot 11 | { 12 | public static string PathToGnuplot = @"C:\Program Files (x86)\gnuplot\bin"; 13 | private static Process ExtPro; 14 | private static StreamWriter GnupStWr; 15 | private static List PlotBuffer; 16 | private static List SPlotBuffer; 17 | private static bool ReplotWithSplot; 18 | 19 | public static bool Hold { get; private set; } 20 | 21 | static GnuPlot() 22 | { 23 | if (PathToGnuplot[PathToGnuplot.Length - 1].ToString() != @"\") 24 | PathToGnuplot += @"\"; 25 | ExtPro = new Process(); 26 | ExtPro.StartInfo.FileName = PathToGnuplot + "gnuplot.exe"; 27 | ExtPro.StartInfo.UseShellExecute = false; 28 | ExtPro.StartInfo.RedirectStandardInput = true; 29 | ExtPro.Start(); 30 | GnupStWr = ExtPro.StandardInput; 31 | PlotBuffer = new List(); 32 | SPlotBuffer = new List(); 33 | Hold = false; 34 | } 35 | 36 | public static void WriteLine(string gnuplotcommands) 37 | { 38 | 39 | GnupStWr.WriteLine(gnuplotcommands); 40 | GnupStWr.Flush(); 41 | } 42 | 43 | public static void Write(string gnuplotcommands) 44 | { 45 | GnupStWr.Write(gnuplotcommands); 46 | GnupStWr.Flush(); 47 | } 48 | 49 | public static void Set(params string[] options) 50 | { 51 | for (int i = 0; i < options.Length; i++) 52 | GnupStWr.WriteLine("set " + options[i]); 53 | 54 | } 55 | 56 | public static void Unset(params string[] options) 57 | { 58 | for (int i = 0; i < options.Length; i++) 59 | GnupStWr.WriteLine("unset " + options[i]); 60 | } 61 | 62 | public static bool SaveData(double[] Y, string filename) 63 | { 64 | StreamWriter dataStream = new StreamWriter(filename, false); 65 | WriteData(Y, dataStream); 66 | dataStream.Close(); 67 | 68 | return true; 69 | } 70 | 71 | public static bool SaveData(double[] X, double[] Y, string filename) 72 | { 73 | StreamWriter dataStream = new StreamWriter(filename, false); 74 | WriteData(X, Y, dataStream); 75 | dataStream.Close(); 76 | 77 | return true; 78 | } 79 | 80 | public static bool SaveData(double[] X, double[] Y, double[] Z, string filename) 81 | { 82 | StreamWriter dataStream = new StreamWriter(filename, false); 83 | WriteData(X, Y, Z, dataStream); 84 | dataStream.Close(); 85 | 86 | return true; 87 | } 88 | 89 | public static bool SaveData(int sizeY, double[] Z, string filename) 90 | { 91 | StreamWriter dataStream = new StreamWriter(filename, false); 92 | WriteData(sizeY, Z, dataStream); 93 | dataStream.Close(); 94 | 95 | return true; 96 | } 97 | 98 | public static bool SaveData(double[,] Z, string filename) 99 | { 100 | StreamWriter dataStream = new StreamWriter(filename, false); 101 | WriteData(Z, dataStream); 102 | dataStream.Close(); 103 | 104 | return true; 105 | } 106 | 107 | public static void Replot() 108 | { 109 | if (ReplotWithSplot) 110 | SPlot(SPlotBuffer); 111 | else 112 | Plot(PlotBuffer); 113 | } 114 | 115 | public static void Plot(string filenameOrFunction, string options = "") 116 | { 117 | if (!Hold) PlotBuffer.Clear(); 118 | PlotBuffer.Add(new StoredPlot(filenameOrFunction, options)); 119 | Plot(PlotBuffer); 120 | } 121 | public static void Plot(double[] y, string options = "") 122 | { 123 | if (!Hold) PlotBuffer.Clear(); 124 | PlotBuffer.Add(new StoredPlot(y, options)); 125 | Plot(PlotBuffer); 126 | } 127 | public static void Plot(double[] x, double[] y, string options = "") 128 | { 129 | if (!Hold) PlotBuffer.Clear(); 130 | PlotBuffer.Add(new StoredPlot(x, y, options)); 131 | Plot(PlotBuffer); 132 | } 133 | 134 | public static void Contour(string filenameOrFunction, string options = "", bool labelContours = true) 135 | { 136 | if (!Hold) PlotBuffer.Clear(); 137 | var p = new StoredPlot(filenameOrFunction, options, PlotTypes.ContourFileOrFunction); 138 | p.LabelContours = labelContours; 139 | PlotBuffer.Add(p); 140 | Plot(PlotBuffer); 141 | } 142 | public static void Contour(int sizeY, double[] z, string options = "", bool labelContours = true) 143 | { 144 | if (!Hold) PlotBuffer.Clear(); 145 | var p = new StoredPlot(sizeY, z, options, PlotTypes.ContourZ); 146 | p.LabelContours = labelContours; 147 | PlotBuffer.Add(p); 148 | Plot(PlotBuffer); 149 | } 150 | public static void Contour(double[] x, double[] y, double[] z, string options = "", bool labelContours = true) 151 | { 152 | if (!Hold) PlotBuffer.Clear(); 153 | var p = new StoredPlot(x, y, z, options, PlotTypes.ContourXYZ); 154 | p.LabelContours = labelContours; 155 | PlotBuffer.Add(p); 156 | Plot(PlotBuffer); 157 | } 158 | public static void Contour(double[,] zz, string options = "", bool labelContours = true) 159 | { 160 | if (!Hold) PlotBuffer.Clear(); 161 | var p = new StoredPlot(zz, options, PlotTypes.ContourZZ); 162 | p.LabelContours = labelContours; 163 | PlotBuffer.Add(p); 164 | Plot(PlotBuffer); 165 | } 166 | 167 | public static void HeatMap(string filenameOrFunction, string options = "") 168 | { 169 | if (!Hold) PlotBuffer.Clear(); 170 | PlotBuffer.Add(new StoredPlot(filenameOrFunction, options, PlotTypes.ColorMapFileOrFunction)); 171 | Plot(PlotBuffer); 172 | } 173 | public static void HeatMap(int sizeY, double[] intensity, string options = "") 174 | { 175 | if (!Hold) PlotBuffer.Clear(); 176 | PlotBuffer.Add(new StoredPlot(sizeY, intensity, options, PlotTypes.ColorMapZ)); 177 | Plot(PlotBuffer); 178 | } 179 | public static void HeatMap(double[] x, double[] y, double[] intensity, string options = "") 180 | { 181 | if (!Hold) PlotBuffer.Clear(); 182 | PlotBuffer.Add(new StoredPlot(x, y, intensity, options, PlotTypes.ColorMapXYZ)); 183 | Plot(PlotBuffer); 184 | } 185 | public static void HeatMap(double[,] intensityGrid, string options = "") 186 | { 187 | if (!Hold) PlotBuffer.Clear(); 188 | PlotBuffer.Add(new StoredPlot(intensityGrid, options, PlotTypes.ColorMapZZ)); 189 | Plot(PlotBuffer); 190 | } 191 | 192 | public static void SPlot(string filenameOrFunction, string options = "") 193 | { 194 | if (!Hold) SPlotBuffer.Clear(); 195 | SPlotBuffer.Add(new StoredPlot(filenameOrFunction, options,PlotTypes.SplotFileOrFunction)); 196 | SPlot(SPlotBuffer); 197 | } 198 | public static void SPlot(int sizeY, double[] z, string options = "") 199 | { 200 | if (!Hold) SPlotBuffer.Clear(); 201 | SPlotBuffer.Add(new StoredPlot(sizeY, z, options)); 202 | SPlot(SPlotBuffer); 203 | } 204 | 205 | public static void SPlot(double[] x, double[] y, double[] z, string options = "") 206 | { 207 | if (!Hold) SPlotBuffer.Clear(); 208 | SPlotBuffer.Add(new StoredPlot(x, y, z, options)); 209 | SPlot(SPlotBuffer); 210 | } 211 | 212 | public static void SPlot(double[,] zz, string options = "") 213 | { 214 | if (!Hold) SPlotBuffer.Clear(); 215 | SPlotBuffer.Add(new StoredPlot(zz, options)); 216 | SPlot(SPlotBuffer); 217 | } 218 | 219 | 220 | public static void Plot(List storedPlots) 221 | { 222 | ReplotWithSplot = false; 223 | string plot = "plot "; 224 | string plotstring = ""; 225 | string contfile; 226 | string defcntopts; 227 | removeContourLabels(); 228 | for (int i = 0; i < storedPlots.Count; i++) 229 | { 230 | var p = storedPlots[i]; 231 | defcntopts = (p.Options.Length > 0 && (p.Options.Contains(" w") || p.Options[0] == 'w')) ? " " : " with lines "; 232 | switch (p.PlotType) 233 | { 234 | case PlotTypes.PlotFileOrFunction: 235 | if (p.File != null) 236 | plotstring += (plot + plotPath(p.File) + " " + p.Options); 237 | else 238 | plotstring += (plot + p.Function + " " + p.Options); 239 | break; 240 | case PlotTypes.PlotXY: 241 | case PlotTypes.PlotY: 242 | plotstring += (plot + @"""-"" " + p.Options); 243 | break; 244 | case PlotTypes.ContourFileOrFunction: 245 | contfile = Path.GetTempPath() + "_cntrtempdata" + i + ".dat"; 246 | makeContourFile((p.File != null ? plotPath(p.File) : p.Function), contfile); 247 | if (p.LabelContours) setContourLabels(contfile); 248 | plotstring += (plot + plotPath(contfile) + defcntopts + p.Options); 249 | break; 250 | case PlotTypes.ContourXYZ: 251 | contfile = Path.GetTempPath() + "_cntrtempdata" + i + ".dat"; 252 | makeContourFile(p.X, p.Y, p.Z, contfile); 253 | if (p.LabelContours) setContourLabels(contfile); 254 | plotstring += (plot + plotPath(contfile) + defcntopts + p.Options); 255 | break; 256 | case PlotTypes.ContourZZ: 257 | contfile = Path.GetTempPath() + "_cntrtempdata" + i + ".dat"; 258 | makeContourFile(p.ZZ, contfile); 259 | if (p.LabelContours) setContourLabels(contfile); 260 | plotstring += (plot + plotPath(contfile) + defcntopts + p.Options); 261 | break; 262 | case PlotTypes.ContourZ: 263 | contfile = Path.GetTempPath() + "_cntrtempdata" + i + ".dat"; 264 | makeContourFile(p.YSize, p.Z, contfile); 265 | if (p.LabelContours) setContourLabels(contfile); 266 | plotstring += (plot + plotPath(contfile) + defcntopts + p.Options); 267 | break; 268 | 269 | 270 | case PlotTypes.ColorMapFileOrFunction: 271 | if (p.File != null) 272 | plotstring += (plot + plotPath(p.File) + " with image " + p.Options); 273 | else 274 | plotstring += (plot + p.Function + " with image " + p.Options); 275 | break; 276 | case PlotTypes.ColorMapXYZ: 277 | case PlotTypes.ColorMapZ: 278 | plotstring += (plot + @"""-"" " + " with image " + p.Options); 279 | break; 280 | case PlotTypes.ColorMapZZ: 281 | plotstring += (plot + @"""-"" " + "matrix with image " + p.Options); 282 | break; 283 | } 284 | if (i == 0) plot = ", "; 285 | } 286 | GnupStWr.WriteLine(plotstring); 287 | 288 | for (int i = 0; i < storedPlots.Count; i++) 289 | { 290 | var p = storedPlots[i]; 291 | switch (p.PlotType) 292 | { 293 | case PlotTypes.PlotXY: 294 | WriteData(p.X, p.Y, GnupStWr, false); 295 | GnupStWr.WriteLine("e"); 296 | break; 297 | case PlotTypes.PlotY: 298 | WriteData(p.Y, GnupStWr, false); 299 | GnupStWr.WriteLine("e"); 300 | break; 301 | case PlotTypes.ColorMapXYZ: 302 | WriteData(p.X, p.Y, p.Z, GnupStWr, false); 303 | GnupStWr.WriteLine("e"); 304 | break; 305 | case PlotTypes.ColorMapZ: 306 | WriteData(p.YSize, p.Z, GnupStWr, false); 307 | GnupStWr.WriteLine("e"); 308 | break; 309 | case PlotTypes.ColorMapZZ: 310 | WriteData(p.ZZ, GnupStWr, false); 311 | GnupStWr.WriteLine("e"); 312 | GnupStWr.WriteLine("e"); 313 | break; 314 | 315 | } 316 | } 317 | GnupStWr.Flush(); 318 | } 319 | 320 | public static void SPlot(List storedPlots) 321 | { 322 | ReplotWithSplot = true; 323 | var splot = "splot "; 324 | string plotstring = ""; 325 | string defopts = ""; 326 | removeContourLabels(); 327 | for (int i = 0; i < storedPlots.Count; i++) 328 | { 329 | var p = storedPlots[i]; 330 | defopts = (p.Options.Length > 0 && (p.Options.Contains(" w") || p.Options[0] == 'w')) ? " " : " with lines "; 331 | switch (p.PlotType) 332 | { 333 | case PlotTypes.SplotFileOrFunction: 334 | if (p.File != null) 335 | plotstring += (splot + plotPath(p.File) + defopts + p.Options); 336 | else 337 | plotstring += (splot + p.Function + defopts + p.Options); 338 | break; 339 | case PlotTypes.SplotXYZ: 340 | case PlotTypes.SplotZ: 341 | plotstring += (splot + @"""-"" " + defopts + p.Options); 342 | break; 343 | case PlotTypes.SplotZZ: 344 | plotstring += (splot + @"""-"" matrix " + defopts + p.Options); 345 | break; 346 | } 347 | if (i == 0) splot = ", "; 348 | } 349 | GnupStWr.WriteLine(plotstring); 350 | 351 | for (int i = 0; i < storedPlots.Count; i++) 352 | { 353 | var p = storedPlots[i]; 354 | switch (p.PlotType) 355 | { 356 | case PlotTypes.SplotXYZ: 357 | WriteData(p.X, p.Y, p.Z, GnupStWr, false); 358 | GnupStWr.WriteLine("e"); 359 | break; 360 | case PlotTypes.SplotZZ: 361 | WriteData(p.ZZ, GnupStWr, false); 362 | GnupStWr.WriteLine("e"); 363 | GnupStWr.WriteLine("e"); 364 | break; 365 | case PlotTypes.SplotZ: 366 | WriteData(p.YSize, p.Z, GnupStWr, false); 367 | GnupStWr.WriteLine("e"); 368 | break; 369 | } 370 | } 371 | GnupStWr.Flush(); 372 | } 373 | 374 | public static void WriteData(double[] y, StreamWriter stream, bool flush = true) 375 | { 376 | for (int i = 0; i < y.Length; i++) 377 | stream.WriteLine(y[i].ToString()); 378 | 379 | if (flush) stream.Flush(); 380 | } 381 | 382 | public static void WriteData(double[] x, double[] y, StreamWriter stream, bool flush = true) 383 | { 384 | for (int i = 0; i < y.Length; i++) 385 | stream.WriteLine(x[i].ToString() + " " + y[i].ToString()); 386 | 387 | if (flush) stream.Flush(); 388 | } 389 | 390 | public static void WriteData(int ySize, double[] z, StreamWriter stream, bool flush = true) 391 | { 392 | for (int i = 0; i < z.Length; i++) 393 | { 394 | if (i > 0 && i % ySize == 0) 395 | stream.WriteLine(); 396 | stream.WriteLine(z[i].ToString()); 397 | } 398 | 399 | if (flush) stream.Flush(); 400 | } 401 | 402 | public static void WriteData(double[,] zz, StreamWriter stream, bool flush = true) 403 | { 404 | int m = zz.GetLength(0); 405 | int n = zz.GetLength(1); 406 | string line; 407 | for (int i = 0; i < m; i++) 408 | { 409 | line = ""; 410 | for (int j = 0; j < n; j++) 411 | line += zz[i, j].ToString() + " "; 412 | stream.WriteLine(line.TrimEnd()); 413 | } 414 | 415 | if (flush) stream.Flush(); 416 | } 417 | 418 | public static void WriteData(double[] x, double[] y, double[] z, StreamWriter stream, bool flush = true) 419 | { 420 | int m = Math.Min(x.Length, y.Length); 421 | m = Math.Min(m, z.Length); 422 | for (int i = 0; i < m; i++) 423 | { 424 | if (i > 0 && x[i] != x[i - 1]) 425 | stream.WriteLine(""); 426 | stream.WriteLine(x[i] + " " + y[i] + " " + z[i]); 427 | } 428 | 429 | if (flush) stream.Flush(); 430 | } 431 | 432 | static string plotPath(string path) 433 | { 434 | return "\"" + path.Replace(@"\", @"\\") + "\""; 435 | } 436 | 437 | public static void SaveSetState(string filename = null) 438 | { 439 | if (filename == null) 440 | filename = Path.GetTempPath() + "setstate.tmp"; 441 | GnupStWr.WriteLine("save set " + plotPath(filename)); 442 | GnupStWr.Flush(); 443 | waitForFile(filename); 444 | } 445 | public static void LoadSetState(string filename = null) 446 | { 447 | if (filename == null) 448 | filename = Path.GetTempPath() + "setstate.tmp"; 449 | GnupStWr.WriteLine("load " + plotPath(filename)); 450 | GnupStWr.Flush(); 451 | } 452 | 453 | //these makecontourFile functions should probably be merged into one function and use a StoredPlot parameter 454 | static void makeContourFile(string fileOrFunction, string outputFile)//if it's a file, fileOrFunction needs quotes and escaped backslashes 455 | { 456 | SaveSetState(); 457 | Set("table " + plotPath(outputFile)); 458 | Set("contour base"); 459 | Unset("surface"); 460 | GnupStWr.WriteLine(@"splot " + fileOrFunction); 461 | Unset("table"); 462 | GnupStWr.Flush(); 463 | LoadSetState(); 464 | waitForFile(outputFile); 465 | } 466 | 467 | static void makeContourFile(double[] x, double[] y, double[] z, string outputFile) 468 | { 469 | SaveSetState(); 470 | Set("table " + plotPath(outputFile)); 471 | Set("contour base"); 472 | Unset("surface"); 473 | GnupStWr.WriteLine(@"splot ""-"""); 474 | WriteData(x, y, z, GnupStWr); 475 | GnupStWr.WriteLine("e"); 476 | Unset("table"); 477 | GnupStWr.Flush(); 478 | LoadSetState(); 479 | waitForFile(outputFile); 480 | } 481 | 482 | static void makeContourFile(double[,] zz, string outputFile) 483 | { 484 | SaveSetState(); 485 | Set("table " + plotPath(outputFile)); 486 | Set("contour base"); 487 | Unset("surface"); 488 | GnupStWr.WriteLine(@"splot ""-"" matrix"); 489 | WriteData(zz, GnupStWr); 490 | GnupStWr.WriteLine("e"); 491 | GnupStWr.WriteLine("e"); 492 | Unset("table"); 493 | GnupStWr.Flush(); 494 | LoadSetState(); 495 | waitForFile(outputFile); 496 | } 497 | 498 | static void makeContourFile(int sizeY, double[] z, string outputFile) 499 | { 500 | SaveSetState(); 501 | Set("table " + plotPath(outputFile)); 502 | Set("contour base"); 503 | Unset("surface"); 504 | GnupStWr.WriteLine(@"splot ""-"""); 505 | WriteData(sizeY, z, GnupStWr); 506 | GnupStWr.WriteLine("e"); 507 | Unset("table"); 508 | GnupStWr.Flush(); 509 | LoadSetState(); 510 | waitForFile(outputFile); 511 | } 512 | 513 | static int contourLabelCount = 50000; 514 | static void setContourLabels(string contourFile) 515 | { 516 | var file = new System.IO.StreamReader(contourFile); 517 | string line; 518 | while ((line = file.ReadLine()) != null) 519 | { 520 | if (line.Contains("label:")) 521 | { 522 | string[] c = file.ReadLine().Trim().Replace(" ", " ").Replace(" ", " ").Split(' '); 523 | GnupStWr.WriteLine("set object " + ++contourLabelCount + " rectangle center " + c[0] + "," + c[1] + " size char " + (c[2].ToString().Length + 1) + ",char 1 fs transparent solid .7 noborder fc rgb \"white\" front"); 524 | GnupStWr.WriteLine("set label " + contourLabelCount + " \"" + c[2] + "\" at " + c[0] + "," + c[1] + " front center"); 525 | } 526 | } 527 | file.Close(); 528 | } 529 | static void removeContourLabels() 530 | { 531 | while (contourLabelCount > 50000) 532 | GnupStWr.WriteLine("unset object " + contourLabelCount + ";unset label " + contourLabelCount--); 533 | } 534 | 535 | static bool waitForFile(string filename, int timeout = 10000) 536 | { 537 | Thread.Sleep(20); 538 | int attempts = timeout / 100; 539 | System.IO.StreamReader file = null; 540 | while (file == null) 541 | { 542 | try { file = new System.IO.StreamReader(filename); } 543 | catch 544 | { 545 | if (attempts-- > 0) 546 | Thread.Sleep(100); 547 | else 548 | return false; 549 | } 550 | } 551 | file.Close(); 552 | return true; 553 | } 554 | 555 | public static void HoldOn() 556 | { 557 | Hold = true; 558 | PlotBuffer.Clear(); 559 | SPlotBuffer.Clear(); 560 | } 561 | 562 | public static void HoldOff() 563 | { 564 | Hold = false; 565 | PlotBuffer.Clear(); 566 | SPlotBuffer.Clear(); 567 | } 568 | 569 | public static void Close() 570 | { 571 | ExtPro.CloseMainWindow(); 572 | } 573 | 574 | } 575 | 576 | enum PointStyles 577 | { 578 | Dot = 0, 579 | Plus = 1, 580 | X = 2, 581 | Star = 3, 582 | DotSquare = 4, 583 | SolidSquare = 5, 584 | DotCircle = 6, 585 | SolidCircle = 7, 586 | DotTriangleUp = 8, 587 | SolidTriangleUp = 9, 588 | DotTriangleDown = 10, 589 | SolidTriangleDown = 11, 590 | DotDiamond = 12, 591 | SolidDiamond = 13 592 | } 593 | 594 | enum PlotTypes 595 | { 596 | PlotFileOrFunction, 597 | PlotY, 598 | PlotXY, 599 | ContourFileOrFunction, 600 | ContourXYZ, 601 | ContourZZ, 602 | ContourZ, 603 | ColorMapFileOrFunction, 604 | ColorMapXYZ, 605 | ColorMapZZ, 606 | ColorMapZ, 607 | SplotFileOrFunction, 608 | SplotXYZ, 609 | SplotZZ, 610 | SplotZ 611 | } 612 | 613 | class StoredPlot 614 | { 615 | public string File = null; 616 | public string Function = null; 617 | public double[] X; 618 | public double[] Y; 619 | public double[] Z; 620 | public double[,] ZZ; 621 | public int YSize; 622 | public string Options; 623 | public PlotTypes PlotType; 624 | public bool LabelContours; 625 | 626 | public StoredPlot() 627 | { 628 | } 629 | public StoredPlot(string functionOrfilename, string options = "", PlotTypes plotType = PlotTypes.PlotFileOrFunction) 630 | { 631 | if (IsFile(functionOrfilename)) 632 | File = functionOrfilename; 633 | else 634 | Function = functionOrfilename; 635 | Options = options; 636 | PlotType = plotType; 637 | } 638 | 639 | public StoredPlot(double[] y, string options = "") 640 | { 641 | Y = y; 642 | Options = options; 643 | PlotType = PlotTypes.PlotY; 644 | } 645 | 646 | public StoredPlot(double[] x, double[] y, string options = "") 647 | { 648 | X = x; 649 | Y = y; 650 | Options = options; 651 | PlotType = PlotTypes.PlotXY; 652 | } 653 | 654 | //3D data 655 | public StoredPlot(int sizeY, double[] z, string options = "", PlotTypes plotType = PlotTypes.SplotZ) 656 | { 657 | YSize = sizeY; 658 | Z = z; 659 | Options = options; 660 | PlotType = plotType; 661 | } 662 | 663 | public StoredPlot(double[] x, double[] y, double[] z, string options = "", PlotTypes plotType = PlotTypes.SplotXYZ) 664 | { 665 | if (x.Length < 2) 666 | YSize = 1; 667 | else 668 | for (YSize = 1; YSize < x.Length; YSize++) 669 | if (x[YSize] != x[YSize - 1]) 670 | break; 671 | Z = z; 672 | Y = y; 673 | X = x; 674 | Options = options; 675 | PlotType = plotType; 676 | } 677 | 678 | public StoredPlot(double[,] zz, string options = "", PlotTypes plotType = PlotTypes.SplotZZ) 679 | { 680 | ZZ = zz; 681 | Options = options; 682 | PlotType = plotType; 683 | } 684 | 685 | private bool IsFile(string functionOrFilename) 686 | { 687 | int dot = functionOrFilename.LastIndexOf("."); 688 | if (dot < 1) return false; 689 | if (char.IsLetter(functionOrFilename[dot - 1]) || char.IsLetter(functionOrFilename[dot + 1])) 690 | return true; 691 | return false; 692 | } 693 | 694 | } 695 | 696 | 697 | } 698 | -------------------------------------------------------------------------------- /GnuplotCSharp.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {F26034B7-2EB2-49D1-949B-893C74925D53} 9 | Exe 10 | Properties 11 | GnuplotCSharp 12 | GnuplotCSharp 13 | v4.0 14 | Client 15 | 512 16 | 17 | 18 | x86 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | x86 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | PreserveNewest 53 | 54 | 55 | 56 | 63 | -------------------------------------------------------------------------------- /GnuplotCSharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual C# Express 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GnuplotCSharp", "GnuplotCSharp.csproj", "{F26034B7-2EB2-49D1-949B-893C74925D53}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {F26034B7-2EB2-49D1-949B-893C74925D53}.Debug|x86.ActiveCfg = Debug|x86 13 | {F26034B7-2EB2-49D1-949B-893C74925D53}.Debug|x86.Build.0 = Debug|x86 14 | {F26034B7-2EB2-49D1-949B-893C74925D53}.Release|x86.ActiveCfg = Release|x86 15 | {F26034B7-2EB2-49D1-949B-893C74925D53}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | You are free to use this code as you wish. Please mention you got it on GitHub from James Morris aka AwokeKnowing. Also, email me at james david morris a/t g mail .com (no spaces) and let me know about your project. 2 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("GnuplotCSharp")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("GnuplotCSharp")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("076bd764-055b-47b8-bc02-ef511143c6e2")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gnuplot.C# 2 | ======== 3 | 4 | **The GnuplotCSharp project makes it easy to use gnuplot in C# applications by adding a single file ([GnuPlot.cs](GnuPlot.cs)) to your existing Visual Studio project.** Gnuplot has a [richly documented set of commands](http://gnuplot.sourceforge.net/demo/), and this project brings these advanced graphing capabilities into the C# / Visual Studio programming environment. 5 | 6 | Note: gnuplot executable must be available on the system, or can be included in your project. 7 | 8 | Overview 9 | -------- 10 | Many popular scientific publications with graphs use gnuplot. It is extremely well documented and handles 2D, 3D (surface and pointcloud), heatmap, png, jpg, and much more using simple math syntax as well as simple text-based data. And it supports many output formats, as well interactive zooming/rotating. 11 | 12 | It only takes about 10 lines of code to wire up C# to send commands to gnuplot. But as soon as you try to send arrays of data, overlay multiple graphs, and generally work with gnuplot from C#, you'll immediately see that you have to add all kinds of utility functions to not clutter up your code. 13 | 14 | Gnuplot.C# provides a nice set of functions to make it easy to use all the power of gnuplot to visualize your data in C#. Check out the examples of how easy it is to visualize functions and data. 15 | 16 | Installation 17 | ---------- 18 | Just put gnuplot.cs in your project, change the first line from C:\gnuplot\bin to the location of gnuplot.exe on your system. 19 | 20 | If you haven't installed gnuplot on your system, download it at http://sourceforge.net/projects/gnuplot/files/ or http://www.gnuplot.info 21 | 22 | Inspiration 23 | ----------- 24 | In 2012, I completed the excellent Machine Learning course by Andrew Ng (Coursera). We used Octave/Matlab, and Octave uses gnuplot for its graphs. I wanted to recreate all the class projects in C# for practice. Microsoft has a cloud numerics library, so C# is a good choice for machine learning if you want to for example, train your machine learning algorithm on a large dataset and scale across many computers in the Azure cloud. 25 | 26 | I believe this project will be helpful to anyone who wants to visualize data/functions in C#, so I have released it here. 27 | 28 | Examples 29 | ======== 30 | To see various demos in action, download the files, open the solution (Visual Studio) and run Demo.cs (make sure you've installed gnuplot first) 31 | 32 | If you are not familiar with gnuplot, I recommend you visit www.gnuplot.info and see all the demos there. Then come back for how to do it all in C#. 33 | 34 | Plot 35 | -------- 36 | 37 | **Plot a function** 38 | ```C# 39 | GnuPlot.Plot("sin(x) + 2"); 40 | ``` 41 | ![Plot a function](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/plotf.png) 42 | ------- 43 |

44 | 45 | **Plot a function with custom color and line width** (see gnuplot documentation) 46 | ```C# 47 | GnuPlot.Plot("sin(x) + 2", "lc rgb 'magenta' lw 5"); 48 | ``` 49 | ![Plot a function](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/plotf2.png) 50 | ------- 51 |

52 | 53 | **Plot an array of y values** 54 | ```C# 55 | double[] Y = new double[] { -4, 6.5, -2, 3, -8, -5, 11, 4, -5, 10 }; 56 | GnuPlot.Plot(Y); 57 | ``` 58 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/plotY.png) 59 | ------- 60 |

61 | 62 | **Plot an array of x and y values** (notice the x values vs previous graph) 63 | ```C# 64 | double[] X = new double[] { -10, -8.5, -2, 1, 6, 9, 10, 14, 15, 19 }; 65 | double[] Y = new double[] { -4, 6.5, -2, 3, -8, -5, 11, 4, -5, 10 }; 66 | GnuPlot.Plot(X, Y); 67 | ``` 68 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/plotXY.png) 69 | ------- 70 |

71 | 72 | **Overlay multiple graphs** (HoldOn causes future plots to be overlayed. HoldOff make future plots replace previous. 73 | ```C# 74 | GnuPlot.HoldOn(); 75 | GnuPlot.Plot("cos(x) + x"); 76 | GnuPlot.Plot("cos(2*x)", "with points pt 3"); 77 | ``` 78 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/plotOverlay.png) 79 | ------- 80 |

81 | 82 | Splot (3D data) 83 | -------- 84 | 85 | **plot a 3D function** 86 | ```C# 87 | GnuPlot.SPlot("1 / (.05*x*x + .05*y*y + 1)"); 88 | ``` 89 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splot1.png) 90 | ------- 91 |

92 | 93 | Set the number of lines with **isosamples** to control "detail" (see gnuplot documentation) 94 | ```C# 95 | GnuPlot.Set("isosamples 30"); 96 | GnuPlot.SPlot("1 / (.05*x*x + .05*y*y + 1)"); 97 | ``` 98 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splot2.png) 99 | ------- 100 |

101 | 102 | Hide back faces with **hidden3d** to make it "solid" (see gnuplot documentation) 103 | ```C# 104 | GnuPlot.Set("isosamples 30", "hidden3d"); 105 | GnuPlot.SPlot("1 / (.05*x*x + .05*y*y + 1)"); 106 | ``` 107 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splot3.png) 108 | ------- 109 |

110 | 111 | **plot a file with 3D data** 112 | __data.txt__ (this can be in any format accepted by gnuplot. Here it's x,y,z points for two separate polygons) 113 | ``` 114 | -1 -1 -1 115 | -1 1 -1 116 | -.1 .1 1 117 | -.1 -.1 1 118 | -1 -1 -1 119 | 120 | 121 | .1 .1 1 122 | .1 -.1 1 123 | 1 -1 -1 124 | 1 1 -1 125 | .1 .1 1 126 | 127 | ``` 128 | 129 | **to plot the above file** 130 | ```C# 131 | splot ("data.txt"); 132 | ``` 133 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splotfile.png) 134 | ------- 135 |

136 | 137 | **plot an array of z values** (specify y size to break the array into a square grid) 138 | ```C# 139 | double[] Z = new double[] { -4, -2.5, 1, 3, -3, -2, 3, 4, -1, 2, 6, 8 }; 140 | GnuPlot.Set("pm3d"); //color planes by z value 141 | GnuPlot.SPlot(4, Z); //split the 12 z values into rows of 4 points 142 | ``` 143 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splotSZ.png) 144 | ------- 145 |

146 | 147 | **plot a grid of z values** and use some styling (see gnuplot docs for style commands) 148 | ```C# 149 | double[,] Z = new double[,] { {-4,-2.5,1,3}, {-3,-2,3,4}, {-1,2,6,8 } }; 150 | GnuPlot.Set("pm3d","palette gray"); //we'll make monochrome color based on height of the plane 151 | GnuPlot.SPlot(Z,"with points pointtype 6"); //we'll try with points at vertexes instead of lines 152 | ``` 153 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splotZZ.png) 154 | ------- 155 |

156 | 157 | **plot a point cloud of x, y, and z values** 158 | ```C# 159 | //make some random data points 160 | double[] X = new double[100]; 161 | double[] Y = new double[100]; 162 | double[] Z = new double[100]; 163 | Random r=new Random(); 164 | for (int i = 0; i < 100; i++) 165 | { 166 | X[i] = r.Next(30) - 15; 167 | Y[i] = r.Next(50) - 25; 168 | Z[i] = r.Next(20) - 10; 169 | } 170 | 171 | //set the range for the x,y,z axis and plot (using pointtype triangle and color blue) 172 | GnuPlot.Set("xrange[-30:30]", "yrange[-30:30]", "zrange[-30:30]"); 173 | GnuPlot.SPlot(X, Y, Z, "with points pointtype 8 lc rgb 'blue'"); 174 | ``` 175 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splotxyz1.png) 176 | ------- 177 |

178 | 179 | **plot a surface based on random unordered x, y, and z values** 180 | ```C# 181 | //make 20 random data points 182 | double[] X = new double[20]; 183 | double[] Y = new double[20]; 184 | double[] Z = new double[20]; 185 | Random r=new Random(); 186 | for (int i = 0; i < 20; i++) 187 | { 188 | X[i] = r.Next(30) - 15; 189 | Y[i] = r.Next(50) - 25; 190 | Z[i] = r.Next(20) - 10; 191 | } 192 | 193 | //fit the points to a surface grid of 40x40 with smoothing level 2 194 | GnuPlot.Set("dgrid3d 40,40,2"); 195 | 196 | //set the range for the x,y,z axis and plot (using pm3d to map height to color) 197 | GnuPlot.Set("xrange[-30:30]", "yrange[-30:30]", "zrange[-30:30]"); 198 | GnuPlot.SPlot(X, Y, Z,"with pm3d"); 199 | ``` 200 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/splotxyz2.png) 201 | ------- 202 |

203 | 204 | 205 | Contour (3D data as "top view" with contour lines) 206 | -------- 207 | Contour maps are very useful to make it easier to see the peaks and valleys, and see what areas have the same range of values. Gnuplot lets you show contour maps, but you can't draw 2d points and lines on them. The Contour methods generate a 2D contour map, allowing you to plot further data points on it. 208 | 209 | ```C# 210 | GnuPlot.Unset("key"); //hide the key or legend 211 | GnuPlot.Set("cntrparam levels 20","isosamples 50", "xr[-5:5]","yr[-6:6]"); //notice cntrparam levels (# height levels) 212 | GnuPlot.Contour("sin(x) * cos(y)+x","lc rgb 'blue'"); //plot a 3d function (or data) 213 | ``` 214 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/contourfunc.png) 215 | ------- 216 |

217 | 218 | 219 | 220 | Heatmap (intensity/z-scale map) 221 | -------- 222 | 223 | ```C# 224 | double[,] Z = new double[,]{{0,0,0,1,2,2,1,0,0,0}, 225 | {0,0,2,3,3,3,3,2,0,0}, 226 | {0,2,3,4,4,4,4,3,2,0}, 227 | {2,3,4,5,5,5,5,4,3,2}, 228 | {3,4,5,6,7,7,6,5,4,3}, 229 | {3,4,5,6,7,7,6,5,4,3}, 230 | {2,3,4,5,5,5,5,4,3,2}, 231 | {0,2,3,4,4,4,4,3,2,0}, 232 | {0,0,2,3,3,3,3,2,0,0}, 233 | {0,0,0,1,2,2,1,0,0,0}}; 234 | GnuPlot.HeatMap(Z); 235 | ``` 236 | ![Plot data](https://raw.github.com/AwokeKnowing/GnuplotCSharp/master/ReadmeImages/heatmap.png) 237 | ------- 238 |

239 | 240 | 241 | Set and Unset 242 | -------- 243 | 244 | ```C# 245 | GnuPlot.HoldOn(); 246 | GnuPlot.Set("title 'Phase-Locked Signals'"); 247 | GnuPlot.Set("samples 2000"); 248 | GnuPlot.Unset("key"); 249 | GnuPlot.Plot("sin(x)"); 250 | GnuPlot.Plot("cos(x)"); 251 | ``` 252 | 253 | ![Set and Unset](/ReadmeImages/phase.png) 254 | 255 | Replot 256 | -------- 257 | 258 | ```C# 259 | 260 | ``` 261 | 262 | Write and WriteLine 263 | -------- 264 | 265 | ```C# 266 | 267 | ``` 268 | 269 | SaveData 270 | -------- 271 | 272 | ```C# 273 | 274 | ``` 275 | 276 | PointStyles 277 | -------- 278 | 279 | ```C# 280 | 281 | ``` 282 | 283 | StoredPlot 284 | -------- 285 | 286 | ```C# 287 | 288 | 289 | ``` 290 | 291 | API Reference 292 | --- 293 | Plot 294 | 295 | Splot 296 | 297 | Contour 298 | 299 | Heatmap 300 | 301 | Set 302 | 303 | Unset 304 | 305 | HoldOn 306 | 307 | HoldOff 308 | 309 | SaveData 310 | 311 | WriteLine 312 | 313 | Write 314 | 315 | Replot 316 | 317 | Disclaimer 318 | ---------- 319 | Use this code at your own risk. The author cannot guarantee that it is free of defects or that it will work as you expect. It's less than 1000 lines of code, so just look at it, and decide if it works for you. 320 | 321 | Happy coding!! 322 | 323 | 324 | -------------------------------------------------------------------------------- /ReadmeImages/contourfunc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/contourfunc.png -------------------------------------------------------------------------------- /ReadmeImages/heatmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/heatmap.png -------------------------------------------------------------------------------- /ReadmeImages/phase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/phase.png -------------------------------------------------------------------------------- /ReadmeImages/plotOverlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/plotOverlay.png -------------------------------------------------------------------------------- /ReadmeImages/plotXY.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/plotXY.png -------------------------------------------------------------------------------- /ReadmeImages/plotY.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/plotY.png -------------------------------------------------------------------------------- /ReadmeImages/plotf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/plotf.png -------------------------------------------------------------------------------- /ReadmeImages/plotf2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/plotf2.png -------------------------------------------------------------------------------- /ReadmeImages/splot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splot1.png -------------------------------------------------------------------------------- /ReadmeImages/splot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splot2.png -------------------------------------------------------------------------------- /ReadmeImages/splot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splot3.png -------------------------------------------------------------------------------- /ReadmeImages/splotSZ.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splotSZ.png -------------------------------------------------------------------------------- /ReadmeImages/splotZZ.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splotZZ.png -------------------------------------------------------------------------------- /ReadmeImages/splotfile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splotfile.png -------------------------------------------------------------------------------- /ReadmeImages/splotxyz1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splotxyz1.png -------------------------------------------------------------------------------- /ReadmeImages/splotxyz2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AwokeKnowing/GnuplotCSharp/373ad6b4f12679ea84ae1744440f4a3c1a912f45/ReadmeImages/splotxyz2.png -------------------------------------------------------------------------------- /splotexampledata.txt: -------------------------------------------------------------------------------- 1 | -1 -1 -1 2 | -1 1 -1 3 | -.1 .1 1 4 | -.1 -.1 1 5 | -1 -1 -1 6 | 7 | 8 | .1 .1 1 9 | .1 -.1 1 10 | 1 -1 -1 11 | 1 1 -1 12 | .1 .1 1 13 | 14 | --------------------------------------------------------------------------------