Set background transparent for coutourf plot, versions 2014b and later

2 Ansichten (letzte 30 Tage)
I'm attempting to plot two 'contourf()' plots side by side with different color maps. I had done this quite successfully using the 'set(myAxes(2), 'Visible','off');'. However, the addition of 'xlabel' and 'ylabel' to one but not both set of axes moves the bounding boxes of the plot and they no longer match up. So instead I need to plot the second 'contourf()' not with no axes or background, but with a transparent one.
Previous to 2014b solutions like this one could be used, by directly accessing the children of the 'countourf()' plot. Since the change in 2014b to a different set of return variables for 'countourf()' this solution no longer works, and the children of the countour plot it's self can't be accessed.
I have found that for '[C,h] = countourf();' you can access h.FacePrims and their .ColorType and .ColorData in order to set the alpha of the contours themselves to be transparent, but I have been unable to discover a similar way to access the background of the plot to set it to be transparent.
For reference, my code is below. It is the second 'countourf()' whose background I'd like to be transparent.
figure('units','normalized');
ax(1) = axes;
[C1,h1] = contourf(Xgrid,Zgrid,foc2psIonDenCut,color.contVec);
set(h1,'LineStyle','None');
colormap(ax(1),color.cmapBR);
caxis(ax(1),[color.contMin color.contMax]);
xlim(ax(1),[-2 4]);
ylim(ax(1),[-52 52]);
set(ax(1),'YTick',[-50:25:50]);
xlabel('Z Dimension [\mum]');
ylabel('X Dimension [\mum]');
% Background Rectangle
rectangle('Position',[-2 -52 6 52],'FaceColor',[0.9 0.9 0.9],'LineStyle','none');
ax(2) = axes;
[C2,h2] = contourf(Xgrid,Zgrid,def2psIonDenCut,color.contVec);
set(h2,'LineStyle','None');
colormap(ax(2),color.cmapPK);
caxis(ax(2),[color.contMin color.contMax]);
xlim(ax(2),[-2 4]);
ylim(ax(2),[-52 52]);
set(ax(2),'YTick',[-50:25:50]);
xlabel(ax(2),'Z Dimension [\mum]');
ylabel(ax(2),'X Dimension [\mum]');
%set(ax(2), 'Visible','off'); %This is where in previous versions I'd turn the second axes off.
  1 Kommentar
Rohit Jain
Rohit Jain am 15 Mai 2015
I think the possibility to create a transparent contour plot is not available in MATLAB R2014b.
However you can use contourc to get the ContourMatrix which you use to create patches (where you can control the transparency) to recreate the contour plot. Please note that this workaround does not preserve the stacking order of contours that are within other contours with a higher level; i.e., the stacking order of the contours is entirely dependent on the contour level.I have attached a file 'demo.m' for an example.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rohit Jain
Rohit Jain am 15 Mai 2015
I think the possibility to create a transparent contour plot is not available in MATLAB R2014b.
However you can use contourc to get the ContourMatrix which you use to create patches (where you can control the transparency) to recreate the contour plot. Please note that this workaround does not preserve the stacking order of contours that are within other contours with a higher level; i.e., the stacking order of the contours is entirely dependent on the contour level.I have attached a file 'demo.m' for an example.
  1 Kommentar
Frank King
Frank King am 15 Mai 2015
This sounds like a reasonable solution, so I've accepted it. However, I have NOT tested it.
Instead I gave up the ghost and made two images and edited them together in Photoshop. It took literally two minutes. I'd have liked to have an all Matlab fix to the problem, but apparently to do that is quite complex.
Nevertheless, I thank you very much for your help and for spending time to write some code to help me out.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by