在MATLAB R2015b中设置颜色条的Alpha [英] Setting alpha of colorbar in MATLAB R2015b

查看:108
本文介绍了在MATLAB R2015b中设置颜色条的Alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在绘图中设置一些透明度,可以使用alpha进行设置.这很好用,但我也想调整颜色条.这是一个示例:

I would like to set some transparency in my plot which I can do with alpha. This works great but I also want to adapt the colorbar. Here is an example:

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

该示例摘自此处.在此页面上,存在两种解决方案,但都不适用于Matlab R2015b.

The example is taken from here. On this page two solutions exists but none works for Matlab R2015b.

推荐答案

使用HG2图形(R2014b +),您可以获取一些未公开的基础绘图对象并更改透明度.

With HG2 graphics (R2014b+) you can get some of the undocumented underlying plot objects and alter the transparency.

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;

您必须小心,因为颜色栏会不断刷新,并且这些更改不会保留.为了让他们在打印图形时停留,我只是将FaceColorBinding模式更改为interpolated

You have to be careful doing this as the colorbar is continually refreshed and these changes will not stick. To get them to stay while I printed the figure, I just changed the ColorBinding mode of the Face to something besides interpolated

c.Face.ColorBinding = 'discrete';

这意味着当您更改颜色限制或颜色图时,不会对其进行更新.如果要更改其中任何一项,则需要将ColorBinding重置为intepolated,然后再次运行上面的代码.

This means that it won't be updated when you change color limits or the colormap. If you want to change either of those things you'll need to reset the ColorBinding to intepolated and then run the above code again.

c.Face.ColorBinding = 'interpolated';

例如,以下操作将使用带有透明色条的图像保存两个色图:

For example, the following would save an image with a transparent colorbar for two colormaps:

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');

这篇关于在MATLAB R2015b中设置颜色条的Alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆