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

2 ビュー (過去 30 日間)
Frank King
Frank King 2015 年 5 月 12 日
コメント済み: Frank King 2015 年 5 月 15 日
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 件のコメント
Rohit Jain
Rohit Jain 2015 年 5 月 15 日
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.

サインインしてコメントする。

採用された回答

Rohit Jain
Rohit Jain 2015 年 5 月 15 日
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 件のコメント
Frank King
Frank King 2015 年 5 月 15 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by