smooth the surf or pcolor graph

I having a trouble in making my graph more clear and smooth in other words, make the locations with high value of Z be much more clear or distributed among the adjacent points soth that it can be obvious that at certain location we have high Z. a sample of my data is attached and the below is the code I'm using. the attached figure is what I'm really looking for to have. Thanks in advance
num = readtable("01)90.xlsx") ;
% Take all the data under each variable name
x=num{1:1:end, contains(num.Properties.VariableNames, 'x')};
y=num{1:1:end, contains(num.Properties.VariableNames, 'y')};
z=num{1:1:end, contains(num.Properties.VariableNames, 'v')};
% Convert the matrix to 1 column only (scalar)
x=x(:);
y=y(:);
z=z(:);
% Delete rows that contain NaNs with reference to the variable z
i=1; [m,n]=size(x);
while i<=m
if isnan(z(i,1))
x(i,:)=[];
y(i,:)=[];
z(i,:)=[];
i=i-1;
end
i=i+1; [m,n]=size(x);
end
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
xi = linspace(x0,x1,100) ;
yi = linspace(y0,y1,100) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
% Get boundary coordinates
idx = boundary(x,y) ;
xb = x(idx) ; yb = y(idx) ;
% Get points lying inside the boundary
idx = inpolygon(X,Y,xb,yb) ;
Z(~idx) = NaN ;
f=pcolor(X,Y,Z);
colorbar
shading interp
axis([-800 800 -800 800]);

 Risposta accettata

hello
looking first at your data distribution (histogram(Z)) I noticed 90% of the data is between 15 and 300 , very few samples are present above 300 so a linear colorbar was in my mind not the optimal choice
so the next idea was to plot the data with a log scaled colorbar . This is how the result looks now :
code is below :
clc
close all
clear all
num = readtable("01)90.xlsx") ;
% Take all the data under each variable name
x=num{1:1:end, contains(num.Properties.VariableNames, 'x')};
y=num{1:1:end, contains(num.Properties.VariableNames, 'y')};
z=num{1:1:end, contains(num.Properties.VariableNames, 'v')};
% Convert the matrix to 1 column only (scalar)
x=x(:);
y=y(:);
z=z(:);
% Delete rows that contain NaNs with reference to the variable z
i=1; [m,n]=size(x);
while i<=m
if isnan(z(i,1))
x(i,:)=[];
y(i,:)=[];
z(i,:)=[];
i=i-1;
end
i=i+1; [m,n]=size(x);
end
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
xi = linspace(x0,x1,100) ;
yi = linspace(y0,y1,100) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
% Get boundary coordinates
idx = boundary(x,y) ;
xb = x(idx) ; yb = y(idx) ;
% Get points lying inside the boundary
idx = inpolygon(X,Y,xb,yb) ;
Z(~idx) = NaN ;
f=pcolor(X,Y,log10(Z));
N = 32; % colorbar discrete values
cmap = colormap(jet(N)) ; %Create Colormap
cbh = colorbar ; %Create Colorbar
cbh.Ticks = linspace(min(log10(Z),[],'all'), max(log10(Z),[],'all'), N) ; %Create N ticks from min to max of Z array
tmp = round(logspace(log10(min(Z,[],'all')), log10(max(Z,[],'all')), N));
cbh.TickLabels = num2cell(tmp) ; %Replace the labels of these N ticks with the numbers defined in "tmp"
shading interp
axis([-800 800 -800 800]);

9 Commenti

Thank you very much for your response. However, the figure you have provided is still not exactly what I'm looking for. I agree with you regarding the data and I already tried to delete all the data above 300 and 400. I also don't know if the problem is in my data or I'm not following the correct codes.
I'm curious if I take the average of Z at each repeated x and y coordinates expecially if I take the average not only about the repeated coordinates but rather the average of Z that surrounds each x and y coordinates like a small circle. would you please help me with that? I believe this can make the high Z values be distributed somehow on the adjacent points.
Many thanks in advance
hello
regarding averaging - don't you think this will simply "distort" the representation of your data ? the "hot" areas will look larger but does it reflect the "real" thing ?
it will be used only as a representation that at this location we have hot area, also averaging doesn't change in the results it only reduces the fluctuations which exactly what I need.
Do you know how can I do this?
you help is highly appreciated.
this is the code with 2D smoothing
num = readtable("01)90.xlsx") ;
% Take all the data under each variable name
x=num{1:1:end, contains(num.Properties.VariableNames, 'x')};
y=num{1:1:end, contains(num.Properties.VariableNames, 'y')};
z=num{1:1:end, contains(num.Properties.VariableNames, 'v')};
% Convert the matrix to 1 column only (scalar)
x=x(:);
y=y(:);
z=z(:);
% Delete rows that contain NaNs with reference to the variable z
i=1; [m,n]=size(x);
while i<=m
if isnan(z(i,1))
x(i,:)=[];
y(i,:)=[];
z(i,:)=[];
i=i-1;
end
i=i+1; [m,n]=size(x);
end
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
xi = linspace(x0,x1,100) ;
yi = linspace(y0,y1,100) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
% Get boundary coordinates
idx = boundary(x,y) ;
xb = x(idx) ; yb = y(idx) ;
% Get points lying inside the boundary
idx = inpolygon(X,Y,xb,yb) ;
Z(~idx) = NaN ;
% 2D smoothing
Nr = 5;
Nc = 5;
Z = smooth2a(Z,Nr,Nc);% This function smooths the data using a mean filter over a
% rectangle of size (2*Nr+1)-by-(2*Nc+1).
f=pcolor(X,Y,log10(Z));
N = 16; % colorbar discrete values
cmap = colormap(jet(N)) ; %Create Colormap
cbh = colorbar ; %Create Colorbar
cbh.Ticks = linspace(min(log10(Z),[],'all'), max(log10(Z),[],'all'), N) ; %Create N ticks from min to max of Z array
tmp = round(logspace(log10(min(Z,[],'all')), log10(max(Z,[],'all')), N));
cbh.TickLabels = num2cell(tmp) ; %Replace the labels of these N ticks with the numbers defined in "tmp"
shading interp
axis([-800 800 -800 800]);
it uses the function smotth2a (from :smooth2a - File Exchange - MATLAB Central (mathworks.com) ) ( (a ttached)
smoothing will have an impact on the min / max amplitude, the more you smooth (average) , the more you reduce the amplitude , so a very hot local point will be transformed into a larger area but with significantly lower values
without smoothing : range goes from 15 to 850
with smoothing : range goes from 50 to 200
That was very helpful thanks a lot. I just have one question to further understand the codes.
Can you please explain to me what does the log scale affect on the representation?
the decision to plot in linear or log scale is either dictated by the physical laws (like for sound, the ear has a non linear response to sound amplitude so we use log scale to say that each increase of X dB (log amplitude) correspond to a similar perceived increase of sound intensity) or it can be simply because we want to have a colormap whith more steps in the low amplitude range , and less steps in the high amplitue range. The choice of a log is arbitrary, as you could choose another non linear function (power / polynamial function could also be used). So this is your decision if you stick to linear or non linear z scale mapping; also when your data has a very large factor between the min and max , a log scale is more appropriate to show the small amplitude values that would otherwise be unnoticed if displayed with a linear scale.
Just for fun, I get back to linear Z scale plotting but kept the smoothing;
the result looks fine too :
code :
num = readtable("01)90.xlsx") ;
% Take all the data under each variable name
x=num{1:1:end, contains(num.Properties.VariableNames, 'x')};
y=num{1:1:end, contains(num.Properties.VariableNames, 'y')};
z=num{1:1:end, contains(num.Properties.VariableNames, 'v')};
% Convert the matrix to 1 column only (scalar)
x=x(:);
y=y(:);
z=z(:);
% Delete rows that contain NaNs with reference to the variable z
i=1; [m,n]=size(x);
while i<=m
if isnan(z(i,1))
x(i,:)=[];
y(i,:)=[];
z(i,:)=[];
i=i-1;
end
i=i+1; [m,n]=size(x);
end
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
xi = linspace(x0,x1,100) ;
yi = linspace(y0,y1,100) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
% Get boundary coordinates
idx = boundary(x,y) ;
xb = x(idx) ; yb = y(idx) ;
% Get points lying inside the boundary
idx = inpolygon(X,Y,xb,yb) ;
Z(~idx) = NaN ;
% 2D smoothing
Nr = 5;
Nc = 5;
Z = smooth2a(Z,Nr,Nc);% This function smooths the data using a mean filter over a
% rectangle of size (2*Nr+1)-by-(2*Nc+1).
f=pcolor(X,Y,Z);
N = 16; % colorbar discrete values
cmap = colormap(jet(N)) ; %Create Colormap
colorbar
shading interp
axis([-800 800 -800 800]);
Thank you very much I highly really appreciate your responses.
You're welcome !

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by