MATLAB Answers

Plotting contourf with discrete x axis

5 views (last 30 days)
G E on 28 Feb 2020
Edited: G E on 29 Feb 2020
I am struggling to plot some data using contourf, and would be extremely grateful for any advice.
My data ("Temp", 10000x10) contains the temperature in 10 vertical "zones" (zone 10 = bottom, hot part, zone 1 = top, cooler part) of a blast furnace, modelled over 10,000 seconds.
I would like to do a contour plot, showing how the temperature changes in all zones vs. time. Ideally, I would get the zone number on the y axis (discrete), the time on the x axis, and the temperature as the colour.
When I plot:
I get the following:
MATLAB seems to be doing some sort of interpolation along each "zone" - how can I remove this? The zone (x axis) should be discrete - there is no zone 5.5, just zone 5 and zone 6.
I also can't get the axes to swap despite my best attempts!
Would be extremely grateful for any advice :)

Accepted Answer

Kelly Kearney
Kelly Kearney on 28 Feb 2020
The contour function always assumes your input data are points on a surface that can be linearly interpolated between. To get a plot with discrete blocks, you have a few options.
1) Plot using imagesc or pcolor
2) Upsample the data before plotting.
Also, to swap axes, you need to provide x and y inputs or transpose your matrix.
% Data
x1 = 1:10;
y1 = 1:20;
[x3,y3] = meshgrid(x1,y1);
Temp = peaks(20); % Some sample data
Temp = Temp(:,1:10);
% Upsampled data
x2 = linspace(0,10.5,100);
Temp2 = interp1(1:10,Temp',x2,'nearest')';
% Plot options
ax(1) = subplot(2,2,1);
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
set(gca, 'ydir', 'normal');
ax(2) = subplot(2,2,2);
shading flat;
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
set(gca, 'ydir', 'normal');
ax(3) = subplot(2,2,3);
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
ax(4) = subplot(2,2,4);
contourf(x2, y1, Temp2,50);
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
set(ax, 'clim', [-5 7], 'xlim', [0.5 10.5], 'ylim', [0.5 20.5]);
  1 Comment
G E on 29 Feb 2020
Amazing - thank you very much for the quick reply and detailed explanation!
imagesc exactly what I needed - very happy with it :))

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!