Fill area between contour levels

51 Ansichten (letzte 30 Tage)
Alvise Giora
Alvise Giora am 21 Feb. 2020
Hi everyone!
I have a simple contourplot with some levels. I want to fill with a solid color (grey) the area between two levels (0 and 4).
Thanks in advance
Here's my code:
close all;
clear;
clc;
dati=xlsread('../isoFAMTEMmono.xlsx');
%x data
luce = dati(2:14,1);
%y data
sp_arco = dati(1,2:7);
%z data
famTEM1_1 = dati( 2:14, 2:7 );
figure;
hold on
[C1,h1]=contour(sp_arco,luce,famTEM1_1,'k','LevelList',0:2:20,'ShowText','on','LineWidth',1.2);
[C2,h2]=contour(sp_arco,luce,famTEM1_1,'k','LevelList',20:5:50,'ShowText','on','LineWidth',1.2);
[C3,h3]=contour(sp_arco,luce,famTEM1_1,'k','LevelList',50:10:100,'ShowText','on','LineWidth',1.2);
xlabel('Spessore Arco');
ylabel('Luce');
title(Titolo1A,'FontSize',10)
clabel(C1,h1,'LabelSpacing',500);
clabel(C2,h2,'LabelSpacing',500);
clabel(C3,h3,'LabelSpacing',500);
xlim([0.3,1.5]);
ylim([3,15]);
xticks(0.3:0.1:1.5);
yticks(0:1:15);
hold off
  4 Kommentare
Alvise Giora
Alvise Giora am 24 Feb. 2020
Bearbeitet: Alvise Giora am 24 Feb. 2020
close all;
clear;
clc;
dati=xlsread('../isoFAMTEMmono.xlsx');
%x data
luce = dati(2:14,1);
%y data
sp_arco = dati(1,2:7);
%z data
famTEM1_1 = dati( 2:14, 2:7 );
cmap=[0.8 0.8 0.8
1 1 1];
figure;
hold on
[C1,h1]=contourf(sp_arco,luce,famTEM1_1,'k','LevelList',0:2:20,'ShowText','on','LineWidth',1.2);
[C2,h2]=contourf(sp_arco,luce,famTEM1_1,'k','LevelList',20:5:50,'ShowText','on','LineWidth',1.2);
[C3,h3]=contourf(sp_arco,luce,famTEM1_1,'k','LevelList',50:10:100,'ShowText','on','LineWidth',1.2);
xlabel('Spessore Arco');
ylabel('Luce');
title(Titolo1A,'FontSize',10)
clabel(C1,h1,'LabelSpacing',500);
clabel(C2,h2,'LabelSpacing',500);
clabel(C3,h3,'LabelSpacing',500);
colormap(cmap);
caxis([0 8]);
xlim([0.3,1.5]);
ylim([3,15]);
xticks(0.3:0.1:1.5);
yticks(0:1:15);
hold off
darova
darova am 24 Feb. 2020
smart!

Melden Sie sich an, um zu kommentieren.

Antworten (2)

the cyclist
the cyclist am 21 Feb. 2020
Bearbeitet: the cyclist am 21 Feb. 2020
I am hoping for your sake that there is a better way than this (possibly using the Image Processing Toolbox?), but here is one way.
% Create the figure. Turn on "hold", to superpose the patch afterward
figure
hold on
% Create a simple contour plot, and output the contour information
M = contour(peaks(3));
% Extract the level information for two of the contours.
% This step is manual and annoying. It is *possible* to automate this,
% but I think it would be pretty intricate. Not worth the effort if you
% only need to do this once.
% See documentation of contour output M, for how the info is stored.
% (https://www.mathworks.com/help/matlab/ref/contour.html#mw_27cd6c94-d861-4e0a-837c-0a19f2574186)
level1 = M(:,26:30);
level2 = M(:,32:36);
% Combine the x and y coordinates of the two levels.
x = [level1(1,:) level2(1,:)];
y = [level1(2,:) level2(2,:)];
% Define a light gray color
gray = [0.8 0.8 0.8];
% Fill the patch
fill(x,y,gray)
% Cover up the line that connects the final vertex back to the initial one,
% using the same color
line([x(1) x(end)],[y(1) y(end)],'Color',gray)
  2 Kommentare
Alvise Giora
Alvise Giora am 24 Feb. 2020
Thank you for the response. I've understand what you did. Unfortunately i have a lot of plots and the levels aren't constant.
I have tried also to use contourf and custom colormaps. Is there a way to define specific colors for defined levels?
Maleen Wijeratna Kidiwela
Maleen Wijeratna Kidiwela am 8 Feb. 2021
issue i have with this is selecting levels i want from
level1 = M(:,26:30);
level2 = M(:,32:36);

Melden Sie sich an, um zu kommentieren.


darova
darova am 24 Feb. 2020
Here is an idea:
  • plot all your contours
  • fill external contour you want
[~,h1] = contourf(X,Y,Z,[1 1],'edgecolor','none');
h11 = get(h1,'children');
set(h11,'FaceColor',[1 1 1]*0.8)
  • fill interior contour with white color
[~,h2] = contourf(X,Y,Z,[3 3],'edgecolor','k');
h22 = get(h2,'children');
set(h22,'FaceColor',[1 1 1])
QUESTION:
Don't know why some contours lose their colors after i tried to change Z coordinate (because of mixing colors)
set(h22,'ZData',Z*0+0.1)
Script i used
[X,Y,Z] = peaks(20);
% surf(X,Y,Z,'facecolor','none','edgeColor',[1 1 1]*0.7)
% hold on
[~,h] = contour(X,Y,Z,-5:2:5);
set(h,'linewidth',2);
% hold off
hold on
[~,h1] = contourf(X,Y,Z,[1 1],'edgecolor','none');
[~,h2] = contourf(X,Y,Z,[3 3],'edgecolor','k');
hold off
h11 = get(h1,'children');
h22 = get(h2,'children');
set(h11,'FaceColor',[1 1 1]*0.8)
set(h22,'FaceColor',[1 1 1])
set(h22,'ZData',Z*0+0.1)
view(3)
box off
grid on
zlim([-1 1])

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by