如何修改ZedGraph代码绘制±1σ上下误差棒?
问题描述
现有一个基于ZedGraph绘制误差棒的函数,当前用于绘制2σ误差棒(灰色线条为误差曲线),希望修改函数,使其绘制向上1σ和向下1σ的对称误差棒。
现有误差棒函数代码:
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"); } }
解决方案
ZedGraph的AddErrorBar方法默认支持以y值±误差值的方式绘制对称误差棒,只需调整误差值的传入逻辑,并优化函数语义即可实现1σ误差棒的绘制:
修改后的误差棒函数
public void AddErrorBarsOneSigma(string curveName, List<double> xValues, List<double> yValues, List<double> yErrorsOneSigma, Color color) { if (xValues.Count != yValues.Count || yValues.Count != yErrorsOneSigma.Count) { throw new ArgumentException("所有列表长度必须一致。"); } // 传入1σ误差值,ZedGraph会自动绘制y±1σ的上下对称误差棒 ErrorBarItem errorBar = myPane_.AddErrorBar(curveName, xValues.ToArray(), yValues.ToArray(), yErrorsOneSigma.ToArray(), color); // 配置误差棒基础样式 errorBar.Bar.IsVisible = true; errorBar.Bar.PenWidth = DefaultLineWidth; errorBar.Bar.Color = color; // 可选:显示误差棒顶部/底部的短帽,让样式更规范 errorBar.Cap.IsVisible = true; errorBar.Cap.PenWidth = DefaultLineWidth; errorBar.Cap.Color = color; // 刷新图表以显示修改 zgc_.AxisChange(); zgc_.Invalidate(); }
关键注意事项
- 误差值转换:如果原
yErrors参数存储的是2σ误差值,调用时需要将其转换为1σ:List<double> yErrorsOneSigma = yErrors.Select(err => err / 2).ToList(); - 函数名更新:将原函数名改为
AddErrorBarsOneSigma,让函数语义与功能匹配。 - 样式优化:开启
errorBar.Cap.IsVisible可以显示误差棒的短帽,提升图表可读性。
修改后的驱动代码示例
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using WeightedLinearRegressionNamespace; using ZedGraph; public class WeightedLinearRegression { public static void Main(string[] args) { ZedGraphControl zgc = new ZedGraphControl(); zgc.Size = new Size(1200, 800); double[] xData = new double[] { 1, 2, 3, 4, 5 }; double[] yData = new double[] { 2, 4, 6, 8, 10 }; // 原数据为2σ误差,转换为1σ double[] yErrorsTwoSigma = new double[] { 0.5, 0.8, 0.6, 0.9, 0.7 }; List<double> yErrorsOneSigma = yErrorsTwoSigma.Select(err => err / 2).ToList(); ZedGraphPlotter plotter = new ZedGraphPlotter(zgc); plotter.AddErrorBarsOneSigma("1σ误差棒", new List<double>(xData), new List<double>(yData), yErrorsOneSigma, Color.Red); plotter.AddCurve("误差曲线", yErrorsOneSigma); plotter.SavePlot("", "error_bar_with_1_sigma.png"); } }
内容的提问来源于stack exchange,提问作者user366312
相关产品推荐
相关产品推荐

