2016-09-07 58 views
0

我有一個數據集,看起來像這樣添加多個XTICK標籤:展望爲兩條平行的條形圖

enter image description here

我要做到以下幾點:

  1. 確保酒吧做不重疊。
  2. 將每個條視爲單獨的數據集,即x軸上的標籤應該是分開的,一個用於黃色系列,一個用於紅色系列。這些標籤應該是的話(我想有兩個系列的圖表中XTICK標籤),一個用於words_2,一個爲words_1 ..

當前代碼:

import matplotlib.pyplot as plt 
import numpy as np 
import copy 
import random 
from random import randint 

random.seed(11) 

word_freq_1 = [('test', 510), ('Hey', 362), ("please", 753), ('take', 446), ('herbert', 325), ('live', 222), ('hate', 210), ('white', 191), ('simple', 175), ('harry', 172), ('woman', 170), ('basil', 153), ('things', 129), ('think', 126), ('bye', 124), ('thing', 120), ('love', 107), ('quite', 107), ('face', 107), ('eyes', 107), ('time', 106), ('himself', 105), ('want', 105), ('good', 105), ('really', 103), ('away',100), ('did', 100), ('people', 99), ('came', 97), ('say', 97), ('cried', 95), ('looked', 94), ('tell', 92), ('look', 91), ('world', 89), ('work', 89), ('project', 88), ('room', 88), ('going', 87), ('answered', 87), ('mr', 87), ('little', 87), ('yes', 84), ('silly', 82), ('thought', 82), ('shall', 81), ('circle', 80), ('hallward', 80), ('told', 77), ('feel', 76), ('great', 74), ('art', 74), ('dear',73), ('picture', 73), ('men', 72), ('long', 71), ('young', 70), ('lady', 69), ('let', 66), ('minute', 66), ('women', 66), ('soul', 65), ('door', 64), ('hand',63), ('went', 63), ('make', 63), ('night', 62), ('asked', 61), ('old', 61), ('passed', 60), ('afraid', 60), ('night', 59), ('looking', 58), ('wonderful', 58), ('gutenberg-tm', 56), ('beauty', 55), ('sir', 55), ('table', 55), ('turned', 54), ('lips', 54), ("one's", 54), ('better', 54), ('got', 54), ('vane', 54), ('right',53), ('left', 53), ('course', 52), ('hands', 52), ('portrait', 52), ('head', 51), ("can't", 49), ('true', 49), ('house', 49), ('believe', 49), ('black', 49), ('horrible', 48), ('oh', 48), ('knew', 47), ('curious', 47), ('myself', 47)] 



word_freq_2 = [((tuple[0], randint(1,500))) for i,tuple in enumerate(word_freq_1)] 

N = 25 
ind = np.arange(N) # the x locations for the groups 
width = 0.35  # the width of the bars 

fig, ax = plt.subplots() 

words_1 = [x[0] for x in word_freq_1][:25] 
values_1 = [int(x[1]) for x in word_freq_1][:25] 

words_2 = [x[0] for x in word_freq_2][:25] 
values_2 = [int(x[1]) for x in word_freq_2][:25] 

print words_2 

rects1 = ax.bar(ind, values_1, color='r') 

rects2 = ax.bar(ind + width, values_2, width, color='y') 

# add some text for labels, title and axes ticks 
ax.set_ylabel('Words') 
ax.set_title('Word Frequencies by Test and Training Set') 
ax.set_xticks(ind + width) 
ax.set_xticklabels(words_2,rotation=90) 
ax.tick_params(axis='both', which='major', labelsize=6) 
ax.tick_params(axis='both', which='minor', labelsize=6) 
fig.tight_layout() 

ax.legend((rects1[0], rects2[0]), ('Test', 'Train')) 

plt.savefig('test.png') 

回答

0

我找到了解決辦法對此。關鍵是將xticks設置爲minormajor兩種。另外,重疊條紋是由於我沒有指定rects1的條寬。

rects1 = ax.bar(ind, values_1, width,color='r') 

rects2 = ax.bar(ind + width, values_2, width, color='y') 

ax.set_ylabel('Words') 
ax.set_title('Word Frequencies by Test and Training Set') 
ax.set_xticks(ind,minor=False) 
ax.set_xticks(ind + width,minor=True) 
ax.set_xticklabels(words_1,rotation=90,minor=False,ha='left') 
ax.set_xticklabels(words_2,rotation=90,minor=True,ha='left') 
ax.tick_params(axis='both', which='major', labelsize=6) 
ax.tick_params(axis='both', which='minor', labelsize=6) 

導致:

enter image description here