2017-02-23 109 views
0

我工作的堆疊barplot,正如你所看到的,它有一些問題: enter image description here 1)如何,我可以添加到阿爾法表中的顏色?我嘗試在the_table中加入alpha=0.75,但它沒有做任何事情。
2)如何增加表格行的高度以適應文本?
3)如何移動'3/1'欄使其居中在表格欄上?
4)如何移動圖例使其位於圖上方?
5)如何修復正在切斷的圖例?Matplot表格,圖例,堆疊barplot問題

這裏是我的代碼:

import matplotlib.pyplot as plt 
import numpy as np 

res = (0, 21) 
canc = (21, 0) 
me = (37, 37) 
ee = (4, 4) 
sw = (16, 16) 
te = (31, 31) 
spec = (1, 1) 
sys = (2, 2) 
lm = (9, 9) 

data = [res, canc, me, ee, sw, te, spec, sys, lm] 
labels = ['Resolved', 'To be cancelled', 'Mech Eng', 'Elec Eng', 'Software', 'Test Eng', 'Specialty', 'Systems', 'LM Review'] 
dates = ('2/22', '3/1 (Projected)') 

fig, ax = plt.subplots() 

index = np.arange(len(dates)) 
bar_width = 0.25 
y_offset = np.array([0.0] * len(dates)) 

gray = plt.cm.gray(0.5) 
yellow = plt.cm.hot(0.9) 
green = plt.cm.Greens(0.5) 
magenta = plt.cm.RdPu(0.5) 
orange = plt.cm.Oranges(0.5) 
purple = plt.cm.Purples(0.5) 
greenblue = plt.cm.GnBu(0.4) 
red = plt.cm.Reds(0.5) 
blue = plt.cm.Blues(0.5) 
colors = [gray, yellow, green, magenta, orange, purple, greenblue, red, blue] 

cell_text = [] 
for row in range(len(data)): 
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row], alpha=0.75, align='center', label=labels[row]) 
    y_offset = y_offset + data[row] 

colors = colors[::-1] 
data.reverse() 
labels.reverse() 

the_table = plt.table(cellText=data, rowLabels=labels, rowColours=colors, alpha=0.1, colLabels=dates, loc='bottom', cellLoc='center') 
the_table.scale(1, 1.6) 

plt.ylabel('Open Items') 

plt.subplots_adjust(left=0.2, bottom=0.35) 

plt.xticks([]) 
plt.yticks(np.arange(0, 130, 5)) 
ax.set_xlim(-0.5, 1.5) 
plt.legend() 

handles, labels = ax.get_legend_handles_labels() 
ax.legend(handles[::-1], labels[::-1], bbox_to_anchor=(1., 1.15), ncol=4) 

plt.show() 

編輯:實測值:
該溶液至#2 & 3#。
解決方法#1,但仍然希望能夠實際應用阿爾法而不是索引顏色。
得到了我想要它的傳說,但現在它被切斷了。

回答

1

1)添加alpha:您可以使用相應顏色的alpha通道。這是通過將顏色轉換爲rgba,然後將元組的alpha設置爲期望值來完成的。

alpha = 0.75 
colors = ['gray', 'yellow', 'green'] 
cols=[] 
for c in colors: 
    col = list(matplotlib.colors.to_rgba(c)) 
    col[3] = alpha 
    cols.append(col) 

2)中心說明上述的圖:這是所有位取決於圖大小,字體大小等等,因此理想的參數需要通過反覆試驗來發現,但想法可以是放置(x = 0.5)和頂部(y = 1.0)的圖例邊界框,並指定loc參數爲'lower center',這意味着圖例的底部中心應位於該點。使得y有點大,在圖例和座標軸之間產生一些填充。

這是完整的腳本,我也調整了圖形的大小讓所有的情節都適合畫布。

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.colors 

res = (0, 21) 
canc = (21, 0) 
me = (37, 37) 
ee = (4, 4) 
sw = (16, 16) 
te = (31, 31) 
spec = (1, 1) 
sys = (2, 2) 
lm = (9, 9) 

data = [res, canc, me, ee, sw, te, spec, sys, lm] 
labels = ['Resolved', 'To be cancelled', 'Mech Eng', 'Elec Eng', 'Software', 'Test Eng', 'Specialty', 'Systems', 'LM Review'] 
dates = ('2/22', '3/1 (Projected)') 

fig, ax = plt.subplots(figsize=(6,6)) #<- set figure size large enough for data 

index = np.arange(len(dates)) 
bar_width = 0.25 
y_offset = np.array([0.0] * len(dates)) 

# set alpha to colors: 
alpha = 0.75 
colors = ['gray', 'yellow', 'green', 'magenta', 'orange', 'palevioletred', 'mediumspringgreen', 'red', 'blue'] 
cols=[] 
for c in colors: 
    col = list(matplotlib.colors.to_rgba(c)) 
    col[3] = alpha 
    cols.append(col) 
colors = cols[::-1] 

cell_text = [] 
for row in range(len(data)):        #colors need to be inverted here as well, don't they? 
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[::-1][row], alpha=0.75, align='center', label=labels[row]) 
    y_offset = y_offset + data[row] 


data.reverse() 
labels.reverse() 

the_table = plt.table(cellText=data, rowLabels=labels, rowColours=colors, alpha=0.1, colLabels=dates, loc='bottom', cellLoc='center') 
the_table.scale(1, 1.6) 

plt.ylabel('Open Items') 

plt.subplots_adjust(left=0.22, bottom=0.35, right=0.78, top=0.82) # <- allocate some spacing for legend on top and on right as well 

plt.xticks([]) 
plt.yticks(np.arange(0, 130, 5)) 
ax.set_xlim(-0.5, 1.5) 
#plt.legend() <- remove, we only need one single legend defined below 

handles, labels = ax.get_legend_handles_labels() 
# loc=8 means the bbox ccordinates define the lower center of the legend 
# so placing it at x=0.5 (horizontal center of the axes), y=1.02 (vertical top of the axes) 
ax.legend(handles[::-1], labels[::-1], loc=8, bbox_to_anchor=(0.5, 1.02), ncol=4) 

plt.show() 

enter image description here

+0

感謝。我收到一個錯誤:'AttributeError:'模塊'對象沒有'to_rgba''屬性。看起來我已經安裝了1.5版本,我猜'to_rgba'只有2.0版本? – drewd423

+0

這可能是。 'matplotlib.colors.colorConverter.to_rgba(c)'是否會起作用? – ImportanceOfBeingErnest

+0

它沒有。試圖現在下載並安裝2.0。 – drewd423