public void Test_NaturalSpline_ValuesMatch() { double[] xs = { 0, 10, 20, 30 }; double[] ys = { 65, 25, 55, 80 }; var nsi = new ScottPlot.Statistics.Interpolation.NaturalSpline(xs, ys, resolution: 20); var psi = new ScottPlot.Statistics.Interpolation.PeriodicSpline(xs, ys, resolution: 20); var esi = new ScottPlot.Statistics.Interpolation.EndSlopeSpline(xs, ys, resolution: 20); // verify output values have not deviated frrom what is expected Assert.AreEqual(-1752748231, ScottPlot.Tools.SimpleHash(nsi.interpolatedXs)); Assert.AreEqual(-1060440570, ScottPlot.Tools.SimpleHash(nsi.interpolatedYs)); Assert.AreEqual(-1752748231, ScottPlot.Tools.SimpleHash(psi.interpolatedXs)); Assert.AreEqual(-351909896, ScottPlot.Tools.SimpleHash(psi.interpolatedYs)); Assert.AreEqual(-1752748231, ScottPlot.Tools.SimpleHash(esi.interpolatedXs)); Assert.AreEqual(-2040768015, ScottPlot.Tools.SimpleHash(esi.interpolatedYs)); var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys, Color.Black, markerSize: 10, lineWidth: 0, label: "Original Data"); plt.AddScatter(nsi.interpolatedXs, nsi.interpolatedYs, Color.Red, markerSize: 3, label: "Natural Spline"); plt.AddScatter(psi.interpolatedXs, psi.interpolatedYs, Color.Green, markerSize: 3, label: "Periodic Spline"); plt.AddScatter(esi.interpolatedXs, esi.interpolatedYs, Color.Blue, markerSize: 3, label: "End Slope Spline"); plt.Legend(); TestTools.SaveFig(plt); }
public static void SaveScottPlot(double[] xIndexValues, double maxDose, double[] sourceDoses, string sourceAlias, double[] targetDoses, string targetAlias, string titleText, string filename, string location) { string longFileName = location + @"\" + filename; string longDirectory = longFileName.Substring(0, longFileName.LastIndexOf(@"\")); if (!(System.IO.Directory.Exists(longDirectory))) { System.IO.Directory.CreateDirectory(longDirectory); } var plt = new ScottPlot.Plot(1440, 900); plt.AddScatter(xIndexValues, sourceDoses, label: sourceAlias); plt.AddScatter(xIndexValues, targetDoses, label: targetAlias); double xmin = xIndexValues.Min(); double xmax = xIndexValues.Max(); plt.Title(filename + '\n' + titleText); plt.XLabel(@"Depth (mm)"); plt.YLabel(@"Dose (Gy)"); if (maxDose < 1.5 && maxDose > 0.1) { plt.SetAxisLimitsY(0, 1.2); } plt.SaveFig(longFileName + @".png"); }
public void Test_Marker_Precision() { var plt = new ScottPlot.Plot(); plt.AddScatter( new double[] { 1, 2, 3, 4 }, new double[] { -1, 1, -1, 1 }, markerSize: 10 ); plt.AddScatter( new double[] { 1, 2, 3, 4 }, new double[] { 1, -1, 1, -1 } ); double[] xs = ScottPlot.DataGen.Range(1, 4, .02, true); plt.AddScatter(xs, ScottPlot.DataGen.Sin(xs.Length, 2)); plt.YLabel("vertical units"); plt.XLabel("horizontal units"); plt.Title(ScottPlot.Tools.GetOsName()); plt.Legend(); TestTools.SaveFig(plt); }
public static ScottPlot.Plot SamplePlotScatter(int width = 600, int height = 400) { double[] dataXs = ScottPlot.DataGen.Consecutive(50); double[] dataSin = ScottPlot.DataGen.Sin(50); double[] dataCos = ScottPlot.DataGen.Cos(50); var plt = new ScottPlot.Plot(width, height); plt.AddScatter(dataXs, dataSin); plt.AddScatter(dataXs, dataCos); return(plt); }
public static void DesignerModeDemoPlot(ScottPlot.Plot plt) { int pointCount = 101; double pointSpacing = .01; double[] dataXs = ScottPlot.DataGen.Consecutive(pointCount, pointSpacing); double[] dataSin = ScottPlot.DataGen.Sin(pointCount); double[] dataCos = ScottPlot.DataGen.Cos(pointCount); plt.AddScatter(dataXs, dataSin); plt.AddScatter(dataXs, dataCos); plt.AxisAuto(0); plt.Title("ScottPlot User Control"); plt.YLabel("Sample Data"); }
public void Test_CrossHair_DateTime() { var plt = new ScottPlot.Plot(400, 300); plt.Title("Crosshair with DateTime Axis"); plt.XLabel("Horizontal Axis"); plt.YLabel("Vertical Axis"); // plot DateTime data on the horizontal axis int pointCount = 100; Random rand = new(0); double[] ys = ScottPlot.DataGen.RandomWalk(rand, pointCount); double[] xs = Enumerable.Range(0, pointCount) .Select(x => new DateTime(2016, 06, 27).AddDays(x)) .Select(x => x.ToOADate()).ToArray(); plt.AddScatter(xs, ys); plt.XAxis.DateTimeFormat(true); // add a crosshair var ch = plt.AddCrosshair(xs[50], ys[50]); // indicaite horizontal axis is DateTime and give a proper DateTime format string // https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings ch.IsDateTimeX = true; ch.StringFormatX = "d"; // use a numeric vertical axis but customize the format string // https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings ch.IsDateTimeY = false; ch.StringFormatY = "F4"; TestTools.SaveFig(plt); }
public void Test_Style_CreateThenModify() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(400, 300); // initialize with thin green line and diamond markers var scatter = plt.AddScatter(xs: xs, ys: ys, color: System.Drawing.Color.Green, lineWidth: 2, markerSize: 10, markerShape: ScottPlot.MarkerShape.filledDiamond); // modify for thick magenta line and circle markers scatter.LineWidth = 10; scatter.Color = System.Drawing.Color.Magenta; scatter.MarkerSize = 20; scatter.MarkerShape = ScottPlot.MarkerShape.filledCircle; // do the same for a scatter plot var signal = plt.AddSignal(ys: ys, color: System.Drawing.Color.Green); signal.OffsetY = 5; signal.LineWidth = 10; signal.Color = System.Drawing.Color.Orange; signal.MarkerSize = 20; string name = System.Reflection.MethodBase.GetCurrentMethod().Name; plt.SaveFig(System.IO.Path.GetFullPath(name + ".png")); }
/// <summary> /// Read the database to load star data and return a graph of the results /// </summary> /// <returns></returns> public System.Drawing.Bitmap GetGraphBitmap() { // populate table with data ReadAllRows().GetAwaiter().GetResult(); var sortedStarsByDate = StarsByDate.OrderBy(x => x.Value); // convert data to double arrays List <double> dates = new List <double>(); List <double> counts = new List <double>(); foreach ((var k, var v) in sortedStarsByDate) { dates.Add(k.ToOADate()); counts.Add(v); } // use ScottPlot to create the graph from the table data var plt = new ScottPlot.Plot(700, 400); plt.Title("ScottPlot Stars on GitHub"); var scatter = plt.AddScatter(dates.ToArray(), counts.ToArray(), markerSize: 0, lineWidth: 2); plt.XAxis.DateTimeFormat(true); plt.YLabel("Total Stars"); plt.YAxis.SetSizeLimit(min: 50); System.Drawing.Bitmap bmp = plt.Render(); Log($"Generated graph with {dates.Count()} points"); return(bmp); }
public void Scatter(double[] xs, double[] ys, double xStart, double xEnd, double expectedMin, double expectedMax) { var plt = new ScottPlot.Plot(); var scat = plt.AddScatter(xs, ys); (double yMin, double yMax) = scat.GetYDataRange(xStart, xEnd); Assert.AreEqual(expectedMin, yMin); Assert.AreEqual(expectedMax, yMax); }
static void Main(string[] args) { var xs = new double[] { 1, 2, 3, 4, 5 }; var ys = new double[] { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); plt.SaveFig("demo.png"); }
public void Test_Fill_Between() { // the sine wave will have ~100 points double[] xs1 = ScottPlot.DataGen.Range(1, 10, .1, true); double[] ys1 = ScottPlot.DataGen.Sin(xs1); // the sine wave will have ~20 points double[] xs2 = ScottPlot.DataGen.Range(1, 10, .5, true); double[] ys2 = ScottPlot.DataGen.Cos(xs2); var plt = new ScottPlot.Plot(400, 300); plt.AddFill(xs1, ys1, xs2, ys2); plt.AddScatter(xs1, ys1, Color.Black, 2); plt.AddScatter(xs2, ys2, Color.Black, 2); plt.AxisAuto(0); TestTools.SaveFig(plt); }
public void Test_Grid_HorizontalOnly() { int pointCount = 50; double[] dataXs = ScottPlot.DataGen.Consecutive(pointCount); double[] dataSin = ScottPlot.DataGen.Sin(pointCount); double[] dataCos = ScottPlot.DataGen.Cos(pointCount); var plt = new ScottPlot.Plot(); plt.AddScatter(dataXs, dataSin); plt.AddScatter(dataXs, dataCos); plt.XAxis.Grid(false); string name = System.Reflection.MethodBase.GetCurrentMethod().Name; string filePath = System.IO.Path.GetFullPath(name + ".png"); plt.SaveFig(filePath); Console.WriteLine($"Saved {filePath}"); }
public void Test_Validate_XYLengthMismatch() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, 9, 16 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); Assert.Throws <InvalidOperationException>(() => { plt.Validate(deep: false); }); }
public void Test_Render_XContainsNan() { double[] xs = { 1, 2, double.NaN, 4, 5 }; double[] ys = { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); Assert.Throws <InvalidOperationException>(() => { plt.Render(); }); }
public void Test_Validate_YContainsInf() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, double.PositiveInfinity, 16, 25 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); Assert.DoesNotThrow(() => { plt.Validate(deep: false); }); Assert.Throws <InvalidOperationException>(() => { plt.Validate(deep: true); }); }
public void Test_DocPlottables_ModifyValues() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(400, 300); var scatter = plt.AddScatter(xs, ys); TestTools.SaveFig(plt, "before"); ys[2] = 23; TestTools.SaveFig(plt, "after"); }
public void Test_Plot_Basic() { int pointCount = 51; double[] x = ScottPlot.DataGen.Consecutive(pointCount); double[] sin = ScottPlot.DataGen.Sin(pointCount); double[] cos = ScottPlot.DataGen.Cos(pointCount); var plt = new ScottPlot.Plot(600, 400); plt.AddScatter(x, sin, label: "sin"); plt.AddScatter(x, cos, label: "cos"); plt.YLabel("vertical units"); plt.XLabel("horizontal units"); plt.Title(ScottPlot.Tools.GetOsName()); plt.Legend(); TestTools.SaveFig(plt, artifact: true); }
public void Test_Render_AllValid() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); TestTools.SaveFig(plt); Assert.Pass(); }
public void AutoAxis_EqualAxisOnScatterDifferentResolutions_UnitsPerPixelEqual(int width, int height) { double[] xs = new double[] { 1, 5, 7, 19, 42 }; double[] ys = new double[] { 51, -5, 6, 12, 3 }; var plt = new ScottPlot.Plot(width, height); plt.AddScatter(xs, ys); plt.AxisScaleLock(true); plt.AxisAuto(); var(xUnitsPerPixel, yUnitsPerPixel) = getUnitsPerPixel(plt); Assert.AreEqual(xUnitsPerPixel, yUnitsPerPixel, xUnitsPerPixel * 0.000001); }
public void AutoAxis_EqualAxisOnScatter_UnitsPerPixelEqual() { double[] xs = new double[] { 1, 5, 7, 19, 42 }; double[] ys = new double[] { 51, -5, 6, 12, 3 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); plt.AxisScaleLock(true); plt.AxisAuto(); var(xUnitsPerPixel, yUnitsPerPixel) = getUnitsPerPixel(plt); Assert.AreEqual(xUnitsPerPixel, yUnitsPerPixel, xUnitsPerPixel * 0.000001); }
public void Test_CSV_Scatter(string cultureName) { var plt = new ScottPlot.Plot(); var sig = plt.AddScatter( xs: new double[] { 0, 10, 20, 30 }, ys: new double[] { 1.11, 2.22, 3.33, 4.44 }); string expected = "0, 1.11\n10, 2.22\n20, 3.33\n30, 4.44\n"; var culture = CultureInfo.GetCultureInfo(cultureName); System.Threading.Thread.CurrentThread.CurrentCulture = culture; System.Threading.Thread.CurrentThread.CurrentUICulture = culture; Assert.AreEqual(expected, sig.GetCSV()); }
public void Zoom_EqualAxisOnScatter_UnitsPerPixelEqual(int dx, int dy) { double[] xs = new double[] { 1, 5, 7, 19, 42 }; double[] ys = new double[] { 51, -5, 6, 12, 3 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); plt.AxisScaleLock(true); plt.AxisAuto(); plt.GetSettings(false).AxesZoomPx(dx, dy); var(xUnitsPerPixel, yUnitsPerPixel) = getUnitsPerPixel(plt); Assert.AreEqual(xUnitsPerPixel, yUnitsPerPixel, xUnitsPerPixel * 0.000001); }
public void Resize_EqualAxisOnScatter_UnitsPerPixelEqual(int width, int height) { double[] xs = new double[] { 1, 5, 7, 19, 42 }; double[] ys = new double[] { 51, -5, 6, 12, 3 }; var plt = new ScottPlot.Plot(); plt.AddScatter(xs, ys); plt.AxisScaleLock(true); plt.AxisAuto(); plt.Resize(width, height); var(xUnitsPerPixel, yUnitsPerPixel) = getUnitsPerPixel(plt); Assert.AreEqual(xUnitsPerPixel, yUnitsPerPixel, xUnitsPerPixel * ScaleLockPrecision); }
public void Test_DocPlottables_CreateWithHelperMethod() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(400, 300); plt.AddScatter(xs, ys, color: Color.Red, lineWidth: 2); TestTools.SaveFig(plt); string thisHash = ScottPlot.Tools.BitmapHash(plt.Render()); createHash ??= thisHash; Assert.AreEqual(createHash, thisHash); }
public void Test_Scatter_SinglePoint() { // https://github.com/ScottPlot/ScottPlot/issues/948 var plt = new ScottPlot.Plot(); plt.AddScatter( xs: new double[1] { 0 }, ys: new double[1] { 2 }, markerSize: 1, lineWidth: 1); plt.Render(); }
public void Test_AxisAutoX_Repeated() { double[] xs = { 1, 2, 3, 4, 5 }; double[] ys = { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(400, 300); plt.AddScatter(xs, ys); plt.SetAxisLimits(-5, 10, -15, 40); for (int i = 0; i < 10; i++) { plt.AxisAutoX(); } TestTools.SaveFig(plt); }
public void Test_GetDataLimits() { var plt = new ScottPlot.Plot(); Random rand = new(0); double[] xs = ScottPlot.DataGen.Random(rand, 100); double[] ys = ScottPlot.DataGen.Random(rand, 100); plt.AddScatter(xs, ys); plt.Render(); // default axis limits contain padding and should be larger than data var axisLimitsDefault = plt.GetAxisLimits(); Console.WriteLine(axisLimitsDefault); Assert.Less(axisLimitsDefault.XMin, xs.Min()); Assert.Greater(axisLimitsDefault.XMax, xs.Max()); Assert.Less(axisLimitsDefault.YMin, ys.Min()); Assert.Greater(axisLimitsDefault.YMax, ys.Max()); // axis limits after tight margins should make axis limts equal data limits plt.Margins(0, 0); var axisLimitsTight = plt.GetAxisLimits(); Console.WriteLine(axisLimitsTight); Assert.AreEqual(axisLimitsTight.XMin, xs.Min(), 1e-8); Assert.AreEqual(axisLimitsTight.XMax, xs.Max(), 1e-8); Assert.AreEqual(axisLimitsTight.YMin, ys.Min(), 1e-8); Assert.AreEqual(axisLimitsTight.YMax, ys.Max(), 1e-8); // data limits should be the same numbers without modifying the axes var dataLimits = plt.GetDataLimits(); Console.WriteLine(dataLimits); Assert.AreEqual(dataLimits.XMin, xs.Min()); Assert.AreEqual(dataLimits.XMax, xs.Max()); Assert.AreEqual(dataLimits.YMin, ys.Min()); Assert.AreEqual(dataLimits.YMax, ys.Max()); }
public void Test_Mel_Graph() { int specPoints = 4096; double maxFreq = 50_000; double maxMel = 2595 * Math.Log10(1 + maxFreq / 700); Random rand = new Random(1); double[] freq = ScottPlot.DataGen.Consecutive(specPoints, maxFreq / specPoints); double[] power = ScottPlot.DataGen.RandomWalk(rand, specPoints, .02, .5); var plt1 = new ScottPlot.Plot(800, 300); plt1.AddScatter(freq, power, markerSize: 0); int filterSize = 25; // generate scales double[] pointsLinear = new double[filterSize + 1]; double[] pointsMel = new double[filterSize + 1]; for (int i = 0; i < filterSize + 1; i++) { double thisFreq = maxFreq * i / filterSize; double thisMel = maxMel * i / filterSize; pointsLinear[i] = thisFreq; pointsMel[i] = 700 * (Math.Pow(10, thisMel / 2595d) - 1); } // draw rectangles double[] binStartFreqs = pointsMel; for (int binIndex = 0; binIndex < binStartFreqs.Length - 2; binIndex++) { double freqLow = binStartFreqs[binIndex]; double freqCenter = binStartFreqs[binIndex + 1]; double freqHigh = binStartFreqs[binIndex + 2]; var sctr = plt1.AddScatter( xs: new double[] { freqLow, freqCenter, freqHigh }, ys: new double[] { 0, 1, 0 }, markerSize: 0, lineWidth: 2); int indexLow = (int)(specPoints * freqLow / maxFreq); int indexHigh = (int)(specPoints * freqHigh / maxFreq); int indexSpan = indexHigh - indexLow; Console.WriteLine($"bin {binIndex}: [{freqLow} Hz - {freqHigh} Hz] = [{indexLow}:{indexHigh}]"); double binValue = 0; double binScaleSum = 0; for (int i = 0; i < indexSpan; i++) { double frac = (double)i / indexSpan; frac = (frac < .5) ? frac * 2 : 1 - frac; binScaleSum += frac; binValue += power[indexLow + i] * frac; } binValue /= binScaleSum; plt1.AddPoint(freqCenter, binValue, sctr.Color, 10); } plt1.SaveFig("mel1.png"); }