2014-07-01 13 views
0

創建分級標籤要爲這樣的barplot創建matplotlib分級標籤:我如何在matplotlib

barplot

兩個競爭對手在5次比較。我想獲得類似於

Competitor1 Competitor2  Competitor1 Competitor2 
      1       2 

標籤是否有可能在matplotlib或我必須手動添加後呢?

感謝

我的代碼:

# imports 
import pandas as pd 
import sys 
import itertools 
import os 
import matplotlib as mpl 
from matplotlib import pyplot as plt 
import numpy as np 

# axes limits 
x_min = 1 
y_min = 0 
x_max = None 
y_max = None 

bar_width = 0.4 

bar_offset = -bar_width 

for method in methods: 

    # load method's data 
    data = pd.read_table(fpath, sep='\t', index_col=0) 

    # update x position with offset 
    x = data.index + bar_offset 

    # grey is always higher than black 
    y_grey = data['grey'] 
    y_black = data['black'] 

    # update x-axis upper limit 
    x_max = max(x) if x_max is None else max(max(x), x_max) 

    # update y-axis upper limit 
    if y_max is None: 
     y_max = max(y_grey + y_grey) 
    else: 
     y_max = max(max(y_grey + y_grey), y_max) 

    # plot grey and black bars 
    color = 'k' 
    plt.bar(x, y_grey, color=color, alpha=0.65, width=bar_width) 
    plt.bar(x, y_black, color=color, alpha=1, width=bar_width) 

    # update offset 
    bar_offset += bar_width + 0.01 


# labels and limits 
xlabel = 'Classes' 
plt.xlabel(xlabel) 
plt.xlim(x_min - 0.6, x_max + 0.6) 
plt.ylim(y_min - 1, y_max + 1) 

# squared aspect 
ax = plt.axes() 
aspect = np.diff(ax.get_xlim())/np.diff(ax.get_ylim()) 
ax.set_aspect(aspect) 

# save figure 
plt.savefig(output_fn) 
plt.close() 
+0

我會建議添加相關的繪圖代碼。 – mehmetminanc

+0

Acc,我忘了。這裏是.. – gc5

回答

1

我在hackish的方式實現的,但它服務的宗旨:

barplot

基本上我把標籤由我和我增加他們和軸之間的差距。

# imports 
import pandas as pd 
import sys 
import itertools 
import os 
import matplotlib as mpl 
from matplotlib import pyplot as plt 
import numpy as np 

# increase margin 
mpl.rcParams['xtick.major.pad'] = 35. 

# axes limits 
x_min = 1 
y_min = 0 
x_max = None 
y_max = None 

bar_width = 0.4 

bar_offset = -bar_width 

for method in methods: 

    # load method's data 
    data = pd.read_table(fpath, sep='\t', index_col=0) 

    # get label from data for method (not shown here) 
    label = get_label(data) 

    # update x position with offset 
    x = data.index + bar_offset 

    # grey is always higher than black 
    y_grey = data['grey'] 
    y_black = data['black'] 

    # update x-axis upper limit 
    x_max = max(x) if x_max is None else max(max(x), x_max) 

    # update y-axis upper limit 
    if y_max is None: 
     y_max = max(y_grey + y_grey) 
    else: 
     y_max = max(max(y_grey + y_grey), y_max) 

    # plot grey and black bars 
    color = 'k' 
    plt.bar(x, y_grey, color=color, alpha=0.65, width=bar_width) 
    plt.bar(x, y_black, color=color, alpha=1, width=bar_width) 

    # put the labels 
    for xx in x: 
    plt.text(xx + bar_width/2 - 0.1, -8, label, rotation=60, 
      color=mpl.rcParams['xtick.color'], 
      size=mpl.rcParams['xtick.labelsize']) 

    # update offset 
    bar_offset += bar_width + 0.01 


# labels and limits 
xlabel = 'Classes' 
plt.xlabel(xlabel) 
plt.xlim(x_min - 0.6, x_max + 0.6) 
plt.ylim(y_min - 1, y_max + 1) 

# squared aspect 
ax = plt.axes() 
aspect = np.diff(ax.get_xlim())/np.diff(ax.get_ylim()) 
ax.set_aspect(aspect) 

# save figure 
plt.savefig(output_fn) 
plt.close() 
+0

看看'scipy.dendrogram' – tacaswell

+0

謝謝@tcaswell,我會試試看。 – gc5