2017-08-02 111 views
1

改變寬度使其變小後,它已移到側面。我如何刪除它們之間的間距。理想情況下,我想居中,但在文檔中似乎沒有設置參數。手動縮放圖片也不會減少這種差距。巴至0和1如何刪除條形圖中條形之間的x軸間距?

import numpy as np 
import matplotlib.pyplot as plt 

objects = ('bar1', 'bar2') 
y_pos = np.arange(len(objects)) 
performance = [2,6] 
stds=[0.3,0.5] 
plt.bar(y_pos, performance, 0.3, align='center', yerr=stds,capsize=5, alpha=0.5) 
plt.xticks(y_pos, objects) 
plt.ylabel('Time (seconds)') 
plt.show() 

enter image description here

+0

使用'plt.figure(1,figsize =(寬度,高度))'其中'width'和'height'表示以英寸圖大小, – Flomp

+0

@Flomp不幸的是,該間隙還甚至極端值保持 – Anderson

回答

2

y_pos列表集位置那麼你的設定寬度到0.3和間隙是0.7。

您必須將y_pos替換爲某些值才能使您的酒吧彼此靠近:第一個酒吧必須位於width/2位置,第二個酒吧位於1.5 * width。 然後,您必須用xlim方法來選擇x軸的最優極限,以便對中棒進行居中。

import numpy as np 
import matplotlib.pyplot as plt 

objects = ('bar1', 'bar2') 
w = 0.3 
y_pos = (w/2.,w*1.5) 
performance = [2,6] 
stds=[0.3,0.5] 
plt.bar(y_pos, performance, width=w, align='center', yerr=stds,capsize=5, alpha=0.5) 
plt.gca().set_xlim([-1.,1.5]) 
plt.xticks(y_pos, objects) 
plt.ylabel('Time (seconds)') 
plt.show() 

我希望有更多的靈活和優雅的解決方案。 enter image description here

相關問題