-
Hey guys, I'm trying to use ScottPlot to build a dataset editor. This is my first ScottPlot program so have mercy on my code example. i'm adding the first derivative of the Penalized Spline curve (orange curve) to the scatter plot so that I can see how it looks. private void DrawChart()
{
try
{
if (X.Length <= 0) { return; }
var n = Y.Length;
SmoothingSplineAlgLib.Fit(X,
Y,
rho,
n,
out double[] Xmodel,
out double[] Ymodel,
out double[] firstDeriv,
out double[] secondDeriv);
myPlot.Plot.Clear(); // This is supposed to clear the entire plot, it doesn't!
myPlot.Plot.Render();
var scatter1 = myPlot.Plot.Add.Scatter(X, Y);
scatter1.LineStyle = LineStyle.None;
scatter1.Color = Colors.Black;
scatter1 = myPlot.Plot.Add.Scatter(Xmodel, Ymodel);
scatter1.LineStyle.Width = 4;
scatter1.Color = Colors.Orange;
scatter1.Axes.XAxis = myPlot.Plot.XAxis;
scatter1.Axes.YAxis = myPlot.Plot.YAxis;
myPlot.Plot.XAxis.TickFont.Size = 20;
myPlot.Plot.YAxis.TickFont.Size = 20;
myPlot.Plot.YAxis.Label.Text = "Price";
// create a second axis and add it to the plot
ScottPlot.AxisPanels.LeftAxis yAxis2 = new();
myPlot.Plot.YAxes.Add(yAxis2);
// add a new plottable and tell it to use the custom Y axis
var scatter2 = myPlot.Plot.Add.Scatter(Xmodel, firstDeriv, Colors.MidnightBlue);
scatter2.Axes.XAxis = myPlot.Plot.XAxis; // standard X axis
scatter2.Axes.YAxis = yAxis2; // custom Y axis
scatter2.Axes.XAxis.TickFont.Size = 20;
scatter2.Axes.YAxis.TickFont.Size = 20;
yAxis2.Label.Text = "Velocity";
crosshair = myPlot.Plot.Add.Crosshair(50, 50);
scatter1.Label = "Price";
scatter2.Label = "Velocity";
myPlot.Plot.Legend();
myPlot.Plot.AutoScale(); // reset limits to fit the data
myPlot.Refresh();
dataPointsTextBox.SetText($"{Xmodel.Length}");
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
} In looking at the source code for ScottPlot 5.0.9-beta, I see that "myPlot.Plot.Clear();" is doing: "Clear() => PlottableList.Clear();" Charles Brauer System Details
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi @CBrauer, thanks for this question!
Actually those various "Add" methods create plottables then add them to the Here's a smaller code example that demonstrates how I use ScottPlot 5 to display data on multiple axes and use the namespace Sandbox.WinForms;
public partial class Form1 : Form
{
ScottPlot.IYAxis YAxis1;
ScottPlot.IYAxis YAxis2;
ScottPlot.RandomDataGenerator Gen = new();
public Form1()
{
InitializeComponent();
// add a second Y axis to the plot
ScottPlot.AxisPanels.LeftAxis yAxis2 = new();
formsPlot1.Plot.Axes.Add(yAxis2);
// store primary and secondary axes locally so we can reference them any time
YAxis1 = formsPlot1.Plot.YAxes[0];
YAxis2 = formsPlot1.Plot.YAxes[1];
button1.Click += (s, e) => PlotRandomData();
formsPlot1.Refresh();
}
void PlotRandomData()
{
formsPlot1.Plot.Clear();
double[] data1 = Gen.RandomWalk(count: 51, mult: 1);
var sig1 = formsPlot1.Plot.Add.Signal(data1);
sig1.Axes.YAxis = YAxis1;
double[] data2 = Gen.RandomWalk(count: 51, mult: 1000);
var sig2 = formsPlot1.Plot.Add.Signal(data2);
sig2.Axes.YAxis = YAxis2;
formsPlot1.Plot.XAxis.Min = 0;
formsPlot1.Plot.XAxis.Max = 50;
YAxis1.Min = -10;
YAxis1.Max = 10;
YAxis2.Min = -10_000;
YAxis2.Max = 10_000;
formsPlot1.Refresh();
}
} Let me know if this code example is not sufficient to resolve your issue. If this is the case, please paste the whole content of your .cs file or a smaller version that lets us run it ourselves so we can reproduce your issue and help you arrive at a fix 👍 |
Beta Was this translation helpful? Give feedback.
-
Hi Scott: |
Beta Was this translation helpful? Give feedback.
-
Hi @CBrauer, I think I understand what's going wrong. First you should understand that the drawing of the graphic is only done after calling In your code, each time |
Beta Was this translation helpful? Give feedback.
Hi @CBrauer, I think I understand what's going wrong.
First you should understand that the drawing of the graphic is only done after calling
Refresh()
, there is no need to call it after clearing the plottables list.plottables
means all graphical items in you graph: scatter plots, annotations, crosshair, etc. But not the axis.If you add a new axe to the graph, it will be there until you remove it!
In your code, each time
DrawChart()
is called, a new axe is added to the graph, but it is never removed.You could change your code to remember the added axis and only add it once, or remove all added axes before adding the new one.