2015-10-06 60 views
1

請參見附件圖像如何y的在BARH蜱擴展空間 - 蟒蛇matplotlib

enter image description here

我源代碼在Python

def plotBarChartH(self,data): 
      LogManager.logDebug('Executing Plotter.plotBarChartH') 

      if type(data) is not dict: 
       LogManager.logError('Input data parameter is not in right format. Need a dict') 
       return False 

      testNames = [] 
      testTimes = [] 

      for val in data: 
       testNames.append(val) 
       testTimes.append(data[val]) 

      matplotlib.rcParams.update({'font.size': 8})  
      yPos = np.arange(len(testNames)) 
      plt.barh(yPos, testTimes, height=0.4, align='center', alpha=0.4) 
      plt.yticks(yPos, testNames) 
      plt.xlabel('time (seconds)') 
      plt.title('Test Execution Times') 
      savePath = os.path.join(ConfigurationManager.applicationConfig['robotreportspath'],'bar.jpg') 
      plt.savefig(savePath) 
      plt.clf() 
      return True 

如下酒吧看起來不錯,但我有兩個問題

  1. y軸上的文字如何顯示在充分?我的意思是一些文本被截斷,我想擴大佔用空間,以便可以全部顯示。

  2. 我可以增加繪製圖表的整個繪圖區嗎?我想增加繪圖區的寬度,使圖像看起來有點大

感謝

回答

1
  1. y軸上的文字如何可以完整顯示?我的意思是一些文本被截斷,我想擴大佔用空間,以便可以全部顯示。

您可以使用plt.axes來控制軸的繪製位置,以便在左側區域留出更多空間。一個例子可能是plt.axes([0.2,0.1,0.9,0.9])

  1. 我可以增加繪製圖表的整個繪圖區嗎?我想增加繪圖區域的寬度,使圖像看起來更大

我不明白你的意思。

  • 可以控制圖的使用大小plt.figure(例如,plt.figure(figsize = (6,12))
  • 可以控制信息,並使用plt.[xy]lim軸之間的空間。例如,如果您想在右側區域留出更多空白區域,則可以使用plt.xlim(200, 600)
  • 您可以使用plt.axes節省一些保證金空間(請參閱上面的問題1)。
+0

嗨,數組代表[0.2,0.1,0.9,0.9]是什麼?我在玩弄價值觀,但無法理解他們的實際表現。 – slysid

+0

它表示xmin,ymin,xmax和ymax的歸一化值。因此,如果使用0,0,1,1則沒有餘量,如果使用0.1,0.1,0.9,0.9,則在軸和圖形邊界之間的每邊都留有10%,... – kikocorreoso

1
  1. 一個選項是包含在你的字符串換行符\n(或使用類似"\n".join(wrap(longstring,60)this回答)。 可以調整你繪圖區與fig.subplots_adjust(left=0.3)確保整個字符串顯示,

實施例:

import matplotlib.pyplot as plt 
import numpy as np 

val = 1000*np.random.rand(5) # the bar lengths 
pos = np.arange(5)+.5 # the bar centers on the y axis 
name = ['name','really long name here', 
     'name 2', 
     'test', 
     'another really long \n name here too'] 

fig, ax = plt.subplots(1,1) 
ax.barh(pos, val, align='center') 
plt.yticks(pos, name) 
fig.subplots_adjust(left=0.3) 
plt.show() 

其給出

enter image description here

  • 您可以通過figsize參數調整物理圖形大小到子圖或圖。
  • 實施例:

    fig, ax = plt.subplots(1,1, figsize=(12,8)) 
    

    的圖中的空間量可以通過設置基於該數據軸線進行調整,

    ax.set_xlim((0,800)) 
    

    ax.set_xlim((0,data.max()+200))自動化。

    3

    您可以在創建Figure對象與plt.figure(figsize=(width,height)), and call plt.tight_layout()`以騰出空間給你的刻度標記明確設置的數字大小(以英寸爲單位)如下:

    import matplotlib.pyplot as plt 
    
    names = ['Content Channels','Kittens for Xbox Platform','Tigers for PS Platform', 
         'Content Series', 'Wombats for Mobile Platform'] 
    
    values = [260, 255, 420, 300, 270] 
    
    fig = plt.figure(figsize=(10,4)) 
    ax = fig.add_subplot(111) 
    yvals = range(len(names)) 
    ax.barh(yvals, values, align='center', alpha=0.4) 
    plt.yticks(yvals,names) 
    plt.tight_layout() 
    
    plt.show() 
    

    enter image description here