溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Matplotlib繪圖之屬性設置

發(fā)布時間:2020-06-25 01:16:26 來源:網絡 閱讀:1024 作者:nineteens 欄目:編程語言

  (1)、導入庫

  import matplotlib.pyplot as plt

  import numpy

  (2)、figure對象和subplot簡單運用

  #figure對象

  fig = plt.figure() #figure是圖象對象

  ax1 = fig.add_subplot(2,2,1) #創(chuàng)建一個2*2的子圖,放在第一個位置

  ax2 = fig.add_subplot(2,2,2) #創(chuàng)建一個2*2的子圖,放在第二個位置

  ax3 = fig.add_subplot(2,2,3) #創(chuàng)建一個2*2的子圖,放在第三個位置

  from numpy.random import randn

  plt.plot(randn(50).cumsum(),'k--') #'k--'告訴python要畫出黑色的虛線

  ax1.hist(randn(100),bins=20,color='k',alpha=0.3)

  (3)、調整subplot周圍的間距

  #調整subplot周圍的間距

  fig,axes = plt.subplots(2,2,sharex=True,sharey=True) #直接生成fiure對象和Axes實例(數(shù)組)

  for i in range(2):

  for j in range(2):

  axes[i, j ].hist(numpy.random.randn(500),bins = 50,color='k',alpha=0.5) #遍歷創(chuàng)建圖

  plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0,hspace=0) #用于調整subplot周圍的間距

  

Matplotlib繪圖之屬性設置


  (4)、顏色、標記和線型

  #顏色、標記和線型

  plt.figure()

  ##linestyle設置線型,color設置顏色,marker設置設置連接點

  plt.plot(numpy.random.randn(30).cumsum(),linestyle='--',color='g',marker='o')

  

Matplotlib繪圖之屬性設置


  (5)、設置標題、軸標簽,刻度以及刻度標簽

  #設置標題、軸標簽,刻度以及刻度標簽

  fig = plt.figure() #創(chuàng)建figure對象

  ax = fig.add_subplot(1,1,1) #獲得Axes對象

  ax.plot(numpy.random.randn(1000).cumsum()) #生成隨機數(shù)

  ax.set_xticks([0,250,500,750,1000]) #設置x軸刻度

  ax.set_yticks([-20,-10,0,10,20]) #設置y軸刻度

  ax.set_title('My first matplotlib plot') #設置標題

  ax.set_xlabel('Xtages') #設置x軸標簽

  ax.set_ylabel('Ytages') #設置y軸標簽

  

Matplotlib繪圖之屬性設置


  (6)、添加圖例

  #添加圖例

  fig = plt.figure()

  ax = fig.add_subplot(1,1,1)無錫婦科醫(yī)院排行 http://www.0510bhyy.com/

  ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標簽用于添加圖例

  ax.plot(numpy.random.randn(1000).cumsum(),'k',label='two')

  ax.plot(numpy.random.randn(1000).cumsum(),'k',label='three')

  ax.legend(loc='best') #loc選項可以選擇圖例的位置

  

Matplotlib繪圖之屬性設置


  (7)、添加注釋

  #添加注釋

  fig = plt.figure()

  ax = fig.add_subplot(1,1,1)

  ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標簽用于添加圖例

  plt.annotate("Important value", (55,20), xycoords='data', #添加注釋的方法

  xytext=(5, 38),

  arrowprops=dict(arrowstyle='->'))

  (8)、繪制常用圖形

  #繪制常用圖形

  fig = plt.figure()

  ax = fig.add_subplot(1,1,1)

  rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3) #長方形

  circ = plt.Circle((0.7,0.2),0.15,color='b',alpha=0.3) #橢圓形

  ax.add_patch(rect) #添加到圖版中

  ax.add_patch(circ)

  (9)、圖表的導出

  #圖表的保存

  fig = plt.figure()

  ax = fig.add_subplot(1,1,1)

  rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3) #長方形

  ax.add_patch(rect) #添加到圖版中

  fig.savefig('figpath.png',dpi = 400,bbox_inches='tight') #dpi可以控制圖象的分辨率,bbox_inches可以剪除圖表的空白部分


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI