Esempio n. 1
0
        /// <summary>
        /// Draws a graph with Gnuplot and saves it as a gif. Subsequently sets Picture of Form to the plot.
        /// </summary>
        private void drawGnuplotGraph()
        {
            using (Gnuplot gp = new Gnuplot()) {
                gp.SetXLabel("Iteration");
                gp.SetYLabel(log.Norm + " Norm");
                gp.Cmd("set title \"Residual Plot\"");
                gp.Cmd("set key outside under horizontal box");
                gp.Cmd("set logscale y");
                gp.Cmd("set format y \"10^{%L}\"");
                gp.Cmd("set grid xtics ytics");

                List <string> KeysToPlot = log.Values.Keys.ToList();
                KeysToPlot.Remove("#Line");
                KeysToPlot.Remove("#Time");
                KeysToPlot.Remove("#Iter");

                for (int i = 0; i < KeysToPlot.Count; i++)
                {
                    gp.PlotXY(log.Values["#Time"], log.Values[KeysToPlot[i]], KeysToPlot[i],
                              new PlotFormat(lineColor: ((LineColors)i), Style: Styles.Lines));
                }

                Image graph = gp.PlotGIF();
                pictureBox1.Image = graph;
                pictureBox1.Refresh();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Plots a line plot from a given residual log.
        /// The y-axis will be scaled logarithmically.
        /// </summary>
        private Image drawGnuplotGraph()
        {
            Image graph;

            using (Gnuplot gp = new Gnuplot()) {
                gp.SetXLabel("Iteration");
                gp.SetYLabel(log.Norm + " Norm");
                //gp.Cmd("set terminal wxt noraise title 'SessionGuid: " + log.SessionGuid.ToString() + "'");
                gp.Cmd("set title " + titleOfPlot);
                gp.Cmd("set key outside under horizontal box");
                gp.Cmd("set logscale y");
                gp.Cmd("set format y \"10^{%L}\"");
                gp.Cmd("set grid xtics ytics");

                List <string> KeysToPlot = log.Values.Keys.ToList();
                KeysToPlot.Remove("#Line");
                KeysToPlot.Remove("#Time");
                KeysToPlot.Remove("#Iter");

                for (int i = 0; i < KeysToPlot.Count; i++)
                {
                    gp.PlotXY(log.Values["#Time"], log.Values[KeysToPlot[i]], KeysToPlot[i],
                              new PlotFormat(lineColor: ((LineColors)i), Style: Styles.Lines));
                }

                graph = gp.PlotGIF();
            }

            return(graph);
        }
Esempio n. 3
0
        /// <summary>
        /// plot a 2D grid
        /// </summary>
        public static void Plot2DGrid(this GridCommons grd, params int[] Cells)
        {
            using (var gp = new Gnuplot()) {
                Console.WriteLine("Plotting 2D grid with gnuplot...");

                if (grd.SpatialDimension != 2)
                {
                    throw new NotSupportedException("works only for 2D grid");
                }


                int J = grd.Cells.Length;
                if (Cells == null)
                {
                    Cells = J.ForLoop(j => j);
                }

                foreach (int j in Cells)
                {
                    var Cell_j = grd.Cells[j];
                    //var Kref = grd.RefElements.Single(KK => KK.SupportedCellTypes.Contains(Cell_j.Type));

                    //var Vtx = Kref.Vertices;
                    //var _Vtx = Kref.GetSubDivTree(3).GlobalVertice;
                    //var Vtx = MultidimensionalArray.Create(_Vtx.GetLength(0), _Vtx.GetLength(1));
                    //Vtx.SetA2d(_Vtx);
                    //Vtx.Scale(1);

                    var Vtx_glob = Cell_j.TransformationParams;


                    double[] xNodes = Vtx_glob.ExtractSubArrayShallow(-1, 0).To1DArray();
                    double[] yNodes = Vtx_glob.ExtractSubArrayShallow(-1, 1).To1DArray();
                    double   xC     = xNodes.Sum() / xNodes.Length;
                    double   yC     = yNodes.Sum() / yNodes.Length;
                    for (int k = 0; k < xNodes.Length; k++)
                    {
                        double dx = xNodes[k] - xC;
                        dx       *= 0.95;
                        xNodes[k] = xC + dx;

                        double dy = yNodes[k] - yC;
                        dy       *= 0.95;
                        yNodes[k] = yC + dy;
                    }

                    double hy = yNodes.Max() - yNodes.Min();

                    gp.PlotXY(xNodes, yNodes, title: null, format: (new PlotFormat(Style: Styles.LinesPoints, lineColor: ((LineColors)j))));

                    gp.Cmd("set label \"{0}\" at {2},{3} font \"Arial,5\"", j.ToString(), Cell_j.GlobalID, xC.ToStringDot(), (yC + hy * 0.21).ToStringDot());
                    gp.Cmd("set label \"[{1}]\" at {2},{3} font \"Arial,5\"", j.ToString(), Cell_j.GlobalID, xC.ToStringDot(), (yC - hy * 0.21).ToStringDot());
                }

                gp.Execute();

                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Visualizes the stored data using gnuplot.
        /// </summary>
        public void Plot()
        {
            Gnuplot gp = new Gnuplot();

            gp.SetXLabel("x");
            gp.SetYLabel("y");
            gp.Cmd("set terminal wxt noraise");
            if (LogX)
            {
                gp.Cmd("set logscale x");
                gp.Cmd("set format x \"10^{%L}\"");
            }
            if (LogY)
            {
                gp.Cmd("set logscale y");
                gp.Cmd("set format y \"10^{%L}\"");
            }
            gp.Cmd("set grid xtics ytics");

            int lineColor = 0;

            foreach (var group in dataGroups)
            {
                gp.PlotXY(group.Abscissas, group.Values, group.Name,
                          new PlotFormat(lineColor: ((LineColors)(++lineColor)), pointType: ((PointTypes)4), pointSize: 1.5, Style: Styles.LinesPoints));
            }
            gp.Execute();
        }
Esempio n. 5
0
        /// <summary>
        /// Utility/Debug function, plots numbers at specific coordinates.
        /// </summary>
        /// <param name="X"></param>
        /// <param name="filename"></param>
        public static void PlotCoordinateLabels(this MultidimensionalArray X, string filename)
        {
            using (var gp = new Gnuplot()) {
                int L = X.NoOfRows;
                if (X.NoOfCols != 2)
                {
                    throw new NotSupportedException("works only for 2D grid");
                }

                gp.Cmd("set terminal png");
                gp.Cmd("set output \"{0}\"", filename);

                gp.SetXRange(-4, 4);
                gp.SetYRange(-4, 4);

                for (int l = 0; l < L; l++)
                {
                    gp.Cmd("set label \"{0}\" at {1},{2}", l, X[l, 0], X[l, 1]);
                }

                gp.PlotSlope(0, 0);
                gp.Execute();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Writes plot commands to a gnuplot object.
        /// </summary>
        public void ToGnuplot(Gnuplot gp)
        {
            //gp.Cmd($"set terminal enhanced font \"Helvetica, 30\" ");

            // =======
            // margins
            // =======
            {
                if (lmargin != null)
                {
                    gp.Cmd("set lmargin {0:0.####e-00}", this.lmargin.Value);
                }
                else
                {
                    gp.Cmd("unset lmargin ");
                }

                if (rmargin != null)
                {
                    gp.Cmd("set rmargin {0:0.####e-00}", this.rmargin.Value);
                }
                else
                {
                    gp.Cmd("unset rmargin ");
                }

                if (tmargin != null)
                {
                    gp.Cmd("set tmargin {0:0.####e-00}", this.tmargin.Value);
                }
                else
                {
                    gp.Cmd("unset tmargin ");
                }

                if (bmargin != null)
                {
                    gp.Cmd("set bmargin {0:0.####e-00}", this.bmargin.Value);
                }
                else
                {
                    gp.Cmd("unset bmargin ");
                }
            }

            // ============
            // log settings
            // ============
            {
                if (this.LogX)
                {
                    gp.Cmd("set logscale x " + this.LogBaseX);
                }
                else
                {
                    gp.Cmd("unset logscale x");
                }

                if (this.LogY)
                {
                    gp.Cmd("set logscale y " + this.LogBaseY);
                }
                else
                {
                    gp.Cmd("unset logscale y");
                }

                if (this.LogX2)
                {
                    gp.Cmd("set logscale x2 " + this.LogBaseX);
                }
                else
                {
                    gp.Cmd("unset logscale x2");
                }

                if (this.LogY2)
                {
                    gp.Cmd("set logscale y2 " + this.LogBaseY);
                }
                else
                {
                    gp.Cmd("unset logscale y2");
                }
            }

            // ==========
            // axis range
            // ==========

            {
                if ((this.XrangeMax != null) != (this.XrangeMin != null))
                {
                    throw new ArgumentException("X range minimum and maximum must be set either both or none.");
                }
                if ((this.YrangeMax != null) != (this.YrangeMin != null))
                {
                    throw new ArgumentException("Y range minimum and maximum must be set either both or none.");
                }

                if (this.XrangeMin != null)
                {
                    if (this.XrangeMin.Value >= this.XrangeMax.Value)
                    {
                        throw new ArgumentException("X range maximum must be grater than minimum.");
                    }

                    gp.SetXRange(this.XrangeMin.Value, this.XrangeMax.Value);
                }
                else
                {
                    gp.SetXAutorange();
                }

                if (this.YrangeMin != null)
                {
                    if (this.YrangeMin.Value >= this.YrangeMax.Value)
                    {
                        throw new ArgumentException("Y range maximum must be grater than minimum.");
                    }

                    gp.SetYRange(this.YrangeMin.Value, this.YrangeMax.Value);
                }
                else
                {
                    gp.SetYAutorange();
                }


                if ((this.X2rangeMax != null) != (this.X2rangeMin != null))
                {
                    throw new ArgumentException("X2 range minimum and maximum must be set either both or none.");
                }
                if ((this.Y2rangeMax != null) != (this.Y2rangeMin != null))
                {
                    throw new ArgumentException("Y2 range minimum and maximum must be set either both or none.");
                }

                if (this.X2rangeMin != null)
                {
                    if (this.X2rangeMin.Value >= this.X2rangeMax.Value)
                    {
                        throw new ArgumentException("X range maximum must be grater than minimum.");
                    }

                    gp.SetX2Range(this.X2rangeMin.Value, this.X2rangeMax.Value);
                }
                else
                {
                    gp.SetX2Autorange();
                }

                if (this.Y2rangeMin != null)
                {
                    if (this.Y2rangeMin.Value >= this.Y2rangeMax.Value)
                    {
                        throw new ArgumentException("Y2 range maximum must be grater than minimum.");
                    }

                    gp.SetY2Range(this.Y2rangeMin.Value, this.Y2rangeMax.Value);
                }
                else
                {
                    gp.SetY2Autorange();
                }
            }

            // ========================
            // labels, title, legend...
            // ========================
            {
                gp.SetXLabel(this.Xlabel);
                gp.SetYLabel(this.Ylabel);
                gp.SetX2Label(this.X2label);
                gp.SetY2Label(this.Y2label);

                gp.SetTitle(this.Title);


                if (this.ShowLegend)
                {
                    gp.Cmd("unset key");
                    string command = $"set key font \",{this.LegendFont}\"";

                    if ((this.LegendPosition != null) & (this.LegendAlignment != null))
                    {
                        System.Console.WriteLine("legend position and legend alignment is set. Choose only one of them! Ignoring alignment ...");
                    }

                    if (this.LegendPosition != null)
                    {
                        command += String.Format("at {1:0.####e-00},{2:0.####e-00} vertical maxrows {0} ", this.dataGroups.Length, this.LegendPosition[0], this.LegendPosition[1]);
                    }
                    else if (this.LegendAlignment != null)
                    {
                        Dictionary <string, string> alignments = new Dictionary <string, string>();
                        alignments.Add("r", "right");
                        alignments.Add("c", "center");
                        alignments.Add("l", "left");
                        alignments.Add("b", "bottom");
                        alignments.Add("t", "top");

                        switch (LegendAlignment[0])
                        {
                        case "o":
                            //alignment within plotboundary
                            command += "outside ";
                            break;

                        case "i":
                            //alignement within graphyboundary
                            command += "inside ";
                            break;

                        default:
                            throw new ArgumentException("this style is not specified: use [i/o]");
                        }

                        for (int i = 1; i <= 2; i++)
                        {
                            foreach (KeyValuePair <string, string> kvp in alignments)
                            {
                                if (LegendAlignment[i] == kvp.Key)
                                {
                                    command += kvp.Value + " ";
                                }
                            }
                        }
                    }
                    else
                    {
                        //throw new ArgumentNullException("no alignment or position chosen");
                    }
                    if (this.LegendHorizontal == true)
                    {
                        command += "horizontal ";
                    }

                    if (this.LegendSwap == true)
                    {
                        command += "Left reverse ";
                    }

                    System.Console.WriteLine(command);
                    gp.Cmd(command);
                }
                else
                {
                    gp.Cmd("set key off");
                }
            }

            // ====
            // tics
            // ====
            {
                if (this.ShowXtics)
                {
                    if (this.LogX)
                    {
                        gp.Cmd("set xtics format \"$" + this.LogBaseX + "^{%L}$\" ");
                        gp.Cmd("set xtics offset 0, 0-0.4 font \"sans, 18\" ");
                        gp.Cmd($"set xtics font \"sans, {this.LabelFont}\" ");
                    }
                    else
                    {
                        gp.Cmd("set xtics ");
                    }
                }
                else
                {
                    gp.Cmd("set xtics format \" \" "); // shows ticks/marks, but hides numbers
                }

                if (this.ShowX2tics)
                {
                    if (this.LogX2)
                    {
                        gp.Cmd("set x2tics format \"$" + this.LogBaseX + "^{%L}$\" ");
                    }
                    else
                    {
                        gp.Cmd("set x2tics ");
                    }
                }
                else
                {
                    gp.Cmd("set x2tics format \" \" "); // shows ticks/marks, but hides numbers
                }

                if (this.ShowYtics)
                {
                    if (this.LogY)
                    {
                        gp.Cmd("set ytics format \"$" + this.LogBaseY + "^{%L}$\" ");
                        gp.Cmd($"set ytics font \"sans, {this.LabelFont}\" ");
                    }
                    else
                    {
                        gp.Cmd("set ytics ");
                    }
                }
                else
                {
                    gp.Cmd("set ytics format \" \" "); // shows ticks/marks, but hides numbers
                }

                if (this.ShowY2tics)
                {
                    if (this.LogY2)
                    {
                        gp.Cmd("set y2tics format \"$" + this.LogBaseY + "^{%L}$\" ");
                    }
                    else
                    {
                        gp.Cmd("set y2tics ");
                    }
                }
                else
                {
                    //gp.Cmd("unset y2tics");
                    gp.Cmd("set y2tics format \" \" "); // shows ticks/marks, but hides numbers
                }
            }



            // =================
            // finally, plotting
            // =================
            {
                foreach (var xyData in this.dataGroups)
                {
                    gp.PlotXY(xyData.Abscissas, xyData.Values, xyData.Name, xyData.Format, useX2: xyData.UseX2, useY2: xyData.UseY2, deferred: true);
                }

                gp.WriteDeferredPlotCommands();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Writes plot commands to a gnuplot object.
        /// </summary>
        public void ToGnuplot(Gnuplot gp)
        {
            // =======
            // margins
            // =======
            {
                if (lmargin != null)
                {
                    gp.Cmd("set lmargin {0:0.####e-00}", this.lmargin.Value);
                }
                else
                {
                    gp.Cmd("unset lmargin ");
                }

                if (rmargin != null)
                {
                    gp.Cmd("set rmargin {0:0.####e-00}", this.rmargin.Value);
                }
                else
                {
                    gp.Cmd("unset rmargin ");
                }

                if (tmargin != null)
                {
                    gp.Cmd("set tmargin {0:0.####e-00}", this.tmargin.Value);
                }
                else
                {
                    gp.Cmd("unset tmargin ");
                }

                if (bmargin != null)
                {
                    gp.Cmd("set bmargin {0:0.####e-00}", this.bmargin.Value);
                }
                else
                {
                    gp.Cmd("unset bmargin ");
                }
            }

            // ============
            // log settings
            // ============
            {
                if (this.LogX)
                {
                    gp.Cmd("set logscale x");
                }
                else
                {
                    gp.Cmd("unset logscale x");
                }

                if (this.LogY)
                {
                    gp.Cmd("set logscale y");
                }
                else
                {
                    gp.Cmd("unset logscale y");
                }

                if (this.LogX2)
                {
                    gp.Cmd("set logscale x2");
                }
                else
                {
                    gp.Cmd("unset logscale x2");
                }

                if (this.LogY2)
                {
                    gp.Cmd("set logscale y2");
                }
                else
                {
                    gp.Cmd("unset logscale y2");
                }
            }

            // ==========
            // axis range
            // ==========

            {
                if ((this.XrangeMax != null) != (this.XrangeMin != null))
                {
                    throw new ArgumentException("X range minimum and maximum must be set either both or none.");
                }
                if ((this.YrangeMax != null) != (this.YrangeMin != null))
                {
                    throw new ArgumentException("Y range minimum and maximum must be set either both or none.");
                }

                if (this.XrangeMin != null)
                {
                    if (this.XrangeMin.Value >= this.XrangeMax.Value)
                    {
                        throw new ArgumentException("X range maximum must be grater than minimum.");
                    }

                    gp.SetXRange(this.XrangeMin.Value, this.XrangeMax.Value);
                }
                else
                {
                    gp.SetXAutorange();
                }

                if (this.YrangeMin != null)
                {
                    if (this.YrangeMin.Value >= this.YrangeMax.Value)
                    {
                        throw new ArgumentException("Y range maximum must be grater than minimum.");
                    }

                    gp.SetYRange(this.YrangeMin.Value, this.YrangeMax.Value);
                }
                else
                {
                    gp.SetYAutorange();
                }


                if ((this.X2rangeMax != null) != (this.X2rangeMin != null))
                {
                    throw new ArgumentException("X2 range minimum and maximum must be set either both or none.");
                }
                if ((this.Y2rangeMax != null) != (this.Y2rangeMin != null))
                {
                    throw new ArgumentException("Y2 range minimum and maximum must be set either both or none.");
                }

                if (this.X2rangeMin != null)
                {
                    if (this.X2rangeMin.Value >= this.X2rangeMax.Value)
                    {
                        throw new ArgumentException("X range maximum must be grater than minimum.");
                    }

                    gp.SetX2Range(this.X2rangeMin.Value, this.X2rangeMax.Value);
                }
                else
                {
                    gp.SetX2Autorange();
                }

                if (this.Y2rangeMin != null)
                {
                    if (this.Y2rangeMin.Value >= this.Y2rangeMax.Value)
                    {
                        throw new ArgumentException("Y2 range maximum must be grater than minimum.");
                    }

                    gp.SetY2Range(this.Y2rangeMin.Value, this.Y2rangeMax.Value);
                }
                else
                {
                    gp.SetY2Autorange();
                }
            }

            // ========================
            // labels, title, legend...
            // ========================
            {
                gp.SetXLabel(this.Xlabel);
                gp.SetYLabel(this.Ylabel);
                gp.SetX2Label(this.X2label);
                gp.SetY2Label(this.Y2label);

                gp.SetTitle(this.Title);


                if (this.ShowLegend)
                {
                    gp.Cmd("unset key");

                    if (this.LegendPosition != null)
                    {
                        gp.Cmd("set key at {1:0.####e-00},{2:0.####e-00} vertical maxrows {0} ", this.dataGroups.Length, this.LegendPosition[0], this.LegendPosition[1]);
                    }
                    else
                    {
                        gp.Cmd("set key outside right vertical maxrows {0} ", this.dataGroups.Length);
                    }
                }
                else
                {
                    gp.Cmd("set key off");
                }
            }

            // ====
            // tics
            // ====
            {
                if (this.ShowXtics)
                {
                    if (this.LogX)
                    {
                        gp.Cmd("set xtics format \"$10^{%T}$\" ");
                    }
                    else
                    {
                        gp.Cmd("set xtics ");
                    }
                }
                else
                {
                    gp.Cmd("set xtics format \" \" "); // shows ticks/marks, but hides numbers
                }

                if (this.ShowX2tics)
                {
                    if (this.LogX2)
                    {
                        gp.Cmd("set x2tics format \"$10^{%T}$\" ");
                    }
                    else
                    {
                        gp.Cmd("set x2tics ");
                    }
                }
                else
                {
                    gp.Cmd("set x2tics format \" \" "); // shows ticks/marks, but hides numbers
                }

                if (this.ShowYtics)
                {
                    if (this.LogY)
                    {
                        gp.Cmd("set ytics format \"$10^{%T}$\" ");
                    }
                    else
                    {
                        gp.Cmd("set ytics ");
                    }
                }
                else
                {
                    gp.Cmd("set ytics format \" \" "); // shows ticks/marks, but hides numbers
                }

                if (this.ShowY2tics)
                {
                    if (this.LogY2)
                    {
                        gp.Cmd("set y2tics format \"$10^{%T}$\" ");
                    }
                    else
                    {
                        gp.Cmd("set y2tics ");
                    }
                }
                else
                {
                    //gp.Cmd("unset y2tics");
                    gp.Cmd("set y2tics format \" \" "); // shows ticks/marks, but hides numbers
                }
            }



            // =================
            // finally, plotting
            // =================
            {
                foreach (var xyData in this.dataGroups)
                {
                    gp.PlotXY(xyData.Abscissas, xyData.Values, xyData.Name, xyData.Format, useX2: xyData.UseX2, useY2: xyData.UseY2, deferred: true);
                }

                gp.WriteDeferredPlotCommands();
            }
        }