我有以下函数来绘制误差线。
public void AddErrorBarsTwoSigma(string curveName, List<double> xValues, List<double> yValues, List<double> yErrors, Color color)
{
if (xValues.Count != yValues.Count || yValues.Count != yErrors.Count)
{
throw new ArgumentException("All lists must be of equal length.");
}
ErrorBarItem errorBar = myPane_.AddErrorBar(curveName, xValues.ToArray(), yValues.ToArray(), yErrors.ToArray(), color);
errorBar.Bar.IsVisible = true;
errorBar.Bar.PenWidth = DefaultLineWidth;
errorBar.Bar.Color = color;
// Refresh the graph to show changes
zgc_.AxisChange();
zgc_.Invalidate();
}
灰色线是误差曲线。
我想修改这个函数,以便它向上绘制一个西格玛,向下绘制一个西格玛误差线。
我怎样才能做到这一点?
驱动程序如下所示:
using System;
using System.Collections.Generic;
using System.Drawing;
using WeightedLinearRegressionNamespace;
using ZedGraph;
public class WeightedLinearRegression
{
public static void Main(string[] args)
{
// Create a new ZedGraphControl
ZedGraphControl zgc = new ZedGraphControl();
zgc.Size = new Size(1200, 800);
// Sample data points and their corresponding errors
double[] xData = new double[] { 1, 2, 3, 4, 5 };
double[] yData = new double[] { 2, 4, 6, 8, 10 };
double[] yErrors = new double[] { 0.5, 0.8, 0.6, 0.9, 0.7 };
ZedGraphPlotter plotter = new ZedGraphPlotter(zgc);
plotter.AddErrorBarsTwoSigma("bars",
new List<double>(xData),
new List<double>(yData),
new List<double>(yErrors),
Color.Red);
plotter.AddCurve("errors", new List<double>(yErrors));
plotter.SavePlot("", "error_bar_with_2_sigma.png");
}
}
您需要计算误差线的上限和下限。您可以通过在 y 值中添加和减去 sigma 来实现此目的。
double[] upperBounds = new double[yValues.Count];
double[] lowerBounds = new double[yValues.Count];
for (int i=0;i<yValues.Count;++i){
upperBounds[i] = yValues[i]+yErrors[i]; //sigma up
lowerBounds[i] = yValues[i]-yErrors[i]; //sigma down
}
添加上下栏项目
ErrorBarItem upperErrorBar = myPane_.AddErrorBar(curveName + "_upper", xValues.ToArray(), yValues.ToArray(), upperBounds, color);
upperErrorBar.Bar.IsVisible = true;
upperErrorBar.Bar.PenWidth =
upperErrorBar.Bar.Color =
ErrorBarItem lowerErrorBar = myPane_.AddErrorBar(curveName + "_lower",xValues.ToArray(), yValues.ToArray(), lowerBounds, color);
lowerErrorBar.Bar.IsVisible = true;
lowerErrorBar.Bar.PenWidth =
lowerErrorBar.Bar.Color =
这应该为您的代码绘制错误栏 它基本上创建了两组误差线,一组用于上限,另一组用于下限。这些误差线直观地表明 yValues 周围的不确定性范围。