Exemple #1
0
        public void Test_RenderingArtifacts_Demonstrate()
        {
            // Due to a bug in System.Drawing the drawing of perfectly straight lines is
            // prone to rendering artifacts (diagonal lines) when anti-aliasing is off.
            // https://github.com/swharden/ScottPlot/issues/327
            // https://github.com/swharden/ScottPlot/issues/401

            var plt = new ScottPlot.Plot(400, 300);

            plt.Grid(xSpacing: 2, ySpacing: 2, color: Color.Red);
            plt.Axis(-13, 13, -10, 10);

            // create conditions to reveal rendering artifact
            plt.AntiAlias(false, false, false);
            plt.Grid(enableVertical: false);

            // save the figure (bmpData + bmpFigure)
            TestTools.SaveFig(plt);

            // save the data bitmap too
            string gfxFilePath = System.IO.Path.GetFullPath("diag.png");

            plt.GetSettings(false).bmpData.Save(gfxFilePath, ImageFormat.Png);
            Console.WriteLine($"SAVED: {gfxFilePath}");
        }
Exemple #2
0
        public void Test_PlottablePopulations_Population()
        {
            // This example will display a single population: mean age of every country in europe in 2007
            // This example has 1 series that contains 1 population.

            // for this example we will simulate countries by creating random data
            Random rand = new Random(0);
            var    ages = new Population(rand, 44, 78, 2);

            var customPlottable = new PopulationPlot(ages);

            // plot the multi-series
            var plt = new ScottPlot.Plot(400, 300);

            plt.Add(customPlottable);
            plt.XAxis.Ticks(false);

            // additional plot styling
            plt.Title("Life Expectancy in European Countries in 2007");
            plt.YLabel("Age (years)");
            plt.Legend(location: Alignment.LowerRight);
            plt.Grid(lineStyle: LineStyle.Dot);
            plt.XAxis.Grid(false);

            TestTools.SaveFig(plt);
        }
Exemple #3
0
        public void Test_Bar_MultiSeries()
        {
            Random rand = new Random(0);

            string[] groupNames  = { "one", "two", "three", "four", "five" };
            string[] seriesNames = { "alpha", "beta", "gamma" };

            int groupCount = groupNames.Length;

            double[] xs   = DataGen.Consecutive(groupCount);
            double[] ys1  = DataGen.RandomNormal(rand, groupCount, 20, 5);
            double[] ys2  = DataGen.RandomNormal(rand, groupCount, 20, 5);
            double[] ys3  = DataGen.RandomNormal(rand, groupCount, 20, 5);
            double[] err1 = DataGen.RandomNormal(rand, groupCount, 5, 2);
            double[] err2 = DataGen.RandomNormal(rand, groupCount, 5, 2);
            double[] err3 = DataGen.RandomNormal(rand, groupCount, 5, 2);

            var plt = new ScottPlot.Plot(600, 400);

            plt.PlotBarGroups(
                groupLabels: groupNames,
                seriesLabels: seriesNames,
                ys: new double[][] { ys1, ys2, ys3 },
                yErr: new double[][] { err1, err2, err3 });

            plt.Axis(y1: 0);
            plt.Grid(enableVertical: false, lineStyle: LineStyle.Dot);
            plt.Legend(location: Alignment.UpperRight);
            TestTools.SaveFig(plt);
        }
        public static void PlotMonthlyChange(List <MonthlyClosedDifference> series, string propertyName, string folderName, string fileName)
        {
            // get and convert specific property of a list of objects to array of doubles
            PropertyInfo prop = typeof(MonthlyClosedDifference).GetProperty(propertyName);

            double[] valSeries = series.Select(x => Convert.ToDouble(prop.GetValue(x))).ToArray();

            // months to x values and labels
            double[] x      = DataGen.Consecutive(series.Count());
            string[] labels = series.Select(x => x.Month).ToArray();

            var plt = new ScottPlot.Plot(1000, 700);

            plt.Title(propertyName, fontName: "Segoe UI Light", color: Color.Black);
            plt.Ticks(dateTimeX: false, fontName: "Cascadia Mono");
            plt.Grid(xSpacing: 1);

            // apply custom axis tick labels
            plt.XTicks(x, labels);

            // create folder if not exists
            System.IO.Directory.CreateDirectory(folderName);

            plt.PlotBar(x, valSeries, fillColor: Color.Orange);

            plt.SaveFig($"./{folderName}/{fileName}.png");
        }
        public static void PlotOHLC(List <StockHistoryDay> stockHistory, string folderName, string fileName)
        {
            List <ScottPlot.OHLC> valSeriesList = new List <ScottPlot.OHLC>();

            foreach (StockHistoryDay stockHistoryDay in stockHistory)
            {
                ScottPlot.OHLC ohlc = new ScottPlot.OHLC(
                    stockHistoryDay.Open,
                    stockHistoryDay.High,
                    stockHistoryDay.Low,
                    stockHistoryDay.Close,
                    stockHistoryDay.Date
                    );

                valSeriesList.Add(ohlc);
            }

            ScottPlot.OHLC[] valSeries = valSeriesList.ToArray();

            var plt = new ScottPlot.Plot(1000, 700);

            plt.Title("MSFT", fontName: "Segoe UI Light", color: Color.Black);
            plt.Ticks(dateTimeX: true, fontName: "Cascadia Mono");

            // grids at every 7 days
            plt.Grid(xSpacing: 7, xSpacingDateTimeUnit: ScottPlot.Config.DateTimeUnit.Day);
            plt.SetCulture(shortDatePattern: "M\\/dd");

            // create folder if not exists
            System.IO.Directory.CreateDirectory(folderName);

            plt.PlotOHLC(valSeries);

            plt.SaveFig($"./{folderName}/{fileName}.png");
        }
Exemple #6
0
        public void Test_PlottablePopulations_Series()
        {
            // This example will display age, grouped by location.
            // This example has 1 series that contains 5 population objects.

            // for this example we will simulate countries by creating random data
            Random rand = new Random(0);

            var ages = new Population[]
            {
                new Population(rand, 54, 53, 5), // africa
                new Population(rand, 35, 72, 3), // americas
                new Population(rand, 48, 72, 4), // asia
                new Population(rand, 44, 78, 2), // europe
                new Population(rand, 14, 81, 1), // oceania
            };
            var customPlottable = new PopulationPlot(ages, color: System.Drawing.Color.CornflowerBlue);

            // plot the multi-series
            var plt = new ScottPlot.Plot();

            plt.Add(customPlottable);
            plt.XTicks(labels: new string[] { "Africas", "Americas", "Asia", "Europe", "Oceania" });
            plt.XAxis.TickLabelStyle(fontSize: 18);
            plt.YAxis.TickLabelStyle(fontSize: 18);

            // additional plot styling
            plt.XAxis2.Label(label: "Life Expectancy in 2007", size: 26);
            plt.YAxis.Label(label: "Age (years)", size: 18);
            plt.Legend(location: Alignment.LowerRight);
            plt.Grid(lineStyle: LineStyle.Dot);
            plt.XAxis.Grid(false);

            TestTools.SaveFig(plt);
        }
Exemple #7
0
        static void Main(string[] args)
        {
            BendStiffener bendStiffener = new BendStiffener
            {
                max_k = 0.3f,
                D1    = 0.7,
                d1    = 0.18,
                d2    = 0.2,
                L1    = 0.1,
                L2    = 2.3,
                L3    = 0.2,
                EIp   = 1e4,
                Es    = 4.5e6,
                F     = 2e4,
                q     = 0.52
            };

            int N = 10000;

            Vector <double>[] res = bendStiffener.bvpsolver_zzz(bendStiffener.Ode_to_solve, 0.0, bendStiffener.q, 0.0, 5.0, N);
            double[]          x   = Vector <double> .Build.Dense(N, i => i * 5.0 / N).ToArray();

            double[] y    = new double[N];
            double[] dydx = new double[N];

            for (int i = 0; i < N; i++)
            {
                double[] temp = res[i].ToArray();
                y[i]    = temp[0];
                dydx[i] = temp[1];
                //Console.WriteLine(t[i]);
            }
            var plt = new ScottPlot.Plot(800, 600);

            //plt.PlotScatter(x, y, label: "y", markerShape: (MarkerShape)Enum.Parse(typeof(MarkerShape), "none"));
            plt.PlotScatter(x, dydx, label: "dydx", markerShape: (MarkerShape)Enum.Parse(typeof(MarkerShape), "none"));
            plt.Grid(false);
            plt.Legend(fontSize: 10);
            //plt.PlotSignal(new[,] { x, y });
            plt.SaveFig("quickstart.png");


            Console.WriteLine("plot finished"); // gives 164,537981852489
            //Test
            //Console.WriteLine(y[N / 10]); // gives 164,537981852489
            //Console.WriteLine(Math.Exp(-1) + 3 * Math.Exp(4) - 5.0 / 2 + 23.0 / 8); //gives 164,537329540604, which is y(1)

            Console.ReadKey();
        }
Exemple #8
0
        /// <summary>
        /// Method for plotting data to .png
        /// </summary>
        /// <param name="dataPoints"></param>
        public static void Graph(double[,] dataPoints)
        {
            double[] DataError    = new double[dataPoints.GetUpperBound(0) + 1];
            double[] DataAccuracy = new double[dataPoints.GetUpperBound(0) + 1];
            double[] dataXs       = new double[dataPoints.GetUpperBound(0) + 1];

            for (int i = 0; i < dataXs.Length; i++)
            {
                dataXs[i]       = i;
                DataError[i]    = dataPoints[i, 0];
                DataAccuracy[i] = dataPoints[i, 1];
            }

            /// plot the data
            ///

            var plt_acc  = new ScottPlot.Plot(800, 600);
            var plt_loss = new ScottPlot.Plot(800, 600);

            plt_acc.PlotScatter(dataXs, DataAccuracy, label: "Accuracy");
            plt_loss.PlotScatter(dataXs, DataError, label: "Error rate");

            plt_acc.Grid(xSpacing: 20, ySpacing: .1);
            plt_loss.Grid(xSpacing: 5, ySpacing: .1);

            plt_acc.Legend(location: legendLocation.upperLeft);
            plt_loss.Legend(location: legendLocation.upperLeft);

            plt_acc.Title("Accuracy");
            plt_loss.Title("Loss");

            plt_acc.XLabel("Epoch");
            plt_acc.YLabel("Accuracy");

            plt_loss.XLabel("Epoch");
            plt_loss.YLabel("Loss");

            plt_acc.SaveFig("NN_acc.png");
            plt_loss.SaveFig("NN_loss.png");
            Process.Start(new ProcessStartInfo(@"NN_acc.png")
            {
                UseShellExecute = true
            });
            Process.Start(new ProcessStartInfo(@"NN_loss.png")
            {
                UseShellExecute = true
            });
        }
Exemple #9
0
        public void Test_PolygonVsScatter_Alignment()
        {
            double[] xs = { 75, 250, 280, 100 };
            double[] ys = { -100, -75, -200, -220 };

            var plt = new ScottPlot.Plot(320, 240);

            plt.PlotPolygon(xs, ys, fillColor: Color.LightGreen);
            plt.PlotLine(xs[0], ys[0], xs[1], ys[1], Color.Blue);
            plt.Grid(false);
            plt.Frame(false);
            plt.Ticks(false, false);
            plt.Title("Line/Scatter");

            TestTools.SaveFig(plt);
        }
Exemple #10
0
        public void Test_ScaleBar_Simple()
        {
            var plt = new ScottPlot.Plot(400, 300);

            plt.PlotSignal(DataGen.Sin(51));
            plt.PlotSignal(DataGen.Cos(51, mult: 1.5));

            plt.PlotScaleBar(5, .25, "5 ms", "250 pA");

            plt.Grid(false);
            plt.Frame(false);
            plt.Ticks(false, false);
            plt.AxisAuto(0);
            plt.LayoutFrameless();

            TestTools.SaveFig(plt);
        }
Exemple #11
0
        public void Test_Population_CurveSideways()
        {
            Random rand = new Random(0);
            var    ages = new ScottPlot.Statistics.Population(rand, 44, 78, 2);

            double[] curveXs = DataGen.Range(ages.minus3stDev, ages.plus3stDev, .1);
            double[] curveYs = ages.GetDistribution(curveXs);

            var plt = new ScottPlot.Plot(400, 300);

            plt.PlotScatter(DataGen.Random(rand, ages.values.Length), ages.values,
                            markerSize: 10, markerShape: MarkerShape.openCircle, lineWidth: 0);
            plt.PlotScatter(curveYs, curveXs, markerSize: 0);
            plt.Grid(lineStyle: LineStyle.Dot);

            TestTools.SaveFig(plt);
        }
Exemple #12
0
        public void GenerateMultipleBoxAndWhiskers(List <List <ResultModel> > allResults, List <int> xValues, string plotTitle, string plotName = "results")
        {
            var plt = new ScottPlot.Plot(600, 400);

            var poulations = new ScottPlot.Statistics.Population[allResults.Count];
            var p          = 0;

            foreach (var results in allResults)
            {
                var ys = new double[results.Count];
                var i  = 0;
                foreach (var r in results)
                {
                    ys[i] = r.Fitness;
                    i++;
                }
                var pop = new ScottPlot.Statistics.Population(ys);
                poulations[p] = pop;
                p++;
            }

            var popSeries = new ScottPlot.Statistics.PopulationSeries(poulations);

            plt.PlotPopulations(popSeries, "scores");

            // improve the style of the plot
            plt.Title(plotTitle);
            plt.YLabel("Solution energy");

            var ticks = new string[xValues.Count];
            var j     = 0;

            foreach (var iterations in xValues)
            {
                ticks[j] = iterations.ToString();
                j++;
            }
            plt.XTicks(ticks);
            plt.Legend();
            plt.Grid(lineStyle: LineStyle.Dot, enableVertical: false);

            plt.SaveFig($"{plotName}.png");
        }
Exemple #13
0
        private string MakeGraph(string[] labels, double[] values)
        {

            double[] xs = DataGen.Consecutive(labels.Count());


            var plt = new ScottPlot.Plot(1920, 1080);
            plt.PlotBar(xs, values);

            // customize the plot to make it look nicer
            plt.Grid(enableVertical: false, lineStyle: LineStyle.Dot);
            plt.XTicks(xs, labels);
            plt.Ticks(fontSize: 25, xTickRotation: 45);
            plt.Layout(yScaleWidth: 80, titleHeight: 50, xLabelHeight: 200, y2LabelWidth: 20);
            string path = "graph.png";
            plt.SaveFig(path);

            return path;
        }
Exemple #14
0
        public void Test_PolygonVsSignal_AlignmentWithLargeValues()
        {
            double[] xs = { 1e6 + 75, 1e6 + 250, 1e6 + 280, 1e6 + 100 };
            double[] ys = { 1e6 - 100, 1e6 - 75, 1e6 - 200, 1e6 - 220 };

            var plt = new ScottPlot.Plot(320, 240);

            plt.PlotPolygon(xs, ys, fillColor: Color.LightGreen);
            plt.PlotSignal(
                ys: new double[] { ys[0], ys[1] },
                sampleRate: 1.0 / (xs[1] - xs[0]),
                xOffset: xs[0],
                color: Color.Blue,
                markerSize: 0
                );
            plt.Grid(false);
            plt.Frame(false);
            plt.Ticks(false, false);
            plt.Title("Large Value Signal");

            TestTools.SaveFig(plt);
        }
Exemple #15
0
        public void Test_PolygonVsSignal_AlignmentWithLargeValues()
        {
            double[] xs = { 1e6 + 75, 1e6 + 250, 1e6 + 280, 1e6 + 100 };
            double[] ys = { 1e6 - 100, 1e6 - 75, 1e6 - 200, 1e6 - 220 };

            var plt = new ScottPlot.Plot(320, 240);

            plt.AddPolygon(xs, ys, fillColor: Color.LightGreen);
            var sig = plt.AddSignal(
                ys: new double[] { ys[0], ys[1] },
                sampleRate: 1.0 / (xs[1] - xs[0]),
                color: Color.Blue);

            sig.MarkerSize = 0;
            sig.OffsetX    = xs[0];
            plt.Grid(false);
            plt.Frameless();
            plt.XAxis.Ticks(false);
            plt.YAxis.Ticks(false);
            plt.Title("Large Value Signal");

            TestTools.SaveFig(plt);
        }
Exemple #16
0
        static void Main(string[] args)
        {
            int             N  = 10000;
            Vector <double> y0 = Vector <double> .Build.Dense(new[] { -7.0 / 4.0, 55.0 / 8.0 });

            //Func<double, Vector<double>, Vector<double>> der = DerivativeMaker();

            Vector <double>[] res = RungeKutta.FourthOrder(y0, 0, 10, N, DerivativeMaker_zzz2);

            double[] t = Vector <double> .Build.Dense(N, i => i * 10.0 / N).ToArray();

            double[] x = new double[N];
            double[] y = new double[N];
            for (int i = 0; i < N; i++)
            {
                double[] temp = res[i].ToArray();
                x[i] = temp[0];
                y[i] = temp[1];
                //Console.WriteLine(t[i]);
            }

            var plt = new ScottPlot.Plot(800, 600);

            plt.PlotScatter(t, x, label: "x", markerShape: (MarkerShape)Enum.Parse(typeof(MarkerShape), "none"));
            plt.PlotScatter(t, y, label: "y", markerShape: (MarkerShape)Enum.Parse(typeof(MarkerShape), "none"));
            plt.Grid(false);
            plt.Legend(fontSize: 10);
            //plt.PlotSignal(new[,] { x, y });
            plt.SaveFig("quickstart.png");


            //Test
            Console.WriteLine(y[N / 10]);                                           // gives 164,537981852489
            Console.WriteLine(Math.Exp(-1) + 3 * Math.Exp(4) - 5.0 / 2 + 23.0 / 8); //gives 164,537329540604, which is y(1)

            Console.ReadKey();
        }
Exemple #17
0
        public void Test_PlottablePopulations_MultiSeries()
        {
            // This example will display age, grouped by location, and by year.
            // This example has 3 series (years), each of which has 5 population objects (locations).

            // for this example we will simulate countries by creating random data
            Random rand = new Random(0);

            // start by collecting series data as Population[] arrays
            var ages1957 = new Population[]
            {
                new Population(rand, 54, 42, 4), // africa
                new Population(rand, 35, 56, 6), // americas
                new Population(rand, 48, 47, 5), // asia
                new Population(rand, 44, 66, 2), // europe
                new Population(rand, 14, 70, 1), // oceania
            };

            var ages1987 = new Population[]
            {
                new Population(rand, 54, 52, 8), // africa
                new Population(rand, 35, 70, 3), // americas
                new Population(rand, 48, 66, 3), // asia
                new Population(rand, 44, 75, 2), // europe
                new Population(rand, 14, 75, 1), // oceania
            };

            var ages2007 = new Population[]
            {
                new Population(rand, 54, 53, 5), // africa
                new Population(rand, 35, 72, 3), // americas
                new Population(rand, 48, 72, 4), // asia
                new Population(rand, 44, 78, 2), // europe
                new Population(rand, 14, 81, 1), // oceania
            };

            // now create a PopulationSeries object for each series
            string[] groupLabels = new string[] { "Africa", "Americas", "Asia", "Europe", "Oceania" };
            var      series1957  = new PopulationSeries(ages1957, "1957", color: System.Drawing.Color.Red);
            var      series1987  = new PopulationSeries(ages1987, "1987", color: System.Drawing.Color.Green);
            var      series2007  = new PopulationSeries(ages2007, "2007", color: System.Drawing.Color.Blue);

            // now collect all the series into a MultiSeries
            var multiSeries          = new PopulationSeries[] { series1957, series1987, series2007 };
            var plottableMultiSeries = new PopulationMultiSeries(multiSeries);
            var customPlottable      = new PopulationPlot(plottableMultiSeries);

            // plot the multi-series
            var plt = new ScottPlot.Plot();

            plt.Add(customPlottable);
            plt.XTicks(labels: groupLabels);
            plt.XAxis.TickLabelStyle(fontSize: 18);
            plt.YAxis.TickLabelStyle(fontSize: 18);

            // additional plot styling
            plt.XAxis2.Label(label: "Life Expectancy", size: 26);
            plt.YAxis.Label(label: "Age (years)", size: 18);
            plt.Legend(location: Alignment.LowerRight);
            plt.Grid(lineStyle: LineStyle.Dot);
            plt.XAxis.Grid(false);

            TestTools.SaveFig(plt);
        }