How to plot correct best fit line? I'm working on a software where I'm plotting graphs and finding best fit line.
I have used Least-Square Method and linear regression technique with y = mx + c
My problem is that when most of the X values of graph are equal (not all) at that time best fit line is not proper but when there is good variation it seems correct.
Following pictures are for reference, Orange line is graph and Green is best fit (please don't consider other grey lines)
1. points with variation
2. most X values are equal
What is the problem here I'm not getting? please help.

The problem has horrific conditioning.
What is the slope$-$intercept equation for a vertical line?
The value for the slope is swinging between $\pm \infty$. This mean a very small perturbation in your data can cause an enormous change in the results.
The trial function is 
$$
 y(x) = a_{0} + a_{1} x. 
$$
The data will consist of a sequence of measurements $\left\{ x_{k}, y_{k} \right\}_{k=1}^{m}.$
The linear system is
$$
\begin{align}
  \mathbf{A} a  &= y \\
%
\left[ \begin{array}{cc}
 \mathbf{1} & x
\end{array} \right]
%
\left[ \begin{array}{c}
 a_{0} \\ a_{1}
\end{array} \right]
%
&=
\left[ \begin{array}{c}
 y
\end{array} \right]
%
\end{align}
$$
The normal equations are
$$
\begin{align}
  \mathbf{A}^{*} \mathbf{A} a  &= \mathbf{A}^{*} y \\
%
\left[ \begin{array}{cc}
 \mathbf{1} \cdot \mathbf{1} & \mathbf{1} \cdot x \\
 x \cdot \mathbf{1} & x \cdot x
\end{array} \right]
%
\left[ \begin{array}{c}
 a_{0} \\ a_{1}
\end{array} \right]
%
&=
\left[ \begin{array}{c}
 \mathbf{1} \cdot y \\
 x \cdot y
\end{array} \right].
%
\end{align}
$$
The solution for the normal equations is
$$
\begin{align}
  a &= \left( \mathbf{A}^{*} \mathbf{A} \right)^{-1} \mathbf{A}^{*} y \\
%
&=
%
\left(
 \det \left( \mathbf{A}^{*} \mathbf{A} \right)
\right)^{-1}
%
\left[ \begin{array}{rr}
 x \cdot x & -\mathbf{1} \cdot x \\
 -\mathbf{1} \cdot x & \mathbf{1} \cdot \mathbf{1}
\end{array} \right]
%
\left[ \begin{array}{c}
 \mathbf{1} \cdot y \\
 x \cdot y
\end{array} \right].
%
\end{align}
$$
Isolate the slope
$$
  a_{1} = \frac{
\left( \mathbf{1} \cdot \mathbf{1} \right)
\left( x \cdot y \right) - 
\left( \mathbf{1} \cdot x  \right)
\left( \mathbf{1} \cdot y \right)} 
{
\left( \mathbf{1} \cdot \mathbf{1} \right)
\left( x \cdot x \right) - 
\left( \mathbf{1} \cdot x  \right)^{2}
}
$$
Consider the case where $\hat{x}_{k} = \bar{x} + \epsilon_{k}$, where $\epsilon_{k}<<\bar{x}$ for all $k$. The perturbations have mean $0$. The terms in the denominator have this behavior:
$$
\begin{align}
\left( \mathbf{1} \cdot \mathbf{1} \right)
\left( \hat{x} \cdot \hat{x} \right) - 
\left( \mathbf{1} \cdot \hat{x}  \right)^{2}
&\to
m\left( \sum x_{k}^{2} + \sum x_{k} \epsilon_{k} \right)
 - 
\sum x_{k}^{2} + 2 m \bar{x}^{2} \\
&\to
2 m \sum x_{k} \epsilon_{k} - 2 \sum x_{k} \sum \epsilon_{k} \\
&\to
0
\end{align}
$$
@Wouter offers a path to salvation by rotating the coordinate system.
