在 MATLAB R2015b 中设置 colorbar 的 alpha
Setting alpha of colorbar in MATLAB R2015b
我想在我的情节中设置一些透明度,我可以用 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
例子取自here。此页面上存在两个解决方案,但 none 适用于 Matlab R2015b。
使用 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;
执行此操作时必须小心,因为颜色条会不断刷新,并且这些更改不会保留。为了让它们在我打印图形时留下来,我只是将 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');
Suever 的解决方案有效,但是在实时编辑器 (R2020b) 中工作时我不得不添加:
set(gcf,'Visible','on')
这使得图形绘制在外部弹出窗口中。然后它确实有效,但我不知道为什么。
实时编辑器的工作示例:
figure(1);clf;
set(gcf,'Visible','on')
c = colorbar();
drawnow;
alphaVal = 0.5;
% 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;
我想在我的情节中设置一些透明度,我可以用 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
例子取自here。此页面上存在两个解决方案,但 none 适用于 Matlab R2015b。
使用 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;
执行此操作时必须小心,因为颜色条会不断刷新,并且这些更改不会保留。为了让它们在我打印图形时留下来,我只是将 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');
Suever 的解决方案有效,但是在实时编辑器 (R2020b) 中工作时我不得不添加:
set(gcf,'Visible','on')
这使得图形绘制在外部弹出窗口中。然后它确实有效,但我不知道为什么。
实时编辑器的工作示例:
figure(1);clf;
set(gcf,'Visible','on')
c = colorbar();
drawnow;
alphaVal = 0.5;
% 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;