Area plot with gradient
13 次查看(过去 30 天)
显示 更早的评论
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).
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!