MATLAB Answers

ilupi
1

How to make contour plots transparent - in matlab R2015a?

Asked by ilupi
on 22 Oct 2015
Latest activity Commented on by Thoralf Stein on 4 Apr 2019
Hi, how can I make several contourf plots which are transparent? I tried the facealpha but the contour class has no facealpha property. Is there another way to make the contourf plots transparent?

  0 Comments

Sign in to comment.

3 Answers

Answer by Walter Roberson
on 22 Oct 2015

There is no known way to do this in R2014b, R2015a, or R2015b, other than to draw the contour plot yourself using other primitives such as patch()
In sufficiently old versions contourf created patch objects whose AlphaData or FaceAlpha could be adjusted. In the versions for a bit before R2014b, contour() and contourf() produced contourgroup objects which could not have their Alpha adjusted. The work-around in that era was to use contour3() which still generated patch objects, and to hack the ZData that was created to remove some NaN that prevented the faces from being filled. (I have a script somewhere that does this.)

  3 Comments

One thing to watch out for though is that in versions prior to 14b, contourf created lots of extra polygons. This means that when you set the Alpha on them, it was a bit random what color you actually got.
The reason for that was actually the same as the reason that you could mess with the alpha. That version of contourf was implemented using the Patch object. The Patch object does have alpha properties, but it doesn't provide a good way of representing polygons with holes, which are a requirement for doing filled contours well.
You can see what contourf is doing in pre-14b versions by adding some Z coordinates to the patches like this:
[~,h] = contourf(peaks);
p = findobj('Type','patch');
for i=1:length(p)
v = get(p(i),'Vertices');
v(:,3) = -i;
set(p(i),'Vertices',v)
end
view(3)
zlim([-length(p) -1])
If you set the alpha properties on those patches, you're going to see all of those blue ones in the back.
In 14b and later, contourf is no longer implemented using Patch. This means that it doesn't have to create all of those extra polygons. However, it also means that you can no longer do some of these tricks that depended on the implementation.
We are starting to go through and add alpha properties to more objects, not that we've fixed a bunch of these underlying problems. In 15b, we did area, bar, and scatter. We've certainly got contourf on the list, but I don't know which release it will be in.
What is the latest state on contourf, have you fixed the problems?
Unfortunately, at least up to R2016b, there is no Alpha property implemented for contour objects.

Sign in to comment.


Answer by Boris Belousov on 25 Feb 2016

As a possible workaround, you may manually define a contour of the area which you want to make transparent. Here is an example where a part of a contour plot is shaded using another contour plot.
% Axes
X = 1:8;
Y = 1:9;
% Contour plot with four levels and three contour lines
Z = [1 1 1 1 1 0 0 0;
1 1 1 1 1 0 0 0;
1 1 1 1 1 0 0 0;
1 1 1 1 1 1 0 0;
1 1 1 1 1 1 0 0;
2 2 1 1 1 1 1 0;
2 2 2 1 1 1 1 1;
3 2 2 2 2 2 1 1;
3 3 3 3 2 2 1 1];
% Top layer that shades part of the plot
Z_f = [0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 1;
0 0 0 0 0 0 1 1;
0 0 0 0 0 0 1 1;
0 0 0 0 0 0 1 1;
0 0 0 0 0 1 1 1;
0 0 0 0 1 1 1 1;
0 0 0 0 1 1 1 1;
0 0 1 1 1 1 1 1];
% Get contour for the top-layer patch and close the figure
C = contourf(Z_f,'LevelList',0.5); close all
% Close the contour by adding two extra points
x = [C(1,2:end), X(end), X(end)];
y = [C(2,2:end), Y(1), Y(end)];
% Contour plot
contourf(Z,'LevelList',[0 0.5 1.5 2.5]);
% Transparent patch
hold on
fill(x,y,'m','FaceAlpha',0.5);
hold off

  0 Comments

Sign in to comment.


Answer by Patricia Handmann on 1 Nov 2017

Is there some news for Matlab 2017a how to do this in a nice way?

  1 Comment

[~, hContour] = contourf(peaks(20), 10);
drawnow; % this is important, to ensure that FacePrims is ready in the next line!
hFills = hContour.FacePrims; % array of TriangleStrip objects
[hFills.ColorType] = deal('truecoloralpha'); % default = 'truecolor'
for idx = 1 : numel(hFills)
hFills(idx).ColorData(4) = 150; % default=255
end

Sign in to comment.