Plot 2D surface with z axis as colored bar

4 views (last 30 days)
Hi all,
I'm trying to write a code to plot the attached figure. Basically, my X values will vary between 2.99 and 3.23 with corresponding Z values varied from 3.3 to 3.7, while my Y values is not important here. My main idea is to present the variation in Z values as a colored map. I am wondering if anyone has an idea on how to draw such plot.

Answers (1)

Star Strider
Star Strider on 5 Sep 2019
Use the contourf function with 'LineStyle','none'.
Example —
M = peaks(20); % Data For Plot
figure
[c,h] = contourf(M);
h.LineStyle = 'none';
or:
figure
subplot(2,1,1)
[c,h] = contourf(M);
h.LineStyle = 'none';
Ax1 = gca;
sp1 = Ax1.Position;
Ax1.Position = sp1 + [0 0 -0.1 0];
subplot(2,1,2)
[c,h] = contourf(M);
h.LineStyle = 'none';
Ax2 = gca;
sp2 = Ax2.Position;
Ax2.Position = sp2 + [0 0 -0.1 0];
hc2 = colorbar;
hcp2 = hc2.Position;
hc2.Position = hcp2 + [0.15 0 0 hcp2(4)*1.4]
  2 Comments
Talal Salem
Talal Salem on 5 Sep 2019
I already tried to use the contourf command but it didn't work!
clc
clear
close all
x = 2.99:0.01:3.23;
y = 0:0.06:1.45;
z=3.3:0.0165:3.7;
[xx, yy] = meshgrid(x, y);
zz = griddata(x, y, z, xx, yy);
figure(1)
contourf(xx,yy,zz,'EdgeColor', 'none')
colormap(jet)
xlim([2.99 3.23])
ylim([0 1.5])
colorbar
grid off;
Star Strider
Star Strider on 5 Sep 2019
You need to use a different interpolation method:
zz = griddata(x, y, z, xx, yy, 'v4');
The 'nearest' method will also work.
If you want to use any other method, you need to set the NaN values (that comprise all except the diagonal of ‘zz’ with all the other methods) to some definite numerical value, for example:
zz = griddata(x, y, z, xx, yy);
zz(isnan(zz)) = 0;
Experiment to get the result you want.

Sign in to comment.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!