Python绘图基础
【代码框2-19】——matplotlib绘图的一些基本操作
python">
import numpy as np
import matplotlib. pyplot as plt
plt. rcParams[ 'font.sans-serif' ] = 'SimHei'
plt. rcParams[ 'axes.unicode_minus' ] = False
np. random. seed( 2025 )
x = np. random. standard_normal( 200 )
y = 1 + 2 * x + np. random. normal( 0 , 1 , 200 )
plt. figure( figsize= ( 8 , 6 ) )
plt. scatter( x, y, marker= 'o' , color= 'white' , edgecolors= 'blue' ) fit = np. poly1d( np. polyfit( x, y, deg= 1 ) )
y_hat = fit( x)
plt. plot( x, y_hat, c= 'r' )
plt. plot( x. mean( ) , y. mean( ) , 'ro' , markersize= 20 , fillstyle= 'bottom' )
plt. axhline( y. mean( ) , color= 'black' , ls= '-.' , lw= 1 )
plt. axvline( x. mean( ) , color= 'black' , ls= ( 0 , ( 5 , 10 ) ) , lw= 1 )
plt. grid( linestyle= ':' ) ax = plt. gca( )
ax. spines[ 'right' ] . set_color( 'green' )
ax. spines[ 'left' ] . set_color( '#4169E1' )
ax. spines[ 'top' ] . set_color( 'royalblue' )
ax. spines[ 'bottom' ] . set_color( 'b' ) plt. text( x= 0.4 , y= - 2 , s= r'$\hat{y}=\hat{\beta}_0+\hat{\beta}_1x$' , fontdict= { 'size' : 12 , 'bbox' : { 'fc' : 'pink' , 'boxstyle' : 'round' } } , ha= 'left' ) plt. annotate( text= r'均值点' , xy= ( x. mean( ) , y. mean( ) ) , xytext= ( - 0.6 , 3 ) , arrowprops = { 'headwidth' : 10 , 'headlength' : 12 , 'width' : 2 , 'facecolor' : 'r' , 'shrink' : 0.1 , } , fontsize= 14 , color= 'red' , ha= 'right' ) plt. title( '散点图及拟合直线\n并为图形增加新的元素' , fontsize= 14 )
plt. xlabel( 'x = 自变量' , fontsize= 12 )
plt. ylabel( 'y = 因变量' , fontsize= 12 )
plt. legend( [ '拟合直线' ] , loc= 'best' , fontsize= 10 ) plt. show( )
【代码框2-20】——图形布局
python">
import matplotlib. pyplot as plt
import numpy as npnp. random. seed( 1010 )
plt. subplots( nrows= 2 , ncols= 2 , figsize= ( 7 , 5 ) ) plt. subplot( 221 )
plt. scatter( x= range ( 50 ) , y= np. random. randint( low= 0 , high= 100 , size= 50 ) , marker= '*' , c= 'red' )
plt. title( 'subplot(221)' ) plt. subplot( 222 )
plt. plot( range ( 20 ) , np. random. normal( 5 , 10 , 20 ) , marker= 'o' , linestyle= '-.' , linewidth= 2 , markersize= 5 )
plt. title( 'subplot(222)' ) plt. subplot( 223 )
plt. bar( x= range ( 5 ) , height= range ( 5 , 0 , - 1 ) , color= [ 'cyan' , 'pink' ] )
plt. title( 'subplot(223)' ) plt. subplot( 224 )
plt. hist( np. random. normal( loc= 50 , scale= 10 , size= 500 ) , bins= 10 , color= 'lightgreen' )
plt. title( 'subplot(224)' ) plt. tight_layout( )
python">
import matplotlib. pyplot as pltfig = plt. figure( figsize= ( 6 , 5 ) )
grid= plt. GridSpec( 3 , 3 )
plt. subplot( grid[ 0 , : 2 ] )
plt. subplot( grid[ 0 , 2 ] )
plt. subplot( grid[ 1 , : 1 ] )
plt. subplot( grid[ 1 , 1 : ] )
plt. subplot( grid[ 2 , : 3 ] ) fig. tight_layout( )
python">
import matplotlib. pyplot as plt
import numpy as npnp. random. seed( 1010 )
fig = plt. figure( figsize= ( 7 , 4 ) )
spec = fig. add_gridspec( nrows= 2 , ncols= 6 , width_ratios= [ 1 , 1 , 1 , 1 , 1 , 1 ] , height_ratios= [ 1 , 2 ] )
fig. add_subplot( spec[ 0 , 1 : 3 ] )
fig. add_subplot( spec[ 0 , 3 : ] )
ax = fig. add_subplot( spec[ : , 0 ] )
ax = fig. add_subplot( spec[ 1 , 1 : 4 ] )
ax = fig. add_subplot( spec[ 1 , 4 : ] )
fig. tight_layout( )