减少matplotlib栏中两组之间的差距

时间:2018-04-08 01:56:08

标签: python matplotlib

我使用下面的代码生成附图。我的问题是两组酒吧之间的空白太多了。我知道我可以通过增加条宽来缩小差距,但这不是我要求的。我需要保持条宽与我之前生成的其他图一样。

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


sns.set()
sns.set_style("dark")
sns.set_style("whitegrid",{"xtick.major.size": 5})
sns.set(font_scale=1.2)
plt.figure(figsize=(2,6))
sns.set_style("darkgrid")

n_groups = 2


skip=[0.651]
skip.append(sum([0.819335,0.818205])/len([0.819335,0.818205]))

Glove=[0.655]
Glove.append(sum([0.768675,0.835844])/len([0.768675,0.835844]))

RNN=[0.629]
RNN.append(sum([0.768675,0.773007])/len([0.768675,0.773007]))

Global=[0.635]
Global.append(sum([0.759134,0.764281])/len([0.759134,0.764281]))

Cross=[0.665]
Cross.append(sum([0.768675,0.838481])/len([0.768675,0.838481]))
Dist=[0.61]
Dist.append(sum([0.753107,0.755493])/len([0.753107,0.755493]))

#Comparion of Brain Bench Vs Rest
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.05
opacity = 1.0
#('Global Context', 'Skip-Gram',    'RNN',  'Cross-Lingual',    'Glove',    'Non-Distributional','Skip-Gram-Italian')

rects1 = plt.bar(index, skip, bar_width,
                 alpha=opacity,
                 color='#581845',
                 label='Skip-Gram')

rects2 = plt.bar(index + bar_width, Glove, bar_width,
                 alpha=opacity,
                 color='#FF5733',
                 label='Glove')


rects3 = plt.bar(index + bar_width + bar_width, RNN, bar_width,
                 alpha=opacity,
                 color='#000080',
                 label='RNN')

rects4 = plt.bar(index + bar_width + bar_width+bar_width, Global, bar_width,
                 alpha=opacity,
                 color='#800080',
                 label='Global Context')

rects5 = plt.bar(index + bar_width + bar_width+bar_width+bar_width, Cross, bar_width,
                 alpha=opacity,
                 color='#808000',
                 label='Cross-Lingual')

rects6 = plt.bar(index + bar_width + bar_width+bar_width+bar_width+bar_width, Dist, bar_width,
                 alpha=opacity,
                 color='#FFC300',
                 label='Non-Dist')








#algo = ('BrainBench','WS-353','WS-353-SIM', 'WS-353-REL','MEN','MTurk-771')
algo = ( 'BrainBench V1.0','BrainBench V2.0')
algo_len = np.arange(len(algo))
#plt.xticks(index + 0.15, ( 'BrainBench','WS-353','WS-353-SIM', 'WS-353-REL','MEN','MTurk-771'))
plt.xticks(index+bar_width, ( 'BrainBench V1.0','BrainBench V2.0'))
plt.ylim(0.55,0.88)
#sns.plt.title('2 vs. 2 Accuracy for Concrete/Abstract Words in Italian fMRI').set_fontsize('12')
sns.plt.title('Comparison of BrainBench Versions').set_fontsize('12')
sns.plt.ylabel('Correlation').set_fontsize('12')




plt.gcf().subplots_adjust(bottom=0.20)
plt.legend()
plt.tight_layout()
# plt.xticks(rotation=15)
#plt.show()
plt.savefig("/Users/Dhanush/Desktop/Versions.png", dpi=300)

Two groups of bar separated by large gap

这里的基本问题是两组之间的差距很大。我需要用一个条宽

分隔两组

1 个答案:

答案 0 :(得分:0)

我更改了代码以获得所需的结果。 使用列表中的自定义位置替换下面的代码段中的索引和条形宽度。此处列表显示绘制条形图的确切位置。我给了第一条0.1和第二条0.5。每个条宽为0.05,有6个条。所以0.1 + 6 * 0.05 + 0.1作为两组之间的间隙,这给出了0.5作为skip-gram第二条图的起始位置。

rects1 = plt.bar(index, skip, bar_width,
                 alpha=opacity,
                 color='#581845',
                 label='Skip-Gram')

已更改为

rects1 = plt.bar([0.1, 0.5], skip, bar_width,
                 alpha=opacity,align='center',
                 color='#581845',
                 label='Skip-Gram')

enter image description here