Combine a contour plot with a simple 2D plot with same x axis

I have two datasets from which I can make a 2D density plot and a simple 2D plot.
For both plots, the x axis coordinates have the same range. Thus, I would like to merge the two plots into just one figure. What I want to obtain looks very similar to the figure below.
So far I have tried the following:
clear all;
close all;
clc
A = importdata("spectrum.dat"); %importing the data file
x = A(:,1); %extracting the values of the 1st column
y = A(:,2); %extracting the values of the 2nd column
z = A(:,3); %extracting the values of the 3rd column
B = importdata("qpwdata.dat");
a = B(:,1);
b = B(:,2);
figure(1)
plot(a,b,'LineWidth',3)
set(gca,'FontSize',15)
xlim([60 105])
ylim([0 1.2])
xticks([60 65 70 75 80 85 90 95 100 105])
yticks([0 0.2 0.4 0.6 0.8 1.0 1.2])
xlabel('\textbf{B(G)}', 'interpreter', 'latex', 'FontWeight','bold', 'FontSize', 20, 'FontName', 'Times New Roman');
ylabel('$\textbf{Z}$','interpreter', 'latex', 'FontName', 'Times New Roman','Fontsize',20);
tri = delaunay(x,y); %performing the Delaunay triangulation
figure(2)
trisurf(tri,x,y,z) %creating the surface plot
set(gca,'FontSize',15) %changing the values of the axis values -> make them bigger
xlim([60 105])
ylim([-5 5])
xticks([60 65 70 75 80 85 90 95 100 105])
yticks([-5 -4 -3 -2 -1 0 1 2 3 4 5])
xlabel('\textbf{B(G)}', 'interpreter', 'latex', 'FontWeight','bold', 'FontSize', 20, 'FontName', 'Times New Roman');
ylabel('$\textbf{En}^{-2/3}$','interpreter', 'latex', 'FontName', 'Times New Roman','Fontsize',20);
colormap jet
colorbar %inserting the colorbar
shading interp
view(2) %projecting the 3D surface into the 2D plane
what gives me the two plots separately.
I wonder how I can have both plots as in shown in the Figure above.
I have read that the function stackedplot does the job, but I have not understood how I would apply it in my problem.

 Réponse acceptée

Is it essential that there be zero space between the upper and lower plots? Why not just a very tight 2x1 tiledlayout?
t=tiledlayout(2,1,'TileSpacing','none');
nexttile;
plot(1:5);
xticks([]);
yticks(2:5);
nexttile
plot(rand(1,5))

3 commentaires

Indeed this should work. Unfortunately, I am running the version R2018b.
When I try what you suggested, it returns me the following error:
Undefined function or variable 'tiledlayout'.
Is there any way to make something similar using subplot? Or any idea on how to make it work with stackedplot?
I don't think stackedplot will let you mix a line plot with a surf plot. If you can't upgrade to a Matlab version with tiledlayout, this Filex Exchange submission is functionally very similar,
subaxis(2,1,1,'SpacingVert',0);
plot(1:5);
xticks([]);
yticks(2:5);
subaxis(2,1,2,'SpacingVert',0);
plot(rand(1,5))
Thanks. It worked perfectly!
I just wanted to ask you two more things, if possible : first, how do I plot a curve over the heatmap?
I have tried the following code (see below) but the dashed white line does not overlap completely the heat map as displayed in the figure of my original post (there it is a dashed red line).
Second, how do I "smooth" the curve displayed in the second plot (Z vs B) in a way I do not see the "imperfections"?
What I got instead out of my code attempt for the two remaining problems is:
clear all;
close all;
clc
A = importdata("spectrum.dat"); %importing the data file
x = A(:,1); %extracting the values of the 1st column
y = A(:,2); %extracting the values of the 2nd column
z = A(:,3); %extracting the values of the 3rd column
B = importdata("qpwdata.dat");
a = B(:,1);
b = B(:,2);
C = importdata("meanfield.dat");
e = C(:,1);
f = C(:,2);
b(b==0) = -1;
Lb = length (b);
for i = 1:Lb
if (b(i) > 0)
b(i-1) = 0;
j = i;
break
end
end
for i = j:Lb
if (b(i) == -1)
b(i) = 0;
j = i;
break
end
end
for i = j:Lb
if (b(i) > 0)
b(i-1) = 0;
j = i;
break
end
end
b(b==-1) = NaN;
tri = delaunay(x,y); %performing the Delaunay triangulation
s1 = subplot(2,1,1);
trisurf(tri,x,y,z) %creating the surface plot
set(gca,'FontSize',15) %changing the values of the axis values -> make them bigger
xlim([60 105])
ylim([-2 2])
xticks([])
yticks([-5 -4 -3 -2 -1 0 1 2 3 4 5])
%xlabel('\textbf{B(G)}', 'interpreter', 'latex', 'FontWeight','bold', 'FontSize', 20, 'FontName', 'Times New Roman');
ylabel('$\textbf{En}^{-2/3}$','interpreter', 'latex', 'FontName', 'Times New Roman','Fontsize',20);
colormap jet;
%colorbar; %inserting the colorbar
shading interp;
view(2);
hold on
plot(e,f,'--w','LineWidth',2);
hold off
figure(1)
s2 = subplot(2,1,2);
plot(a,b,'-b','LineWidth',3);
p1 = get(s1, 'Position');
p2 = get(s2, 'Position');
p1(2) = p2(2)+p2(4);
set(s1, 'pos', p1);
set(gca,'FontSize',15);
xlim([60 105]);
ylim([0 1.2]);
xticks([60 65 70 75 80 85 90 95 100 105]);
yticks([0 0.2 0.4 0.6 0.8 1.0]);
ytickformat('%.1f');
xlabel('\textbf{B(G)}', 'interpreter', 'latex', 'FontWeight','bold', 'FontSize', 20, 'FontName', 'Times New Roman');
ylabel('$\textbf{Z}$','interpreter', 'latex', 'FontName', 'Times New Roman','Fontsize',20);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by