matplotlib:plt.contourf(畫等高線)

  • 2019 年 11 月 27 日
  • 筆記

import numpy as np  import matplotlib  matplotlib.use(「TkAgg」)  import matplotlib.pyplot as plt    def height(x,y):  #the height function  return(1-x/2+x5+y3)*np.exp(-x2-y2)    n=256    x=np.linspace(-3,3,n)  y=np.linspace(-3,3,n)  X,Y=np.meshgrid(x,y) #把X,Y傳入網格中,X.shape=nn,Y.shape=nn

use plt.contourf to filling contours

#X,Y and value for (X,Y) point  plt.contourf(X,Y,height(X,Y),8,alpha=0.75,cmap=plt.cm.hot)  #8:8+2=10,將高分為10部分,  #alpha:透明度  #cmap:color map    #use plt.contour to add contour lines  C=plt.contour(X,Y,height(X,Y),8,colors=「black」,linewidth=.5)    #adding label  plt.clabel(C,inline=True,fontsize=10)  #clabel:cycle的label,inline=True表示label在line內,fontsize表示label的字體大小    plt.show()