The quality of medicinal plants is closely related to the ecological factors of their growing environment, as their efficacy is reflected in the content of key medicinal components, which in turn indicates the quality of the plants. This study measured the daily variations in major constituents, including lobetyolin, polysaccharides, and total flavonoids, in Codonopsis pilosula (Franch.) Nannf., which in the Changzhi and Jincheng regions of Shanxi Province, China is known as Lu Tangshen. Throughout its growth cycle. Additionally, the study explored the effects of 11 ecological factors (both climatic and soil variables) on the primary medicinal components of C. pilosula. Through block experiments and comparisons between future data predictions and actual measurements, the reliability of the model and the consistency of block experimental data were ultimately confirmed. Principal component analysis (PCA), stepwise multiple linear regression analysis, and nonlinear polynomial modeling were employed to investigate the relationships between ecological factors and quality-related constituents (polysaccharides, total flavonoids, and lobetyolin). The results showed that linear models effectively explained daily temperature (DT) with an adjusted R2 exceeding 0.8, but due to the inherently nonlinear nature of the data, it is evident that linear models are fundamentally inadequate for accurately capturing the underlying relationships. Therefore, their fit for total flavonoids and lobetyolin was suboptimal. The introduction of nonlinear polynomial models (second-, fourth-, and fifth-order) significantly improved the model fit, indicating the existence of complex nonlinear relationships between ecological factors and medicinal components. For polysaccharides, the fourth-order model demonstrated the best performance, while fifth-order models were required to adequately describe the relationships for total flavonoids and lobetyolin. Based on the best models, the optimal ranges for key ecological factors were identified: polysaccharides were best influenced by atmospheric pressure (AP) between 9.1 and 9.3 kPa, air relative humidity (ARH) between 30% and 60%, 40 cm soil mean annual temperature (40cmMAT) between 27.5 °C and 28.5 °C, soil pH between 9.68 and 9.72, and soil nitrogen (N) content between 7 and 9 mg/kg. For total flavonoids, narrow optimal ranges were observed for temperature, humidity, and pH (MAT between 10 °C and 15 °C, 40cmMAT between 27.5 °C and 28.5 °C, and pH between 9.68 and 9.72). Lobetyolin showed optimal conditions at AP of 9.1 to 9.3 kPa, 40cmMAT of 28.0 °C to 28.5 °C, ARH of 65% to 75%, pH near 9.70, and days after planting (DAP) between 10 and 50. The adoption of higher-order polynomial models clarified critical nonlinear inflection points and optimal ecological ranges, providing a refined reference for enhancing the content of medicinal components. These findings offer valuable insights for precision cultivation strategies aimed at improving the quality of C. pilosula.