How do I get smooth edges for a contourf plot on a log-log scale scatterplot?

Hello,
I am having some trouble with using the contourf function on a log-log scale plot. I have 9 datapoints in a 2D scatterplot that are colored for a third variable. The code I use to plot the data as well as the plot are included below.
a = reshape(mtot_1,1,[]); % convert matrix to row vector
b = reshape(MFR_1,1,[]); % convert matrix to row vector
c = reshape(SN_maxes_1,1,[]); % convert matrix to row vector
figure(4)
clf
hold on
scatter(b, a, [], c, 'filled')
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
xlabel('MFR')
ylabel('total mass flow')
As you can see, on a log-log scale, the datapoits form a sort of "skewed quadrilateral" shape with edges that look "straight" when plotted on log-log. I want to create a contour map from these 9 points, but when I do, it looks like the plot below because the contour is generated with a linear interpolation method that creates straight lines between the points on a normal linear axis scale, which then look distorted or curved when plotted on the log-log scale. I have also included the code I use.
figure(5)
clf
hold on
contourf(MFR_1, mtot_1, SN_maxes_1, 100, 'LineStyle', 'none')
scatter(b, a, [], c, 'filled')
set(gca,'xscale','log')
set(gca,'yscale','log')
d = colorbar;
d.Label.String = "Swirl No.";
xlabel('MFR')
ylabel('total mass flow')
I would like to make it so that the contour plot has "straight" edges between the outer datapoints when plotted on the log-log scale, so that the contour map essentially appears as a quadrilateral with straight sides on the log-log plot instead of the odd curvy shape in the contour plot above. Can someone please offer me some advice as to how to achieve this? Thanks in advance!

9 Comments

hello
my 2 cents :
  • transform your data points in log magnitude
  • then do the mesh (interpolation)
  • backward conversion of the mesh data in linear
  • plot in log axis as you already have done
et voilà ....
Thanks for your response! Just a quick question:
When you say transform the datapoints, do you mean transform the x- and y-coordinates? (MFR and total mass flow in my plot) Or also transform the colored variable?
Also, how would I perform a backward conversion?
If you could provide a few lines of code to show what you mean that would help a ton! Thanks!
I'll be glad to help you, but I need some data first !
Great, thanks! Here is the data I am using to plot:
First is the x-axis data (it is a matrix):
MFR_1 = [0.93016, 0.13933, 0.04154; 4.75072, 0.96454, 0.27638; 16.1767, 3.35929, 1.03684]
Then the y-axis data (also a matrix):
mtot_1 = [0.00087393, 0.001293, 0.00161739; 0.00146412, 0.00182395, 0.00211802; 0.00195069, 0.00228598, 0.002528465]
Then the "z" data (if you would call it that). This is what determines the color of the dots. It is also a matrix:
SN_maxes_1 = [1.678801, 1.627564, 1.521288; 1.535838, 1.848008, 1.7666569; 1.419559, 1.818278, 1.963394]
I am currently plotting the contour using the contourf function with this line of code (you can see it in the code in the original post):
contourf(MFR_1, mtot_1, SN_maxes_1, 100, 'LineStyle', 'none')
To plot just the points (no contour) I use the scatter function. This function requires that the inputs be a vector, so I use the following lines of code to change the matrices to vectors and plot them:
a = reshape(MFR_1,1,[]); % convert matrix to row vector
b = reshape(mtot_1,1,[]); % convert matrix to row vector
c = reshape(SN_maxes_1,1,[]); % convert matrix to row vector
scatter(a, b, [], c, 'filled')
To set the axis scale to log-log, I use these lines of code:
set(gca,'xscale','log')
set(gca,'yscale','log')
That should just about cover everything. Let me know if there is any other information I could provide that would help. Thanks again in advance for your help!
@Mathieu NOE Were you ever able to find a solution? The solution below is close but not quite what I need
hello
thanks for your nice comment
I am gonna move my suggestion to the answer section so maybe you will kindly accept it !

Sign in to comment.

 Accepted Answer

hello
I can suggest this , although I consider it needs some refinements
MFR_1 = [0.93016, 0.13933, 0.04154; 4.75072, 0.96454, 0.27638; 16.1767, 3.35929, 1.03684];
%Then the y-axis data (also a matrix):
mtot_1 = [0.00087393, 0.001293, 0.00161739; 0.00146412, 0.00182395, 0.00211802; 0.00195069, 0.00228598, 0.002528465];
%Then the "z" data (if you would call it that). This is what determines the color of the dots. It is also a matrix:
SN_maxes_1 = [1.678801, 1.627564, 1.521288; 1.535838, 1.848008, 1.7666569; 1.419559, 1.818278, 1.963394];
a = reshape(mtot_1,1,[]); % convert matrix to row vector
b = reshape(MFR_1,1,[]); % convert matrix to row vector
c = reshape(SN_maxes_1,1,[]); % convert matrix to row vector
figure(4)
clf
hold on
scatter(b, a, [], c, 'filled')
% create a mesh with constant log spacing , and find points that are inside
% a polygon (convex hull)
bl = log10(b');
al = log10(a');
cl = log10(c');
k = boundary(bl,al,1); % define outer hull
xv = bl(k);
yv = al(k);
% plot(10.^xv, 10.^yv, '-r')
x = linspace(min(xv),max(xv),200);
y = linspace(min(yv),max(yv),200);
[X,Y] = meshgrid(x,y);
x = X(:);
y = Y(:);
in = inpolygon(x,y,xv,yv);
xin = 10.^(X(in));
yin = 10.^(Y(in));
vq = griddata(bl,al,c,log10(xin),log10(yin));
scatter(xin, yin, [], vq, 'filled')
% plot(xin,yin,'.r')
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
xlabel('MFR')
ylabel('total mass flow')
hold off

4 Comments

I have a follow-up question about the plot above. I am trying to make the same type of plot, but with different values on the y-axis. The values for the x-axis and colorbar are the same. I use the following code:
figure(4)
clf
hold on
a = reshape(mtot_1,1,[]); % convert matrix to row vector
a2 = reshape(MF_tot_1,1,[]); % convert matrix to row vector
b = reshape(MFR_1,1,[]); % convert matrix to row vector
c = reshape(SN_maxes_1,1,[]); % convert matrix to row vector
%THIS PLOT IS THE SAME AS THE ONE WE DISCUSSED BEFORE
subplot(1,2,1)
% CONTOUR PLOTTING -----------------------------------------------------
bl = log10(b');
al = log10(a');
cl = log10(c');
k = boundary(bl,al,1); % define outer hull
xv = bl(k);
yv = al(k);
% plot(10.^xv, 10.^yv, '-r')
x = linspace(min(xv),max(xv),1000);
y = linspace(min(yv),max(yv),1000);
[X,Y] = meshgrid(x,y);
x = X(:);
y = Y(:);
in = inpolygon(x,y,xv,yv);
xin = 10.^(X(in));
yin = 10.^(Y(in));
vq = griddata(bl,al,c,log10(xin),log10(yin));
scatter(xin, yin, [], vq, 'filled')
%----------------------------------------------------------------------
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
d = colorbar;
d.Label.String = "Swirl No.";
xlabel('MFR')
ylabel('total mass flow')
box on
grid on
%THIS IS THE NEW PLOT (THAT I HAVE QUESTIONS ABOUT)
subplot(1,2,2)
% CONTOUR PLOTTING -----------------------------------------------------
bl = log10(b');
al = log10(a2'); %USING a2 INSTEAD OF a FOR THE Y-AXIS VALUES
cl = log10(c');
k = boundary(bl,al,1); % define outer hull
xv = bl(k);
yv = al(k);
% plot(10.^xv, 10.^yv, '-r')
x = linspace(min(xv),max(xv),1000);
y = linspace(min(yv),max(yv),1000);
[X,Y] = meshgrid(x,y);
x = X(:);
y = Y(:);
in = inpolygon(x,y,xv,yv);
xin = 10.^(X(in));
yin = 10.^(Y(in));
vq = griddata(bl,al,c,log10(xin),log10(yin));
scatter(xin, yin, [], vq, 'filled')
%----------------------------------------------------------------------
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
d = colorbar;
d.Label.String = "Swirl No.";
xlabel('MFR')
ylabel('total momentum flux')
box on
grid on
set(gcf,'color','w');
sgtitle('Contours of Swirl No.')
This code produces the following plots:
The question I have is this: How can I make it so that the plot on the right has a complete contour (so that it isn't missing that triagular chunk of the contour in the bottom left). The contour on the left is the one we discussed earlier, and is perfect. I just want the one on the right to match it in terms of having a complete contour.
As I said, the data are all the same except for the y-axis in the second plot. The values for MF_tot_1 (which are used for the y-axis values in the second plot) are as follows:
Thanks in advance for your help.
hello
you simply have to tune the threshold value in boundary to get the correct resut
so for the second plot , threshold must be reduced from 1 to 0.5
this line :
k = boundary(bl,al,1); % define outer hull
is now :
k = boundary(bl,al,0.5); % define outer hull
full code :
MFR_1 = [0.93016, 0.13933, 0.04154; 4.75072, 0.96454, 0.27638; 16.1767, 3.35929, 1.03684];
%Then the y-axis data (also a matrix):
mtot_1 = [0.00087393, 0.001293, 0.00161739; 0.00146412, 0.00182395, 0.00211802; 0.00195069, 0.00228598, 0.002528465];
%Then the "z" data (if you would call it that). This is what determines the color of the dots. It is also a matrix:
SN_maxes_1 = [1.678801, 1.627564, 1.521288; 1.535838, 1.848008, 1.7666569; 1.419559, 1.818278, 1.963394];
MF_tot_1 = [0.001616265021936 0.005243033092750 0.015546133692972;
0.005271449006048 0.007910496071212 0.017789070120675;
0.014974604975415 0.016927091212513 0.025444016339618];
a = reshape(mtot_1,1,[]); % convert matrix to row vector
a2 = reshape(MF_tot_1,1,[]); % convert matrix to row vector
b = reshape(MFR_1,1,[]); % convert matrix to row vector
c = reshape(SN_maxes_1,1,[]); % convert matrix to row vector
%THIS PLOT IS THE SAME AS THE ONE WE DISCUSSED BEFORE
subplot(1,2,1)
% CONTOUR PLOTTING -----------------------------------------------------
bl = log10(b');
al = log10(a');
cl = log10(c');
k = boundary(bl,al,1); % define outer hull
xv = bl(k);
yv = al(k);
x = linspace(min(xv),max(xv),1000);
y = linspace(min(yv),max(yv),1000);
[X,Y] = meshgrid(x,y);
x = X(:);
y = Y(:);
in = inpolygon(x,y,xv,yv);
xin = 10.^(X(in));
yin = 10.^(Y(in));
vq = griddata(bl,al,c,log10(xin),log10(yin));
scatter(xin, yin, [], vq, 'filled')
%----------------------------------------------------------------------
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
d = colorbar;
d.Label.String = "Swirl No.";
xlabel('MFR')
ylabel('total mass flow')
box on
grid on
%THIS IS THE NEW PLOT (THAT I HAVE QUESTIONS ABOUT)
subplot(1,2,2)
% CONTOUR PLOTTING -----------------------------------------------------
bl = log10(b');
al = log10(a2'); %USING a2 INSTEAD OF a FOR THE Y-AXIS VALUES
cl = log10(c');
k = boundary(bl,al,0.5); % define outer hull
xv = bl(k);
yv = al(k);
x = linspace(min(xv),max(xv),1000);
y = linspace(min(yv),max(yv),1000);
[X,Y] = meshgrid(x,y);
x = X(:);
y = Y(:);
in = inpolygon(x,y,xv,yv);
xin = 10.^(X(in));
yin = 10.^(Y(in));
vq = griddata(bl,al,c,log10(xin),log10(yin));
scatter(xin, yin, [], vq, 'filled')
%----------------------------------------------------------------------
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
d = colorbar;
d.Label.String = "Swirl No.";
xlabel('MFR')
ylabel('total momentum flux')
box on
grid on
set(gcf,'color','w');
sgtitle('Contours of Swirl No.')

Sign in to comment.

More Answers (1)

Here is how you can get the straight lines along edges:
MFR_1 = [0.93016, 0.13933, 0.04154; 4.75072, 0.96454, 0.27638; 16.1767, 3.35929, 1.03684];
%Then the y-axis data (also a matrix):
mtot_1 = [0.00087393, 0.001293, 0.00161739; 0.00146412, 0.00182395, 0.00211802; 0.00195069, 0.00228598, 0.002528465];
%Then the "z" data (if you would call it that). This is what determines the color of the dots. It is also a matrix:
SN_maxes_1 = [1.678801, 1.627564, 1.521288; 1.535838, 1.848008, 1.7666569; 1.419559, 1.818278, 1.963394];
a = reshape(mtot_1,1,[]); % convert matrix to row vector
b = reshape(MFR_1,1,[]); % convert matrix to row vector
c = reshape(SN_maxes_1,1,[]); % convert matrix to row vector
figure(4)
clf
hold on
scatter(b, a, [], c, 'filled')
set(gca,'xscale','log')
set(gca,'yscale','log')
colorbar
xlabel('MFR')
ylabel('total mass flow')
figure(5)
clf
hold on
contourf(log(MFR_1), log(mtot_1), log(SN_maxes_1), 100, 'LineStyle', 'none')
scatter(log(b), log(a), [], log(c), 'filled')
set(gca,'xscale','linear')
set(gca,'yscale','linear')
d = colorbar;
d.Label.String = "Swirl No.";
xlabel('MFR')
ylabel('total mass flow')

1 Comment

@Sulaymon Eshkabilov This is very close to what I want, but I need the values on the axes to actually match the data. The axis values on your plot show the log values of the datapoints, but I need the original values. In other words, I need the points to be the same as in the contour plot I provided, but just with straight edges to the contour. The axes should also have a log scale

Sign in to comment.

Categories

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!