из matplotlib.pyplot импорт * из numpy импорт * из mpl_toolkits.axisartist импорт *x , y = [ 1. , 2. , 3. , 4. ], [ 6. , 5. , 7. , 10. ] A = vstack ([ x , ones ( len ( x ))]) . T b2 , b1 = linalg . lstsq ( A , y )[ 0 ] x2 = arange ( 0 , 5 , 0.1 ) y2 = лямбда xx : b1 + b2 * xxfig = figure ( figsize = ( 3 , 3 )) ax = Subplot ( fig , "111" , axisbg = 'none' ) fig . add_subplot ( ax ) ax . axis [ "right" ] . set_visible ( False ) ax . axis [ "top" ] . set_visible ( False ) ax . plot ( x , y , 'ro' , label = "Data" ) ax . plot ( x2 , y2 ( x2 ), label = "curve fit" , lw = 2 ) ymin , ymax = [], [] for i in range ( len ( x )): yy = y2 ( x [ i ]) if yy >= y [ i ]: ymin , ymax = append ( ymin , y [ i ]), append ( ymax , yy ) else : ymin , ymax = append ( ymax , yy ), append ( ymin , y [ i ] ) ax.vlines ( x , ymin , ymax , color = ' g ' , linestyles = 'solid' , lw = 1.5 ) ax.set_ylabel ( "y " , fontsize = 12 ) ax . set_xlabel ( "x" , размер шрифта = 12 ) ax.grid ( True ) ax.minorticks_on ( ) ax.set_xlim ( 0 , 5 ) ax.set_ylim ( 4 , 10.5 ) ax.legend ( frameon = True , handletextpad = 0 , loc = ' upper left ' , \ labelspacing = .05 ) setp ( ax.get_legend ( ) . get_texts ( ) , fontsize = 12 ) рис . savefig ( "Linear_least_squares_example2.svg" , bbox_inches = "tight" , \ pad_inches = .15 )