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!