Shaded Contour and Line Contour in one CONTOURF

I have a matrix A=m x n; X=m x 1; Y=n x 1; contourf(X,Y,A','linestyle','none')
I wish to superpose another matrix B (m*n) "in LINE CONTOUR" onto the previous
Please help...
It would be great if I knew to plot matrix B both in single color line with values written in between, or a contour with different color scheme....

2 commentaires

jonas
jonas le 4 Août 2018
I don't understand what you mean, what is a line contour? One without faces, like the normal contour?
Bhowmik.U
Bhowmik.U le 5 Août 2018
Please find the enclosed image as an example image I just downloaded from Internet to make my problem clear
Here the color is my Matrix : A (in shaded form) and by line contour, I mean the black lines within it, with lables in number.. (my Matrix B)
...Hope I am clear now

Connectez-vous pour commenter.

 Réponse acceptée

jonas
jonas le 5 Août 2018
Modifié(e) : jonas le 5 Août 2018
Yea that's fairly easy. An example:
A=peaks(100);
B=peaks(75);
[~,h1]=contourf(A,'linestyle','none');hold on
[c,h2]=contour(B,'linecolor',[0 0 0])
set(h2,'ContourZLevel',10)
clabel(c,h2)
The second to last row makes sure that h2 is always drawn on top, although this case its not needed.

7 commentaires

Bhowmik.U
Bhowmik.U le 20 Août 2018
Sir, the problem here is that it overrides the color scale of h1
secondlt how do I plot only certain levels of B on top of contour plot of A
jonas
jonas le 20 Août 2018
Modifié(e) : jonas le 20 Août 2018
What do you mean it overrides the color scale of h1? The contour h2 shows no color, although its colormap is the same as h1. If you want two different colormaps, then that's an easy fix. However, please clarify what you mean.
The second problem is easy, just specify the contourlines as input. For example, say you want to plot lines at Z=[1 5 10]:
[c,h2]=contour(B,'linecolor',[0 0 0],'levellist',[1 5 10])
Okay, I think I understand what you mean. You probably want the colorscale of the first plot to be completely unlinked to the colorscale of the second, so that they do not influence eachother in any way. This can be done by plotting them on different axes. Try this example:
%%Create and link two axes
ax1=axes
ax2=axes
linkaxes([ax1 ax2],'xy')
%%Some data
A=peaks(100);
B=peaks(75);
%%Toggle first axes and plot contour with facecolor
axes(ax1);hold on
[~,h1]=contourf(A,'linestyle','none');hold on
%colorbar(ax1)
%%Toggle second axes and plot contour lines
axes(ax2)
[c,h2]=contour(B.*100,'linecolor',[0 0 0],'facecolor','none')
%%Put second plot on top
set(h2,'ContourZLevel',10)
%%Add labels
clabel(c,h2)
%%some settings
set([ax1 ax2],'xlim',[0 100],'ylim',[0 100],'color','none')
Is this what you mean?
Bhowmik.U
Bhowmik.U le 20 Août 2018
Thanks a trillion Sir for all your help!..it worked like cream!
Bhowmik.U
Bhowmik.U le 20 Août 2018
however can you please suggest at which place I should use colorbar option
Bhowmik.U
Bhowmik.U le 20 Août 2018
because when I try inserting colorbar after
%colorbar(ax1)
the colorbar enters the plot area producing mess.. :(
jonas
jonas le 20 Août 2018
Modifié(e) : jonas le 20 Août 2018
Happy to help.
The problem is that the colorbar changes the position-property of one axes when you place it outside of the plot area, as it is set to location='eastoutside' by default. There are several ways to solve this:
1) Align the position of the two axes at the end of the script. Add this at the end of the script:
cb=colorbar(ax1)
ax1pos=get(ax1,'position')
set(ax2,'position',ax1pos)
2) add colorbar inside the plot and then move it outside by changing its position. This prevents the axes position from being changed at all. Add this at the end of the script:
cb=colorbar(ax1,'location','west')
cb.Position=cb.Position+[0.6 0 0 0];
3) add colorbar normally, i.e. outside of plot, and change the position of the axes back again afterwards
ax1pos=get(ax1,'position')
cb=colorbar(ax1)
set(ax1,'position',ax1pos)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by