一、绘图参数
1. 图数量
单图
x = np. linspace( - 1 , 1 , 50 )
y = x ** 2
plt. plot( x, y)
plt. show( )
多线
x = np. linspace( - 1 , 1 , 50 )
y1 = x ** 2
y2 = 2 * x + 1
plt. plot( x, y2)
plt. plot( x, y1)
plt. show( )
多图
x = np. linspace( - 1 , 1 , 50 )
y1 = x ** 2
y2 = 2 * x + 1
plt. figure( )
plt. plot( x, y1)
plt. figure( num= 3 , figsize= ( 8 , 5 ) )
plt. plot( x, y2)
plt. show( )
子图
plt. figure( )
plt. subplot( 2 , 1 , 1 )
plt. plot( [ 0 , 1 ] )
plt. subplot( 2 , 3 , 4 )
plt. subplot( 2 , 3 , 5 )
plt. subplot( 2 , 3 , 6 )
plt. figure( )
ax1 = plt. subplot2grid( ( 3 , 3 ) , ( 0 , 0 ) , colspan= 3 , rowspan= 1 )
ax1. plot( [ 1 , 2 ] )
ax1. set_title( "title" )
ax2 = plt. subplot2grid( ( 3 , 3 ) , ( 1 , 0 ) , colspan= 2 )
ax2. plot( )
ax3 = plt. subplot2grid( ( 3 , 3 ) , ( 1 , 2 ) , rowspan= 2 )
ax3. plot( )
ax4 = plt. subplot2grid( ( 3 , 3 ) , ( 2 , 0 ) )
ax4. plot( )
ax5 = plt. subplot2grid( ( 3 , 3 ) , ( 2 , 1 ) )
ax5. plot( )
plt. show( )
图中图
plt. plot( [ 0 , 1 ] )
plt. axes( [ 0.6 , 0.2 , 0.2 , 0.2 ] )
plt. plot( [ 1 , 0 ] , 'g' )
plt. xlabel( 'x' )
plt. show( )
2. 线条
plt. plot( [ 1 , 2 ] , color= 'red' )
plt. plot( [ 1 , 2 ] , linewidth= 3 )
plt. plot( [ 1 , 2 ] , linestyle= '--' )
plt. plot( [ 1 , 2 ] , marker= '+' )
plt. plot( [ 1 , 2 ] , alpha= 0.6 )
3. 文本
标题
plt. plot( [ 0 , 1 ] )
plt. title( r"$This\ is\ Title$" )
plt. show( )
标签
plt. plot( [ 0 , 1 ] )
plt. xlabel( "X" )
plt. ylabel( "Y" )
plt. show( )
图例
p1, = plt. plot( [ 0 , 1 ] , label= 'up' )
p2, = plt. plot( [ 0 , 2 ] , color= 'red' , label= 'down' )
plt. legend( handles= [ p1, p2] , labels= [ 'a' , 'b' ] , loc= 'best' )
plt. show( )
标注
x = np. linspace( - 1 , 5 , 50 )
y = 2 * x + 1
plt. plot( x, y)
x0 = 1
y0 = 2 * x0 + 1
plt. plot( [ x0, x0] , [ y0, - 1 ] )
plt. text( - 1 , 4 , r"$This\ is\ some\ text$" )
plt. annotate( r"$2x+1=%s$" % y0,
xy= ( x0, y0) , xycoords= 'data' , xytext= ( + 30 , - 30 ) ,
textcoords= 'offset points' , fontsize= 16 ,
arrowprops= dict ( arrowstyle= '->' ,
connectionstyle= 'arc3, rad=0.3' ) )
plt. show( )
4. 坐标轴
网格
plt. plot( [ 0 , 1 ] )
plt. grid( True )
plt. axhline( 1 , color= 'r' )
plt. axvline( 1 , color= 'b' )
plt. show( )
坐标范围
plt. plot( [ 0 , 1 ] )
plt. xlim( ( - 1 , 1 ) )
plt. ylim( ( - 1 , 1 ) )
plt. show( )
坐标刻度
plt. plot( [ 0 , 1 ] )
plt. xticks( np. linspace( 0 , 1 , 5 ) )
plt. yticks( [ 0 , 0.5 , 1 ] , [ r"$bad$" , r"$normal$" , r"$good$" ] )
plt. show( )
数学刻度
y = np. random. normal( loc= 0.5 , scale= 0.4 , size= 1000 )
y = y[ ( y > 0 ) & ( y < 1 ) ]
y. sort( )
x = np. arange( len ( y) )
plt. plot( x, y)
plt. yscale( 'linear' )
plt. show( )
坐标背景色
plt. plot( [ 0 , 1 ] )
ax = plt. gca( )
for label in ax. get_xticklabels( ) + ax. get_yticklabels( ) :
label. set_fontsize( 12 )
label. set_bbox( dict ( facecolor= 'green' ,
edgecolor= 'red' ,
alpha= 0.7 ) )
plt. show( )
坐标轴隐藏
plt. plot( [ 0 , 1 ] )
ax = plt. gca( )
ax. spines[ 'right' ] . set_color( 'none' )
ax. spines[ 'top' ] . set_color( 'none' )
plt. show( )
坐标轴移动
plt. plot( [ 0 , 1 ] )
ax = plt. gca( )
ax. spines[ 'right' ] . set_color( 'none' )
ax. spines[ 'top' ] . set_color( 'none' )
ax. xaxis. set_ticks_position( 'bottom' )
ax. yaxis. set_ticks_position( 'left' )
ax. spines[ 'bottom' ] . set_position( ( 'data' , 1 ) )
ax. spines[ 'left' ] . set_position( ( 'data' , 1 ) )
plt. show( )
多坐标轴
fig, ax1 = plt. subplots( )
ax2 = ax1. twinx( )
ax1. plot( [ 0 , 1 ] , 'g-' )
ax2. plot( [ 1 , 0 ] , 'b-' )
ax1. set_xlabel( "X" )
ax1. set_ylabel( "Y1" , color= 'g' )
ax2. set_ylabel( "Y2" , color= 'b' )
plt. show( )
二、各种图
1. 散点图
DataFrame
df = pd. DataFrame( np. random. rand( 50 , 4 ) , columns= [ 'a' , 'b' , 'c' , 'd' ] )
df. plot. scatter( x= 'a' , y= 'b' )
df. plot. scatter( x= 'a' , y= 'b' , c= 'c' , s= 50 )
df. plot. scatter( x= 'a' , y= 'b' , s= df[ 'c' ] * 200 )
ax = df. plot. scatter( x= 'a' , y= 'b' , color= 'DarkBlue' , label= 'Group 1' )
df. plot. scatter( x= 'c' , y= 'd' , color= 'DarkGreen' , label= 'Group 2' , ax= ax)
plt. show( )
Series
n = 1024
x = np. random. normal( 0 , 1 , n)
y = np. random. normal( 0 , 1 , n)
T = np. arctan2( y, x)
plt. scatter( x, y, s= 75 , c= T, alpha= 0.5 )
plt. xlim( ( - 1.5 , 1.5 ) )
plt. ylim( ( - 1.5 , 1.5 ) )
plt. show( )
2. 条形图
DataFrame
df = pd. DataFrame( np. random. rand( 10 , 4 ) , columns= [ 'a' , 'b' , 'c' , 'd' ] )
df. iloc[ 5 ] . plot. bar( )
df. plot. bar( )
df. plot. bar( stacked= True )
df. plot. barh( stacked= True )
plt. show( )
Series
n = 12
x = np. arange( n)
y1 = ( 1 - x / float ( n) * np. random. uniform( 0.5 , 1 , n) )
y2 = ( 1 - x / float ( n) * np. random. uniform( 0.5 , 1 , n) )
plt. bar( x, + y1, facecolor= '#9999ff' , edgecolor= 'white' )
plt. bar( x, - y1, facecolor= '#ff9999' , edgecolor= 'white' )
plt. xlim( ( - 0.5 , n) )
plt. ylim( ( - 1.5 , 1.5 ) )
plt. xticks( ( ) )
plt. yticks( ( ) )
Y1 = zip ( x, y1)
Y2 = zip ( x, y2)
for x, y in Y1:
plt. text( x, y + 0.05 , '%.2f' % y, ha= 'center' , va= 'bottom' )
for x, y in Y2:
plt. text( x, - y - 0.05 , '%.2f' % y, ha= 'center' , va= 'top' )
plt. show( )
3. 直方图
DataFrame
df = pd. DataFrame( { 'a' : np. random. randn( 1000 ) + 1 , 'b' : np. random. randn( 1000 ) ,
'c' : np. random. randn( 1000 ) - 1 } , columns= [ 'a' , 'b' , 'c' ] )
df. plot. hist( alpha= 0.5 )
df. plot. hist( stacked= True )
df. plot. hist( bins= 20 )
df[ 'a' ] . plot. hist( orientation= 'horizontal' )
df[ 'a' ] . plot. hist( cumulative= True )
df. diff( ) . hist( bins= 50 )
plt. show( )
4. 箱线图
DataFrame
df = pd. DataFrame( np. random. rand( 10 , 5 ) , columns= [ 'A' , 'B' , 'C' , 'D' , 'E' ] )
df. plot. box( )
color = { 'boxes' : 'DarkGreen' , 'whiskers' : 'DarkOrange' ,
'medians' : 'DarkBlue' , 'caps' : 'Gray' }
df. plot. box( color= color)
df. plot. box( sym= 'r+' )
df. plot. box( vert= False )
df. plot. box( positions= [ 1 , 4 , 5 , 6 , 8 ] )
plt. show( )
5. 面积图
DataFrame
df = pd. DataFrame( np. random. rand( 10 , 4 ) , columns= [ 'a' , 'b' , 'c' , 'd' ] )
df. plot. area( stacked= False )
plt. show( )
6. 饼状图
DataFrame
df = pd. DataFrame( 3 * np. random. rand( 4 , 2 ) ,
index= [ 'a' , 'b' , 'c' , 'd' ] , columns= [ 'x' , 'y' ] )
df. plot. pie( subplots= True , figsize= ( 8 , 4 ) )
plt. show( )
Series
series = pd. Series( 3 * np. random. rand( 4 ) ,
index= [ 'a' , 'b' , 'c' , 'd' ] , name= 'series' )
series. plot. pie( labels= [ 'AA' , 'BB' , 'CC' , 'DD' ] , colors= [ 'r' , 'g' , 'b' , 'c' ] ,
autopct= '%.2f' , fontsize= 20 , figsize= ( 6 , 6 ) )
plt. show( )
7. 等高线图
Series
def f ( x, y) :
return ( 1 - x / 2 + x ** 5 + y ** 3 ) * np. exp( - x ** 2 - y ** 2 )
n = 256
x = np. linspace( - 3 , 3 , n)
y = np. linspace( - 3 , 3 , n)
X, Y = np. meshgrid( x, y)
plt. contourf( X, Y, f( X, Y) , 8 , alpha= 0.75 , cmap= plt. cm. hot)
C = plt. contour( X, Y, f( X, Y) , 8 , colors= 'black' , linewidths= 0.5 )
plt. clabel( C, inline= True , fontsize= 10 )
plt. show( )