Plotting contourf with discrete x axis

조회 수: 6 (최근 30일)
G E
G E 2020년 2월 28일
편집: G E 2020년 2월 29일
Hello,
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:
contourf(Temp,50)
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 :)

채택된 답변

Kelly Kearney
Kelly Kearney 2020년 2월 28일
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);
imagesc(x1,y1,Temp);
title('imagesc(Temp)');
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
set(gca, 'ydir', 'normal');
ax(2) = subplot(2,2,2);
pcolor(x1,y1,Temp);
shading flat;
title('pcolor(Temp)');
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
set(gca, 'ydir', 'normal');
ax(3) = subplot(2,2,3);
contourf(x1,y1,Temp,50);
title('contourf(Temp)');
hold on;
scatter(x3(:), y3(:), [], Temp(:), 'filled', 'markeredgecolor', 'k');
ax(4) = subplot(2,2,4);
contourf(x2, y1, Temp2,50);
title('contourf(Temp2)');
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
G E
G E 2020년 2월 29일
편집: G E 2020년 2월 29일
Amazing - thank you very much for the quick reply and detailed explanation!
imagesc exactly what I needed - very happy with it :))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by