The spindle error and geometric error are the main sources of inaccuracy in CNC machining. With the rising of the machine tool parts' temperature, the spindle error and geometric error increase continually, and the error curves have a nonlinear distribution. To analyze the thermal effects on spindle error and geometric error, an experiment is carried out. To improve the machining accuracy of a CNC machine, an error model is proposed based on orthogonal polynomials. With the application of the orthogonal polynomials, the polynomial regression can be transformed into multiple linear regressions which are easier to calculate. In order to implement the real-time error compensation for the thermally induced spindle error and geometric error, an error compensation method is proposed based on the external coordinate offset. The thermally induced spindle and geometric error are compensated by 90 % compared with no compensation.