動的に生成される関数
関数を静的メソッドとして表現できない場合(たとえば、カスタムパラメーターが必要な関数)、Func<double, double> 型の変数として表現し、それに応じてプロットできます。
ScottPlot.Plot myPlot = new();
static double LogNormalDist(double x, double a, double b)
{
double expNum = Math.Log(x / a);
double exp = Math.Exp(-(expNum *expNum) / (2* b *b));
double y = Math.Sqrt(2* Math.PI) *b* x * exp;
return double.IsNaN(y) ? 0 : y;
}
double[] testValues = Generate.Range(0.8, 1.2, 0.05);
Color[] colors = new ScottPlot.Colormaps.MellowRainbow().GetColors(testValues.Length);
for (int i = 0; i < testValues.Length; i++)
{
double testValue = testValues[i];
var myFunc = new Func<double, double>((x) => LogNormalDist(x, testValue, 0.5));
var funcPlot = myPlot.Add.Function(myFunc);
funcPlot.LegendText = $"{testValue:0.00}";
funcPlot.LineWidth = 2;
funcPlot.LineColor = colors[i];
}
myPlot.ShowLegend();
myPlot.Legend.Orientation = Orientation.Horizontal;
myPlot.Axes.SetLimitsX(-0.5, 4);
myPlot.Axes.SetLimitsY(-0.8, 2);
myPlot.SavePng("demo.png", 400, 300);
このレシピは、Function カテゴリにある多数のレシピの 1 つです
