Gradient fill of a function inside a circular area Matlab











up vote
2
down vote

favorite












I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best



enter image description here
I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.



The code I have is below



    clear t
N=10;
for i=0:N
t(i+1) = 0+(2*i*pi) / N;
end
F = exp(-cos(t))./(2.*pi*besseli(1,1));
figure(1)
subplot(1,3,1)
plot(t*180/pi,F,'-ob')
xlim([0 360])
xlabel('angle')
subplot(1,3,2)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
plot(cos(t).*F,sin(t).*F,'b','linewidth',2);
subplot(1,3,3)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');









share|improve this question






















  • As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
    – Adriaan
    Nov 7 at 9:13










  • @Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
    – jarhead
    Nov 7 at 9:44










  • What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using imagesc should work
    – Ander Biguri
    Nov 7 at 10:23






  • 1




    have you looked at the patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
    – Brice
    Nov 7 at 10:48










  • I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
    – Hoki
    Nov 7 at 10:56















up vote
2
down vote

favorite












I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best



enter image description here
I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.



The code I have is below



    clear t
N=10;
for i=0:N
t(i+1) = 0+(2*i*pi) / N;
end
F = exp(-cos(t))./(2.*pi*besseli(1,1));
figure(1)
subplot(1,3,1)
plot(t*180/pi,F,'-ob')
xlim([0 360])
xlabel('angle')
subplot(1,3,2)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
plot(cos(t).*F,sin(t).*F,'b','linewidth',2);
subplot(1,3,3)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');









share|improve this question






















  • As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
    – Adriaan
    Nov 7 at 9:13










  • @Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
    – jarhead
    Nov 7 at 9:44










  • What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using imagesc should work
    – Ander Biguri
    Nov 7 at 10:23






  • 1




    have you looked at the patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
    – Brice
    Nov 7 at 10:48










  • I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
    – Hoki
    Nov 7 at 10:56













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best



enter image description here
I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.



The code I have is below



    clear t
N=10;
for i=0:N
t(i+1) = 0+(2*i*pi) / N;
end
F = exp(-cos(t))./(2.*pi*besseli(1,1));
figure(1)
subplot(1,3,1)
plot(t*180/pi,F,'-ob')
xlim([0 360])
xlabel('angle')
subplot(1,3,2)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
plot(cos(t).*F,sin(t).*F,'b','linewidth',2);
subplot(1,3,3)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');









share|improve this question













I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best



enter image description here
I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.



The code I have is below



    clear t
N=10;
for i=0:N
t(i+1) = 0+(2*i*pi) / N;
end
F = exp(-cos(t))./(2.*pi*besseli(1,1));
figure(1)
subplot(1,3,1)
plot(t*180/pi,F,'-ob')
xlim([0 360])
xlabel('angle')
subplot(1,3,2)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
plot(cos(t).*F,sin(t).*F,'b','linewidth',2);
subplot(1,3,3)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');






matlab gradient






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 9:02









jarhead

60741334




60741334












  • As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
    – Adriaan
    Nov 7 at 9:13










  • @Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
    – jarhead
    Nov 7 at 9:44










  • What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using imagesc should work
    – Ander Biguri
    Nov 7 at 10:23






  • 1




    have you looked at the patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
    – Brice
    Nov 7 at 10:48










  • I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
    – Hoki
    Nov 7 at 10:56


















  • As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
    – Adriaan
    Nov 7 at 9:13










  • @Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
    – jarhead
    Nov 7 at 9:44










  • What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using imagesc should work
    – Ander Biguri
    Nov 7 at 10:23






  • 1




    have you looked at the patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
    – Brice
    Nov 7 at 10:48










  • I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
    – Hoki
    Nov 7 at 10:56
















As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13




As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13












@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44




@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44












What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using imagesc should work
– Ander Biguri
Nov 7 at 10:23




What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using imagesc should work
– Ander Biguri
Nov 7 at 10:23




1




1




have you looked at the patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
– Brice
Nov 7 at 10:48




have you looked at the patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
– Brice
Nov 7 at 10:48












I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56




I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










To fill surface, you need to use the patch command.



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap

figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar


Resulting graph:



Result



Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!



For example, the rotated version:



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)

figure
patch(x,y,c)
colorbar


enter image description here



To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.



For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.



For example:



% v controls the direction of the gradient
v = [0.1, 1];

t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));

% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];

% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off


enter image description here






share|improve this answer























  • this is great, but how can you rotate the gradient to a different angle?
    – jarhead
    Nov 7 at 15:28










  • for rotation, see my edits!
    – beesleep
    Nov 7 at 16:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186266%2fgradient-fill-of-a-function-inside-a-circular-area-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










To fill surface, you need to use the patch command.



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap

figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar


Resulting graph:



Result



Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!



For example, the rotated version:



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)

figure
patch(x,y,c)
colorbar


enter image description here



To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.



For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.



For example:



% v controls the direction of the gradient
v = [0.1, 1];

t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));

% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];

% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off


enter image description here






share|improve this answer























  • this is great, but how can you rotate the gradient to a different angle?
    – jarhead
    Nov 7 at 15:28










  • for rotation, see my edits!
    – beesleep
    Nov 7 at 16:50















up vote
2
down vote



accepted










To fill surface, you need to use the patch command.



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap

figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar


Resulting graph:



Result



Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!



For example, the rotated version:



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)

figure
patch(x,y,c)
colorbar


enter image description here



To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.



For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.



For example:



% v controls the direction of the gradient
v = [0.1, 1];

t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));

% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];

% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off


enter image description here






share|improve this answer























  • this is great, but how can you rotate the gradient to a different angle?
    – jarhead
    Nov 7 at 15:28










  • for rotation, see my edits!
    – beesleep
    Nov 7 at 16:50













up vote
2
down vote



accepted







up vote
2
down vote



accepted






To fill surface, you need to use the patch command.



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap

figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar


Resulting graph:



Result



Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!



For example, the rotated version:



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)

figure
patch(x,y,c)
colorbar


enter image description here



To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.



For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.



For example:



% v controls the direction of the gradient
v = [0.1, 1];

t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));

% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];

% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off


enter image description here






share|improve this answer














To fill surface, you need to use the patch command.



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap

figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar


Resulting graph:



Result



Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!



For example, the rotated version:



t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)

figure
patch(x,y,c)
colorbar


enter image description here



To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.



For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.



For example:



% v controls the direction of the gradient
v = [0.1, 1];

t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));

% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];

% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 15:55

























answered Nov 7 at 15:02









beesleep

559210




559210












  • this is great, but how can you rotate the gradient to a different angle?
    – jarhead
    Nov 7 at 15:28










  • for rotation, see my edits!
    – beesleep
    Nov 7 at 16:50


















  • this is great, but how can you rotate the gradient to a different angle?
    – jarhead
    Nov 7 at 15:28










  • for rotation, see my edits!
    – beesleep
    Nov 7 at 16:50
















this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28




this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28












for rotation, see my edits!
– beesleep
Nov 7 at 16:50




for rotation, see my edits!
– beesleep
Nov 7 at 16:50


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186266%2fgradient-fill-of-a-function-inside-a-circular-area-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings