2017-07-31 30 views
0

我想分析四個工具在運行多個程序時的性能。一個子圖是一個工具在所有程序上的結果。結果應該是這樣的: enter image description herePython:專門爲子圖中的條設置x tick?

我用for循環遍歷程序列表中的每一次繪製一個部分如下:

enter image description here

但情節看起來像一個一個和我不能通過使用axis.set_xticks()來分開它們的x標記。看來這個功能沒有效果。

我是否使用正確的功能來設置x刻度?或者我應該如何製作這個情節?

draw_hist_query()可能是最重要的功能對於我的問題

數據樣本:

boolector,ppbv,stp,z3 
0.05349588394165039,0.015434503555297852,0.028127193450927734,0.11303281784057617 
0.0027561187744140625,0.004331827163696289,0.007134914398193359,0.016040563583374023 
0.003190755844116211,0.005587577819824219,0.002897500991821289,0.013916015625 
0.009758472442626953,0.02006363868713379,0.0031282901763916016,0.011539697647094727 
0.057138681411743164,0.012826681137084961,0.030836820602416992,0.0217435359954834 

代碼:

index = range(len(solvers)) 
fig, axes = plt.subplots(nrows=4) 
solvers = ['z3', 'stp', 'boolector', 'ppbv'] 
colors = ['g', 'c', 'b', 'r', 'y', 'orange', 'grey'] 
ticks = [0.1, 0.5, 1.0, 2.0] 
width=0.2 

# program entry 
def all_time_query(path): 
    csv = xxx.csv # the array of data to be analyzed, one csv for one program 
    for axis in axes: 
     axis.set_xticks(range(len(csv))) 
    for c in csv: 
     multi_time_query(c) # draw the bar pair for c, which shows the upper image for one program on four tools 


def multi_time_query(csv): 
    data = pd.read_csv(csv) 
    for solver in solvers: # the four tools 
     bin = index[solvers.index(solver)] 
     hist_t_query(data, solver, ax=axes[bin]) # details to draw the bar pair, uses dataframe.plot.bar 



def hist_t_query(data, solver, ax): 
    solver_data = pd.DataFrame(data).as_matrix(columns=[solver]) 
    # draw one bar for demo 
    draw_hist_query(pd.DataFrame(solver_data), ax) 


# left of bar pair, the right one is similar 
def draw_hist_query(df, ax): 
    count = [] 
    for i in range(len(ticks)): 
     count.append(df[df < ticks[i]].count()) 
     color = stat.colors[i] 
     if i == 0: 
      count[i].plot.bar(ax=ax, color=color, width=width, position=0) 
     else: 
      (count[i] - count[i - 1]).plot.bar(bottom=count[i - 1], 
               ax=ax, color=color, width=width, position=0) 
+1

你可以添加一些代碼?你使用子圖嗎?如果是這樣,那麼你可以用subplots_adjust調整它們http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.subplots_adjust –

+0

@ KacperWolkowski添加了一些代碼 –

+0

我想你還需要顯示'hist_t_query'(in特別是你調用'dataframe.plot.bar'的部分),否則很難知道發生了什麼。 –

回答

0

最後我解決了這個問題: enter image description here

我的想法錯之前大約是次要情節。我想在一對子已經在那裏之後在另一個子圖中添加另一個子對。 但是,一個小區應該一起繪製(一次),不應該分開。在我的情況下,一個子圖的條應該一起出現,並且只需要四次來繪製所有的子圖。

這裏是我的代碼新版本:

def time_query_project(path): 
    fig, axis = plt.subplots(nrows=4) 
    csv = sio.find_csv(path) 
    data = {} 

    for solver in solvers: 
     for c in csv: 
      df = pd.DataFrame(pd.read_csv(c), columns=[solver]) 
      data.update({get_name(c): df.to_dict()[solver]}) 
     df = pd.DataFrame.from_dict(data, orient='columns') 
     ax = axis[solvers.index(solver)] 
     ax.set_ylabel(solver) 
     hist_t_query(df, ax) 


def hist_t_query(data, solver, ax): 
    solver_data = pd.DataFrame(data).as_matrix(columns=[solver]) 
    # draw one bar for demo 
    draw_hist_query(pd.DataFrame(solver_data), ax) 


# left of bar pair, the right one is similar 
def draw_hist_query(df, ax): 
    count = [] 
    for i in range(len(ticks)): 
     count.append(df[df < ticks[i]].count()) 
     color = stat.colors[i] 
     if i == 0: 
      count[i].plot.bar(ax=ax, color=color, width=width, position=0) 
     else: 
      (count[i] - count[i - 1]).plot.bar(bottom=count[i - 1], 
               ax=ax, color=color, width=width, position=0) 
0

一般來說,你有幾種選擇。您可以使用plt.tight_layout(),它會自動執行所有操作,或者您可以使用plt.subplot_adjust()並自行指定每個參數。 正如你可以在文檔中看到,簽名是這樣的:

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

而且,如果你會去交互式窗口,你必須有調整參數的選項,你可以看到你的圖形將每個參數

enter image description here

更改,然後就可以調用subplot_adjust與任何最適合你。

我希望它有幫助。

+0

其實我的問題是在** subplots **上,我自己解決了這個問題。如果你有興趣,請看我的答案。謝謝你們一樣:) –