public void Test_AutoAxis_ShrinkWhenNeeded() { Random rand = new Random(0); var plt = new ScottPlot.Plot(); // small area plt.PlotLine(-5, -5, 5, 5); plt.AxisAuto(); var limitsA = new ScottPlot.Config.AxisLimits2D(plt.Axis()); Console.WriteLine($"limits A: {limitsA}"); // expand to large area plt.Axis(-123, 123, -123, 123); var limitsB = new ScottPlot.Config.AxisLimits2D(plt.Axis()); Console.WriteLine($"limits B: {limitsB}"); // shrink back to small area plt.AxisAuto(); var limitsC = new ScottPlot.Config.AxisLimits2D(plt.Axis()); Console.WriteLine($"limits C: {limitsC}"); Assert.That(limitsB.xSpan > limitsC.xSpan); Assert.That(limitsB.ySpan > limitsC.ySpan); Assert.That(limitsA.xSpan == limitsC.xSpan); Assert.That(limitsA.ySpan == limitsC.ySpan); }
public void Test_AxisSpan_AutoAxisRespected(bool ignore) { // plot with small data in the center var plt = new ScottPlot.Plot(); plt.AddPoint(-10, -10); plt.AddPoint(10, 10); plt.AxisAuto(); var limits1 = plt.GetAxisLimits(); // large data var span1 = plt.AddVerticalSpan(-999, 999); var span2 = plt.AddHorizontalSpan(-999, 999); span1.IgnoreAxisAuto = ignore; span2.IgnoreAxisAuto = ignore; plt.AxisAuto(); var limits2 = plt.GetAxisLimits(); if (ignore) { Assert.AreEqual(limits1, limits2); } else { Assert.AreNotEqual(limits1, limits2); } }
public void Test_AxisAuto_AdjustsAllAxes() { var plt = new ScottPlot.Plot(400, 300); var sig1 = plt.AddSignal(ScottPlot.DataGen.Sin(51)); sig1.XAxisIndex = 0; sig1.YAxisIndex = 0; var sig2 = plt.AddSignal(ScottPlot.DataGen.Cos(51)); sig2.XAxisIndex = 1; sig2.YAxisIndex = 1; // on startup all axes are reset with AxisAuto() plt.Render(); plt.AxisAuto(); var originalLimitsPrimary = plt.GetAxisLimits(0); var originalLimitsSecondary = plt.GetAxisLimits(1); // zoom out on all axes plt.AxisZoom(.1, .1, xAxisIndex: 0, yAxisIndex: 0); plt.AxisZoom(.1, .1, xAxisIndex: 1, yAxisIndex: 1); var zoomedOutLimitsPrimary = plt.GetAxisLimits(0); Assert.Greater(zoomedOutLimitsPrimary.XSpan, originalLimitsPrimary.XSpan); Assert.Greater(zoomedOutLimitsPrimary.YSpan, originalLimitsPrimary.YSpan); var zoomedOutLimitsSecondary = plt.GetAxisLimits(1); Assert.Greater(zoomedOutLimitsSecondary.XSpan, originalLimitsSecondary.XSpan); Assert.Greater(zoomedOutLimitsSecondary.YSpan, originalLimitsSecondary.YSpan); // call AxisAuto() which is now expected to act on all axes plt.AxisAuto(); var resetLimitsPrimary = plt.GetAxisLimits(0); var resetLimitsSecondary = plt.GetAxisLimits(1); Assert.AreEqual(resetLimitsPrimary.XSpan, originalLimitsPrimary.XSpan); Assert.AreEqual(resetLimitsPrimary.YSpan, originalLimitsPrimary.YSpan); Assert.AreEqual(resetLimitsSecondary.XSpan, originalLimitsSecondary.XSpan); Assert.AreEqual(resetLimitsSecondary.YSpan, originalLimitsSecondary.YSpan); TestTools.SaveFig(plt); //var limits = plt.GetAxisLimits(); //Console.WriteLine(limits); }
public void Atan2_Plottting() { var xValues = new List <double>(); var correctValue = new List <double>(); var approxValue1 = new List <double>(); var approxValueError1 = new List <double>(); for (var i = 0; i < 2000; i++) { var tan = (2 * i / 2000f) - 1; xValues.Add(tan); } foreach (var tan in xValues) { var correctTan = Math.Atan2(tan, 1); correctValue.Add(correctTan); var aprox1Tan = fixmath.Atan2(fp.ParseUnsafe((float)tan), fp._1).AsDouble; approxValue1.Add(aprox1Tan); approxValueError1.Add(correctTan - aprox1Tan); } var plt = new ScottPlot.Plot(2048, 2048); plt.PlotScatter(xValues.ToArray(), correctValue.ToArray(), Color.CadetBlue, 0.01f, 1, "Math.Atan2"); plt.PlotScatter(xValues.ToArray(), approxValue1.ToArray(), Color.Firebrick, 0.01f, 1f, "fixmath.Atan2"); plt.AxisAuto(); plt.SaveFig("Atan2.png"); plt = new ScottPlot.Plot(2048, 2048); plt.PlotScatter(xValues.ToArray(), approxValueError1.ToArray(), Color.Firebrick, 0.01f, 1f, "fixmath.Atan2"); plt.AxisAuto(); plt.SaveFig("Atan2_error.png"); }
public void Pow_Plottting() { var xValues = new List <double>(); var correctValue = new List <double>(); var approxValue2 = new List <double>(); var approxValueError2 = new List <double>(); for (var i = 1; i < 2000; i++) { var val = 24 * i / 2000f; xValues.Add(val); } foreach (var val in xValues) { var correctVal = Math.Pow(2, val); correctValue.Add(correctVal); var aprox2Val = fixmath.Pow2(fp.ParseUnsafe((float)val)).AsDouble; approxValue2.Add(aprox2Val); approxValueError2.Add(correctVal - aprox2Val); } var plt = new ScottPlot.Plot(2048, 2048); plt.PlotScatter(xValues.ToArray(), correctValue.ToArray(), Color.CadetBlue, 0.01f, 1, "Math.Pow2"); plt.PlotScatter(xValues.ToArray(), approxValue2.ToArray(), Color.FromArgb(82, 178, 67), 0.01f, 1f, "fixmath.Pow2_2"); plt.AxisAuto(); plt.SaveFig("Pow2.png"); plt = new ScottPlot.Plot(2048, 2048); plt.PlotScatter(xValues.ToArray(), approxValueError2.ToArray(), Color.FromArgb(82, 178, 67), 0.01f, 1f, "fixmath.Pow2_2"); plt.AxisAuto(); plt.SaveFig("Pow2_error.png"); }
public static void Benchmark_Signal_1M_Points(int redrawCount = 1000) { // create the plot int pointCount = 1_000_000; Random rand = new Random(0); double[] data = ScottPlot.DataGen.RandomWalk(rand, pointCount); string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; var pltTest = new ScottPlot.Plot(); pltTest.Title(methodName.Replace("_", " ")); pltTest.PlotSignal(data); pltTest.AxisAuto(); pltTest.SaveFig(methodName + ".png"); // time how long it takes to redraw it var stopwatch = System.Diagnostics.Stopwatch.StartNew(); double[] redrawTimes = new double[redrawCount]; for (int i = 0; i < redrawCount; i++) { Console.WriteLine($"{methodName}() rendering {i + 1} of {redrawCount} ..."); stopwatch.Restart(); pltTest.GetBitmap(true); double timeMS = stopwatch.ElapsedTicks * 1000.0 / System.Diagnostics.Stopwatch.Frequency; redrawTimes[i] = timeMS; } }
public void Test_Fill_SeveralCurvesWithLegend() { double[] blueLineXs = { 0, 3, 5.5, 8, 10, 14, 15 }; double[] blueLineYs = { 10, 13, 10, 13, 11, 15, 15 }; double[] greenLineXs = { 0, 5, 6.5, 8, 11, 15 }; double[] greenLineYs = { 6, 2, 3.5, 2, 5, 1 }; double[] redLineXs = { 0, 5, 8, 15 }; double[] redLineYs = { 2, 7, 4, 11 }; var plt = new ScottPlot.Plot(400, 300); plt.PlotFill(blueLineXs, blueLineYs, fillColor: Color.Blue, fillAlpha: .5, label: "blue"); plt.PlotFill(redLineXs, redLineYs, fillColor: Color.Red, fillAlpha: .5, label: "red"); plt.PlotFill(greenLineXs, greenLineYs, fillColor: Color.Green, fillAlpha: .5, label: "green"); plt.Legend(shadowDirection: ScottPlot.Alignment.MiddleCenter); plt.AxisAuto(0, 0); TestTools.SaveFig(plt); }
static void Main(string[] args) { // generate some data to plot int pointCount = 50; double[] dataXs = new double[pointCount]; double[] dataSin = new double[pointCount]; double[] dataCos = new double[pointCount]; for (int i = 0; i < pointCount; i++) { dataXs[i] = i; dataSin[i] = Math.Sin(i * 2 * Math.PI / pointCount); dataCos[i] = Math.Cos(i * 2 * Math.PI / pointCount); } // plot the data var plt = new ScottPlot.Plot(500, 300); plt.PlotScatter(dataXs, dataSin); plt.PlotScatter(dataXs, dataCos); plt.Title("ScottPlot Quickstart (console)"); plt.XLabel("experiment time (ms)"); plt.YLabel("signal (mV)"); plt.AxisAuto(); plt.SaveFig("console.png"); Console.WriteLine("press ENTER to exit..."); Console.ReadLine(); }
public void Test_BubblePlot_Advanced() { double[] xs = ScottPlot.DataGen.Consecutive(31); double[] ys = ScottPlot.DataGen.Sin(31); var cmap = ScottPlot.Drawing.Colormap.Viridis; var plt = new ScottPlot.Plot(600, 400); var myBubblePlot = plt.AddBubblePlot(); for (int i = 0; i < xs.Length; i++) { double fraction = (double)i / xs.Length; myBubblePlot.Add( x: xs[i], y: ys[i], radius: 10 + i, fillColor: cmap.GetColor(fraction, alpha: .8), edgeColor: System.Drawing.Color.Black, edgeWidth: 2 ); } plt.Title("Advanced Bubble Plot"); plt.AxisAuto(.2, .25); TestTools.SaveFig(plt); }
public static void Stack(ScottPlot.Plot plt, ABFsharp.ABF abf, double yOffset, bool derivative) { plt.Clear(); if (derivative) { for (int i = 0; i < abf.info.sweepCount; i++) { var sweep = abf.GetSweep(i); plt.PlotSignal(Diff(sweep.valuesCopy), abf.info.sampleRate, color: System.Drawing.Color.Red, yOffset: i * yOffset); } plt.YLabel("Membrane Potential (mV)"); } else { for (int i = 0; i < abf.info.sweepCount; i++) { var sweep = abf.GetSweep(i); plt.PlotSignal(sweep.valuesCopy, abf.info.sampleRate, color: System.Drawing.Color.Blue, yOffset: i * yOffset); } } plt.Title($"Stacked Sweeps"); plt.XLabel("Sweep Time (seconds)"); plt.AxisAuto(0, .1); }
private void Test_Instantiation(int width, int height) { Console.Write($"Instantiating new ScottPlot [{width}, {height}] ... "); plt = new ScottPlot.Plot(width, height); plt.PlotScatter(xs, ys); plt.AxisAuto(); plt.GetBitmap(); Pass(); }
public void Test_Render_YIsAllNan() { double[] ys = { double.NaN, double.NaN, double.NaN, double.NaN, double.NaN }; var plt = new ScottPlot.Plot(); plt.PlotSignal(ys); Assert.Throws <InvalidOperationException>(() => { plt.AxisAuto(); }); }
public void Test_AutoAxis_ExpandYonly() { Random rand = new Random(0); var plt = new ScottPlot.Plot(); // small area plt.PlotLine(-5, -5, 5, 5); plt.AxisAuto(); var limitsA = new ScottPlot.Config.AxisLimits2D(plt.Axis()); // large area plt.PlotLine(-99, -99, 99, 99); plt.AxisAuto(yExpandOnly: true); var limitsB = new ScottPlot.Config.AxisLimits2D(plt.Axis()); Assert.That(limitsA.xSpan == limitsB.xSpan); Assert.That(limitsA.ySpan < limitsB.ySpan); }
static void Main(string[] args) { double[] xs = new double[] { 1, 2, 3, 4, 5 }; double[] ys = new double[] { 1, 4, 9, 16, 25 }; var plt = new ScottPlot.Plot(600, 400); plt.PlotScatter(xs, ys); plt.AxisAuto(); plt.SaveFig("demo.png"); }
static void SimpleFftWithGraphs(bool useWindow = false) { // load sample audio with noise and sine waves at 500, 1200, and 1500 Hz double[] audio = FftSharp.SampleData.SampleAudio1(); int sampleRate = 48_000; // optionally apply a window to the data before calculating the FFT if (useWindow) { double[] window = FftSharp.Window.Hanning(audio.Length); FftSharp.Window.ApplyInPlace(window, audio); } // You could get the FFT as a complex result Complex[] fft = FftSharp.Transform.FFT(audio); // For audio we typically want the FFT amplitude (in dB) double[] fftPower = FftSharp.Transform.FFTpower(audio); // Create an array of frequencies for each point of the FFT double[] freqs = FftSharp.Transform.FFTfreq(sampleRate, fftPower.Length); // create an array of audio sample times to aid plotting double[] times = ScottPlot.DataGen.Consecutive(audio.Length, 1000d / sampleRate); // plot the sample audio var plt1 = new ScottPlot.Plot(400, 300); plt1.PlotScatter(times, audio, markerSize: 3); //plt1.Title("Audio Signal"); plt1.YLabel("Amplitude"); plt1.XLabel("Time (ms)"); plt1.AxisAuto(0); // plot the FFT amplitude var plt2 = new ScottPlot.Plot(400, 300); plt2.PlotScatter(freqs, fftPower, markerSize: 3); //plt2.Title("Fast Fourier Transformation (FFT)"); plt2.YLabel("Power (dB)"); plt2.XLabel("Frequency (Hz)"); plt2.AxisAuto(0); // save output if (useWindow) { plt1.SaveFig("../../../output/audio-windowed.png"); plt2.SaveFig("../../../output/fft-windowed.png"); } else { plt1.SaveFig("../../../output/audio.png"); plt2.SaveFig("../../../output/fft.png"); } }
public void Test_Render_YHasNanPixel() { double[] ys = { 1, 4, double.NaN, 16, 25 }; var plt = new ScottPlot.Plot(); plt.PlotSignal(ys); Assert.DoesNotThrow(() => { plt.AxisAuto(); }); Assert.Throws <InvalidOperationException>(() => { plt.Render(); }); }
public void AutoAxis_EqualAxis_UnitsPerPixelEqual() { var plt = new ScottPlot.Plot(); plt.AddLine(0, 0, 5, 1); plt.AxisScaleLock(true); plt.AxisAuto(); var(xUnitsPerPixel, yUnitsPerPixel) = getUnitsPerPixel(plt); Assert.AreEqual(xUnitsPerPixel, yUnitsPerPixel, xUnitsPerPixel * 0.000001); }
public void Test_AxisAuto_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.AxisAuto(); }); }
public void Test_Scatter_Mutable() { var plt = new ScottPlot.Plot(); var scatter = new ScottPlot.Plottable.ScatterPlotList(); plt.Add(scatter); TestTools.SaveFig(plt, "no_points"); scatter.Add(1, 1); plt.AxisAuto(); TestTools.SaveFig(plt, "one_point"); scatter.Add(2, 2); plt.AxisAuto(); TestTools.SaveFig(plt, "two_points"); scatter.AddRange(new double[] { 3, 4, 5 }, new double[] { 1, 6, 3 }); plt.AxisAuto(); TestTools.SaveFig(plt, "many_points"); }
public void Figure_03_Plot_XY_Data() { string name = System.Reflection.MethodBase.GetCurrentMethod().Name.Replace("Figure_", ""); string fileName = System.IO.Path.GetFullPath($"{outputFolderName}/{name}.png"); var plt = new ScottPlot.Plot(width, height); plt.PlotScatter(dataRandom1, dataRandom2); plt.AxisAuto(); plt.SaveFig(fileName); Console.WriteLine($"Saved: {System.IO.Path.GetFileName(fileName)}"); }
public void Figure_32_Signal_Styling() { string name = System.Reflection.MethodBase.GetCurrentMethod().Name.Replace("Figure_", ""); string fileName = System.IO.Path.GetFullPath($"{outputFolderName}/{name}.png"); var plt = new ScottPlot.Plot(width, height); plt.PlotSignal(dataSignal, 20000, linewidth: 3, color: Color.Red); plt.AxisAuto(); plt.SaveFig(fileName); Console.WriteLine($"Saved: {System.IO.Path.GetFileName(fileName)}"); }
public static void plotValues(double[] values, string saveFilePath = "values.png", int sampleRateHz = 8000) { var plt = new ScottPlot.Plot(); plt.PlotSignal(values, sampleRateHz, markerSize: 0, lineWidth: 2); plt.Title("Signal"); plt.YLabel("Value"); plt.XLabel("Time (sec)"); plt.AxisAuto(0); plt.SaveFig(saveFilePath); Console.WriteLine($"Saved: {System.IO.Path.GetFullPath(saveFilePath)}"); }
public static void RightClickMenuItemClicked(ToolStripItem item, ContextMenuStrip rightClickMenu, ScottPlot.Plot plt) { Console.WriteLine("CLICKED:" + item.ToString()); SaveFileDialog savefile = new SaveFileDialog(); string itemName = item.ToString(); if (itemName.StartsWith("About")) { itemName = "About"; } switch (itemName) { case "Save Image": rightClickMenu.Hide(); savefile.FileName = "ScottPlot.png"; savefile.Filter = "PNG Files (*.png)|*.png|All files (*.*)|*.*"; if (savefile.ShowDialog() == DialogResult.OK) { plt.SaveFig(savefile.FileName); } break; case "Save Data": savefile.Title = "Save data for the first plot object"; savefile.FileName = "data.csv"; savefile.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*"; if (savefile.ShowDialog() == DialogResult.OK) { plt.GetPlottables()[0].SaveCSV(savefile.FileName); } break; case "Auto-Axis": rightClickMenu.Hide(); plt.AxisAuto(); break; case "Clear": rightClickMenu.Hide(); plt.Clear(); break; case "Toggle quality while dragging": plt.mouseTracker.lowQualityWhileDragging = !plt.mouseTracker.lowQualityWhileDragging; break; case "ScottPlot": rightClickMenu.Hide(); System.Diagnostics.Process.Start("https://github.com/swharden/ScottPlot"); break; } }
public static void plotFFT(double[] fft, string saveFilePath = "fft.png", int sampleRateHz = 8000) { var plt = new ScottPlot.Plot(); double fftSampleRate = (double)fft.Length / sampleRateHz * 2; plt.PlotSignal(fft, fftSampleRate, markerSize: 0, lineWidth: 2); plt.Title("FFT"); plt.YLabel("Power"); plt.XLabel("Frequency (Hz)"); plt.AxisAuto(0); plt.SaveFig(saveFilePath); Console.WriteLine($"Saved: {System.IO.Path.GetFullPath(saveFilePath)}"); }
public void Test_Fill_OneCurveWithBaseline() { double[] xs = ScottPlot.DataGen.Range(0, 10, .1, true); double[] ys = ScottPlot.DataGen.Sin(xs); var plt = new ScottPlot.Plot(400, 300); plt.PlotFill(xs, ys, fillAlpha: .5, baseline: .5); plt.PlotHLine(0.5, color: Color.Black, lineStyle: ScottPlot.LineStyle.Dash); plt.AxisAuto(0); TestTools.SaveFig(plt); }
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_Fill_OneCurveWithBaseline() { double[] xs = ScottPlot.DataGen.Range(0, 10, .1, true); double[] ys = ScottPlot.DataGen.Sin(xs); var plt = new ScottPlot.Plot(400, 300); plt.AddFill(xs, ys, baseline: .5); plt.AddHorizontalLine(0.5, color: Color.Black); plt.AxisAuto(0); TestTools.SaveFig(plt); }
public void Test_AutoAxis_ScatterHorizontalLine() { var plt = new ScottPlot.Plot(); plt.PlotScatter( xs: new double[] { 1, 2 }, ys: new double[] { 1, 1 } ); plt.AxisAuto(); Assert.IsTrue(plt.GetSettings().axes.x.span > 0); Assert.IsTrue(plt.GetSettings().axes.y.span > 0); }
public void Test_AutoAxis_WorksAfterClear() { var plt = new ScottPlot.Plot(); plt.PlotPoint(0.1, 0.1); plt.PlotPoint(-0.1, -0.1); plt.AxisAuto(); plt.GetBitmap(); // force a render Assert.Greater(plt.Axis()[0], -5); plt.PlotPoint(999, 999); plt.PlotPoint(-999, -999); plt.AxisAuto(); plt.GetBitmap(); // force a render Assert.Less(plt.Axis()[0], -800); plt.Clear(); plt.PlotPoint(0.1, 0.1); plt.PlotPoint(-0.1, -0.1); plt.GetBitmap(); // force a render Assert.Greater(plt.Axis()[0], -5); }