matplotlib命令與格式:標題(title),標注(annotate),文字說明(text)


 

1.title設置圖像標題

 

(1)title常用參數
fontsize設置字體大小,默認12,可選參數 ['xx-small', 'x-small', 'small', 'medium', 'large','x-large', 'xx-large']
fontweight設置字體粗細,可選參數 ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
fontstyle設置字體類型,可選參數[ 'normal' | 'italic' | 'oblique' ],italic斜體,oblique傾斜
verticalalignment設置水平對齊方式 ,可選參數 : 'center' , 'top' , 'bottom' ,'baseline' 
horizontalalignment設置垂直對齊方式,可選參數:left,right,center
rotation(旋轉角度)可選參數為:vertical,horizontal 也可以為數字
alpha透明度,參數值0至1之間
backgroundcolor標題背景顏色
bbox給標題增加外框 ,常用參數如下:
  • boxstyle方框外形
  • facecolor(簡寫fc)背景顏色
  • edgecolor(簡寫ec)邊框線條顏色
  • edgewidth邊框線條大小

 

(2)title例子:

 

plt.title('Interesting Graph',fontsize='large',fontweight='bold') 設置字體大小與格式
plt.title('Interesting Graph',color='blue') 設置字體顏色
plt.title('Interesting Graph',loc ='left') 設置字體位置
plt.title('Interesting Graph',verticalalignment='bottom') 設置垂直對齊方式
plt.title('Interesting Graph',rotation=45) 設置字體旋轉角度
plt.title('Interesting',bbox=dict(facecolor='g', edgecolor='blue', alpha=0.65 )) 標題邊框

 

面向對象api例子:

 

  1.  
    import matplotlib.pyplot as plt
  2.  
    x=[ 1,2,3,4,5]
  3.  
    y=[ 3,6,7,9,2]
  4.  
     
  5.  
    fig,ax=plt.subplots( 1,1)
  6.  
    ax.plot(x,y,label= 'trend')
  7.  
    ax.set_title( 'title test',fontsize=12,color='r')
  8.  
    plt.show()

2.annotate標注文字

 

(1)annotate語法說明 :annotate(s='str' ,xy=(x,y) ,xytext=(l1,l2) ,..)


s 為注釋文本內容 
xy 為被注釋的坐標點
xytext 為注釋文字的坐標位置
xycoords 參數如下:

  • figure points          points from the lower left of the figure 點在圖左下方
  • figure pixels          pixels from the lower left of the figure 圖左下角的像素
  • figure fraction       fraction of figure from lower left 左下角數字部分
  • axes points           points from lower left corner of axes 從左下角點的坐標
  • axes pixels           pixels from lower left corner of axes 從左下角的像素坐標
  • axes fraction        fraction of axes from lower left 左下角部分
  • data                     use the coordinate system of the object being annotated(default) 使用的坐標系統被注釋的對象(默認)
  • polar(theta,r)       if not native ‘data’ coordinates t

extcoords 設置注釋文字偏移量

 

         | 參數 | 坐標系 | 
         | 'figure points' | 距離圖形左下角的點數量 | 
         | 'figure pixels' | 距離圖形左下角的像素數量 | 
         | 'figure fraction' | 0,0 是圖形左下角,1,1 是右上角 | 
         | 'axes points' | 距離軸域左下角的點數量 | 
         | 'axes pixels' | 距離軸域左下角的像素數量 | 
         | 'axes fraction' | 0,0 是軸域左下角,1,1 是右上角 | 
         | 'data' | 使用軸域數據坐標系 |

 

arrowprops  #箭頭參數,參數類型為字典dict

 

  • width           the width of the arrow in points                              點箭頭的寬度
  • headwidth   the width of the base of the arrow head in points  在點的箭頭底座的寬度
  • headlength  the length of the arrow head in points                   點箭頭的長度
  • shrink          fraction of total length to ‘shrink’ from both ends  總長度為分數“縮水”從兩端
  • facecolor     箭頭顏色

 

bbox給標題增加外框 ,常用參數如下:

  •   boxstyle方框外形
  •   facecolor(簡寫fc)背景顏色
  •   edgecolor(簡寫ec)邊框線條顏色
  •   edgewidth邊框線條大小
 bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5)  #fc為facecolor,ec為edgecolor,lw為lineweight

 

 

(2)案例

 

  1.  
    import matplotlib.pyplot as plt
  2.  
    import numpy as np
  3.  
    x = np.arange( 0, 6)
  4.  
    y = x * x
  5.  
    plt.plot(x, y, marker= 'o')
  6.  
    for xy in zip(x, y):
  7.  
    plt.annotate( "(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points')
  8.  
    plt.show()

 

 

plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),arrowprops=dict(facecolor='black', shrink=0.05))


 

 

 

3.text設置文字說明

(1)text語法說明

text(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")

x,y:表示坐標值上的值
string:表示說明文字
fontsize:表示字體大小
verticalalignment:垂直對齊方式 ,參數:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
horizontalalignment:水平對齊方式 ,參數:[ ‘center’ | ‘right’ | ‘left’ ]
xycoords選擇指定的坐標軸系統:

 

  • figure points          points from the lower left of the figure 點在圖左下方
  • figure pixels          pixels from the lower left of the figure 圖左下角的像素
  • figure fraction       fraction of figure from lower left 左下角數字部分
  • axes points           points from lower left corner of axes 從左下角點的坐標
  • axes pixels           pixels from lower left corner of axes 從左下角的像素坐標
  • axes fraction        fraction of axes from lower left 左下角部分
  • data                     use the coordinate system of the object being annotated(default) 使用的坐標系統被注釋的對象(默認)
  • polar(theta,r)       if not native ‘data’ coordinates t

 

arrowprops  #箭頭參數,參數類型為字典dict

  • width           the width of the arrow in points                              點箭頭的寬度
  • headwidth   the width of the base of the arrow head in points  在點的箭頭底座的寬度
  • headlength  the length of the arrow head in points                   點箭頭的長度
  • shrink          fraction of total length to ‘shrink’ from both ends  總長度為分數“縮水”從兩端
  • facecolor     箭頭顏色

bbox給標題增加外框 ,常用參數如下:

  •   boxstyle方框外形
  •   facecolor(簡寫fc)背景顏色
  •   edgecolor(簡寫ec)邊框線條顏色
  •   edgewidth邊框線條大小
 bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5)  #fc為facecolor,ec為edgecolor,lw為lineweight

 


(2)案例

 

文字格式與位置:

 

 

  1.  
    import matplotlib.pyplot as plt
  2.  
    fig = plt.figure()
  3.  
    plt.axis([ 0, 10, 0, 10])
  4.  
    t = "This is a really long string that I'd rather have wrapped so that it"\
  5.  
    " doesn't go outside of the figure, but if it's long enough it will go"\
  6.  
    " off the top or bottom!"
  7.  
    plt.text( 4, 1, t, ha='left', rotation=15, wrap=True)
  8.  
    plt.text( 6, 5, t, ha='left', rotation=15, wrap=True)
  9.  
    plt.text( 5, 5, t, ha='right', rotation=-15, wrap=True)
  10.  
    plt.text( 5, 10, t, fontsize=18, style='oblique', ha='center',va='top',wrap=True)
  11.  
    plt.text( 3, 4, t, family='serif', style='italic', ha='right', wrap=True)
  12.  
    plt.text( -1, 0, t, ha='left', rotation=-15, wrap=True)
  13.  
    plt.show()

 

 
花式文本框:
  1.  
    import matplotlib.pyplot as plt
  2.  
    plt.text( 0.6, 0.5, "test", size=50, rotation=30.,ha="center", va="center",bbox=dict(boxstyle="round",ec=(1., 0.5, 0.5),fc=(1., 0.8, 0.8),))
  3.  
    plt.text( 0.5, 0.4, "test", size=50, rotation=-30.,ha="right", va="top",bbox=dict(boxstyle="square",ec=(1., 0.5, 0.5),fc=(1., 0.8, 0.8),))
  4.  
    plt.draw()
  5.  
    plt.show()


 

 

數學公式:

 

  1.  
    plt.title( r'$\alpha_i > \beta_i$', fontsize=20)
  2.  
    plt.text( 1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
  3.  
    plt.text( 0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',fontsize=20)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM