绘制图表中的行给出的y轴截距和斜率 [英] Plotting a line in a chart given the y intercept and slope

查看:323
本文介绍了绘制图表中的行给出的y轴截距和斜率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的计算由用户给定的几个输入值最适合(截距/坡)线的计划。我已经标明每单值,但不确定代码的情节行给出的斜率和截距。

I've written a program that calculates the line of best fit (intercept/slope) given several input values from the user. I've plotted each of the individual values, however unsure of the code to plot the line given the slope and y-intercept.

这的是斜率:

double m = ( aXY.Sum() - 
           ((levels.Sum() * scores.Sum()) / 5)) / (newaX.Sum() - ((powLevels) / 5));



拦截

The Intercept

double b = meanY - (m * meanX);



点绘制

Plotting of points

for (int i = 0; i < levels.GetLength(0); i++)
{
    chart1.Series["Series1"].Points
                            .AddXY(levels.GetValue(i), scores.ToArray().GetValue(i));
}



任何想法?我决不是一个专家,得到这个地步了实验的公平位..

Any ideas? I am by no means an expert and getting this far took a fair bit of experimenting..

推荐答案

假设你的数据绘制成使用 ChartType.Points 添加一行最简单的方法是添加一个scattergraph有一个额外的 系列 ChartType.Line ,并设置两个点出现。

Assuming your data are plotted as a scattergraph using the ChartType.Points the simplest way to add a line is to add one extra Series with ChartType.Line and set two points there.

其他的方式来创建一个,一条线就像画它或创建一个 LineAnnotation ,但他们的更复杂!

There are other ways to create a line on a Chart, like drawing it or creating a LineAnnotation, but they are much more complicated!

随着<一个HREF =htt​​p://hotmath.com/hotmath_help/topics/line-of-best-fit.html相对=nofollow>这个例子的信这里是一个实现:

Following this example to the letter here is an implementation:

注意,对于最佳的行创建系列后适合的你正在寻找的东西只是在最后两行 ..

Note that after creating the series for the line of best fit the thing you were looking for are just the last two lines..:

private void button1_Click(object sender, EventArgs e)
{
    // create TWO series!
    chart1.Series.Clear();
    chart1.Series.Add("Data");
    chart1.Series.Add("Line of best fit");
    chart1.Series[0].ChartType = SeriesChartType.Point;
    chart1.Series[1].ChartType = SeriesChartType.Line;

    List<int> levels = new List<int>() { 8, 2, 11, 6, 5, 4, 12, 9, 6, 1};
    List<int> scores = new List<int>() { 3, 10, 3, 6, 8, 12, 1, 4, 9, 14};

    double minX = levels.ToList().Min();
    double maxX = levels.ToList().Max();
    double meanX = 1f * levels.Sum() / levels.Count;
    double meanY = 1f * scores.Sum() / scores.Count;

    double st = 0;
    double sb = 0;
    for (int i = 0; i < levels.Count; i++ )
    {
        st += (levels[i] - meanX) * (scores[i] - meanY);
        sb += (levels[i] - meanX) * (levels[i] - meanX);
    }
    double slope = st / sb;
    double y0 = meanY - slope * meanX;  // y-intercept or y-crossing

    for (int i = 0; i < levels.Count; i++)
    {
            chart1.Series[0].Points.AddXY(levels[i], scores[i]);
    }
    // this is the part that creates the line of best fit:
    chart1.Series[1].Points.AddXY(minX, y0 + minX * slope);
    chart1.Series[1].Points.AddXY(maxX, y0 + maxX * slope);
}

如果你愿意,你可以在Y-添加第一个行权点轴:

If you want to you can add the first line point right at the y-axis:

 chart1.Series[1].Points.AddXY(0, y0 );

在这种情况下,你可能要设置图表,以防止它在所示的最小x值包括 1 ,也许是这样的:

In this case you may want to set the minimum x-values shown in the chart to prevent it from including -1, maybe like this:

 chart1.ChartAreas[0].AxisX.Minimum = minX - 1;

这篇关于绘制图表中的行给出的y轴截距和斜率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆