Area plot with gradient

13 次查看(过去 30 天)
Theo
Theo 2011-2-15
Suppose that I have a function f(x). You can think of f(x) as strictly positive if it helps.
Now, there is a secondary function, say M(x), which, for a given x gives a number.
I'd like to create a plot of f(x), except with the column from y = 0 to y = f(x) coloured with a colour determined from M(x). Moreover, I'd like the colours in the horizontal direction to be blended together (as in a gradient).

采纳的回答

Patrick Kalita
Patrick Kalita 2011-2-16
This is the kind of custom graphic that you can build yourself with patches -- or a single patch object in this case. Here is an example:
% Define x, f(x), and M(x)
x = linspace(0, 2*pi, 20)';
f = cos(x) + 2;
M = x.^2;
% Define the vertices: the points at (x, f(x)) and (x, 0)
N = length(x);
verts = [x(:), f(:); x(:) zeros(N,1)];
% Define the faces to connect each adjacent f(x) and the corresponding points at y = 0.
q = (1:N-1)';
faces = [q, q+1, q+N+1, q+N];
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
If you're not familiar with using patches, this may be a lot to absorb at once. But if you read through this section of the patch documentation it will hopefully start to make sense.
  1 个评论
Theo
Theo 2011-4-5
Perfect. Thank you for the wonderful answer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by