2014-11-09 29 views
0

我正在製作一個工具,使用Matplotlib自動化一些圖表,對於我遇到的每個問題,通常在一些研究結束後我終於找到解決方案,但在這裏,米卡住了。Matplotlib,在圖像中設置圖表的位置

1 - 我想要的是,在生成的png文件中,在左側開始圖形之前,我可以插入一些文本對象。如何做到這一點?

2 - 我也希望能夠在圖下插入文本,所以它會很有用。

主要我想能夠定義圖像文件中的情節的位置,下面是我目前have以及什麼I want的例子。

編輯:

def plot_bar(): 


    fig = plt.figure(figsize=(600/96, 360/96), dpi = 96) 


# Setting the padding between the ticks' labels and the axes 
    mpl.rcParams['xtick.major.pad']='10' 

    ax = plt.gca() 

    ax.spines['top'].set_visible(False) 
    ax.spines['right'].set_visible(False) 
    ax.spines['left'].set_visible(False) 
    ax.spines['bottom'].set_visible(False) 


    ax.axes.get_yaxis().set_visible(False) 

    mpl.rcParams['axes.facecolor'] = 'white' 
    mpl.rcParams["figure.facecolor"]= 'white' 
    mpl.rcParams["savefig.facecolor"]= 'white' 
    Values1 = (10 , 25 , 30 , 40) 
    Values2 = (40 , 3 , 10 , 6) 

    N = 4 

    ind = np.arange(N) 

    width = 0.7 
p1 = plt.bar(ind, Values1, width, color='#00A79A', ec='#00A79A') 
    p2 = plt.bar(ind, Values2, width, color='#8FDCD6', bottom = Values1, ec='#8FDCD6') 


    plt.xticks(ind+width/2., ('Fuel\n\n dollars \n\n dollars', 'Tires', 'Roadcalls', 'Maintenance')) 
    plt.yticks(np.arange(0,81,10)) 

    # Removing Tick lines from axes 
    for tic in ax.xaxis.get_major_ticks(): 
    tic.tick1On = tic.tick2On = False 

    for tic in ax.yaxis.get_major_ticks(): 
    tic.tick1On = tic.tick2On = False 
    plt.savefig("fichier.png") 
+0

你能給我們提供你用來形成圖的代碼嗎? – grovesNL 2014-11-09 01:07:33

+0

當然,我會添加它。 – 2014-11-09 01:08:29

+0

如果所提供的解決方案適合您,您能否讓我們知道?如果沒有,我們可以相應地更新它們。 – grovesNL 2014-11-09 07:08:53

回答

0

您可以使用subplots_adjust添加間距。 tight_layout也將擴展您的方框以適應之前(「美元」)從圖中缺失的文本。

至於其餘的情節,應該是相當容易添加標籤。例如,下面的for循環會在每個小節的頂部添加總計(類似於this demo)。

for bar in p2: 
    height = bar.get_height() + p1[bar.get_x()].get_height() 
    ax.text(bar.get_x()+bar.get_width()/2., 1.05*height, '%d'%int(height), 
      ha='center', va='bottom') 
plt.tight_layout() 
plt.subplots_adjust(left=0.3, bottom=0.3) 
+0

謝謝,這正是我想要的。 – 2014-11-09 08:19:32

0

嘗試使用subplots_adjust圖方法:

fig.subplots_adjust(left=0.25, bottom=0.25)