多图布局
子视图
import numpy as np
import matplotlib. pyplot as pltx = np. linspace( 0 , 2 * np. pi) plt. figure( figsize= ( 9 , 6 ) )
ax = plt. subplot( 2 , 1 , 1 )
ax. plot( x, np. sin( x) )
ax = plt. subplot( 2 , 1 , 2 )
ax. plot( x, np. cos( x) )
fig, axes = plt. subplots( 2 , 2 )
axes[ 0 , 0 ] . plot( x, np. sin( x) , color = 'red' ) axes[ 0 , 1 ] . plot( x, np. sin( x) , color = 'green' ) axes[ 1 , 0 ] . plot( x, np. cos( x) , color = 'purple' ) axes[ 1 , 1 ] . plot( x, np. cos( x) )
x = np. linspace( - np. pi, np. pi, 20 )
y = np. sin( x)
plt. figure( figsize= ( 9 , 6 ) )
ax = plt. subplot( 221 )
ax. plot( x, y, color = 'red' )
ax. set_facecolor( 'green' )
ax = plt. subplot( 2 , 2 , 2 )
line, = ax. plot( x, - y)
line
line. set_marker( '*' )
line. set_markerfacecolor( 'red' )
line. set_markeredgecolor( 'green' )
line. set_markersize( 10 )
ax = plt. subplot( 2 , 1 , 2 )
plt. sca( ax) x = np. linspace( - np. pi, np. pi, 200 )
plt. plot( x, np. sin( x* x) , color = 'red' )
嵌套视图
x = np. linspace( - np. pi, np. pi, 25 )
y = np. sin( x)
fig = plt. figure( figsize= ( 9 , 6 ) )
plt. plot( x, y)
ax = plt. axes( [ 0.2 , 0.55 , 0.3 , 0.3 ] )
ax. plot( x, y, color = 'g' )
ax = fig. add_axes( [ 0.55 , 0.2 , 0.3 , 0.3 ] )
ax. plot( x, y, color = 'r' )
x = np. linspace( 0 , 2 * np. pi)
fig, ( ( ax11, ax12, ax13) , ( ax21, ax22, ax23) , ( ax31, ax32, ax33) ) = plt. subplots( 3 , 3 )
fig. set_figwidth( 9 )
fig. set_figheight( 6 ) ax11. plot( x, np. sin( x) )
ax12. plot( x, np. cos( x) )
ax13. plot( x, np. tanh( x) )
ax21. plot( x, np. tan( x) )
ax22. plot( x, np. cosh( x) )
ax23. plot( x, np. sinh( x) )
ax31. plot( x, np. sin( x) + np. cos( x) )
ax32. plot( x, np. sin( x* x) + np. cos( x* x) )
ax33. plot( x, np. sin( x) * np. cos( x) )
plt. tight_layout( )
plt. show( )
x = np. linspace( 0 , 2 * np. pi, 200 )
fig = plt. figure( figsize= ( 12 , 9 ) )
ax1 = plt. subplot( 3 , 1 , 1 )
ax1. plot( x, np. sin( 10 * x) )
ax1. set_title( 'ax1_title' )
ax2 = plt. subplot( 3 , 3 , ( 4 , 5 ) )
ax2. set_facecolor( 'green' )
ax2. plot( x, np. cos( x) , color = 'red' )
ax3 = plt. subplot( 3 , 3 , ( 6 , 9 ) )
ax3. plot( x, np. sin( x) + np. cos( x) )
ax4 = plt. subplot( 3 , 3 , 7 )
ax4. plot( [ 1 , 3 ] , [ 2 , 4 ] )
ax5 = plt. subplot( 3 , 3 , 8 )
ax5. scatter( [ 1 , 2 , 3 ] , [ 0 , 2 , 4 ] )
ax5. set_xlabel( 'ax5_x' , fontsize = 12 )
ax5. set_ylabel( 'ax5_y' , fontsize = 12 ) plt. show( )