在MATLAB R2015b中设置颜色条的Alpha

mac*_*ery 3 matlab alpha colorbar

我想在我的情节中设置一些透明度,我可以这样做alpha。这很好用,但我也想调整颜色条。这是一个例子:

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
Run Code Online (Sandbox Code Playgroud)

该示例从此处获取。在此页面上,存在两种解决方案,但都不适用于Matlab R2015b。

在此处输入图片说明

Sue*_*ver 5

使用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;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

您必须小心执行此操作,因为彩条会不断刷新,并且这些更改不会保留下来。为了让他们留下来,而我打印的身影,我只是改变了ColorBinding的模式Face来的东西,除了interpolated

c.Face.ColorBinding = 'discrete';
Run Code Online (Sandbox Code Playgroud)

这意味着,当您更改颜色限制或颜色表时,不会对其进行更新。如果您要更改其中任何一项,则需要将重置ColorBindingintepolated,然后再次运行上述代码。

c.Face.ColorBinding = 'interpolated';
Run Code Online (Sandbox Code Playgroud)

例如,以下代码将使用带有透明颜色栏的图像保存两个颜色图:

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');
Run Code Online (Sandbox Code Playgroud)