Area plot with gradient

13 visualizaciones (últimos 30 días)
Theo
Theo el 15 de Feb. de 2011
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).

Respuesta aceptada

Patrick Kalita
Patrick Kalita el 16 de Feb. de 2011
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 comentario
Theo
Theo el 5 de Abr. de 2011
Perfect. Thank you for the wonderful answer.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by