2016-05-24 60 views
3

我想在我的圖中設置一些透明度,我可以用alpha來做。這很好,但我也想調整顏色條。以下是一個示例:在MATLAB中設置colorbar的alpha值R2015b

subplot(2,1,1) 
A = imagesc(meshgrid(0:10,0:5)); 
alpha(A,1) 
colorbar 

subplot(2,1,2) 
B = imagesc(meshgrid(0:10,0:5)); 
alpha(B,.1) 
colorbar 

該示例摘自here。在這個頁面上有兩個解決方案,但沒有適用於Matlab R2015b。

enter image description here

+0

你嘗試'HB =彩條; alpha(hb,.1);'? –

+0

@MadPhysicist是的,這是行不通的。 – machinery

回答

4

隨着HG2圖形(R2014b +),你可以得到一些無證底層情節的對象,並改變透明度。

c = colorbar(); 

% Manually flush the event queue and force MATLAB to render the colorbar 
% necessary on some versions 
drawnow 

alphaVal = 0.1; 

% Get the color data of the object that correponds to the colorbar 
cdata = c.Face.Texture.CData; 

% Change the 4th channel (alpha channel) to 10% of it's initial value (255) 
cdata(end,:) = uint8(alphaVal * cdata(end,:)); 

% Ensure that the display respects the alpha channel 
c.Face.Texture.ColorType = 'truecoloralpha'; 

% Update the color data with the new transparency information 
c.Face.Texture.CData = cdata; 

enter image description here

你必須要小心,這樣做的彩條是不斷刷新,這些變化不會粘。爲了讓他們留下來,而我打印的身影,我只是改變了Face的東西的ColorBinding模式除了interpolated

c.Face.ColorBinding = 'discrete'; 

這意味着,當你改變顏色限制或顏色表也不會被更新。如果您想更改其中的任何一項,則需要將ColorBinding重置爲intepolated,然後再次運行上述代碼。

c.Face.ColorBinding = 'interpolated'; 

例如,以下將與一個透明顏色條保存的圖像兩個色彩映射表:

c = colorbar(); 

drawnow; 

alphaVal = 0.1; 

% Make the colorbar transparent 
cdata = c.Face.Texture.CData; 
cdata(end,:) = uint8(alphaVal * cdata(end,:)); 
c.Face.Texture.ColorType = 'truecoloralpha'; 
c.Face.Texture.CData = cdata; 

drawnow 

% Make sure that the renderer doesn't revert your changes 
c.Face.ColorBinding = 'discrete'; 

% Print your figure 
print(gcf, 'Parula.png', '-dpng', '-r300'); 

% Now change the ColorBinding back 
c.Face.ColorBinding = 'interpolated'; 

% Update the colormap to something new 
colormap(jet); 

drawnow 

% Set the alpha values again 
cdata = c.Face.Texture.CData; 
cdata(end,:) = uint8(alphaVal * cdata(end,:)); 
c.Face.Texture.CData = cdata; 

drawnow 

% Make sure that the renderer doesn't revert your changes 
c.Face.ColorBinding = 'discrete'; 

print(gcf, 'Ugly_colormap.png', '-dpng', '-r300'); 
+0

不知何故,這對我不起作用。我收到錯誤「下標索引必須是真正的正整數或邏輯」。因爲大小(cdata)= [0 0]。 – machinery

+0

@machinery嗯奇怪。什麼OS? – Suever

+0

我有Windows 10,64位。 – machinery