How can I fill an area below a 3D graph in MATLAB?
Asked Answered
C

1

7

I created the following 3d plot in MATLAB using the function plot3:

enter image description here

Now, I want to get a hatched area below the "2d sub-graphs" (i.e. below the blue and red curves). Unfortunately, I don't have any idea how to realize that.

I would appreciate it very much if somebody had an idea.

Contracture answered 4/4, 2017 at 21:14 Comment(0)
E
8

You can do this using the function fill3 and referencing this answer for the 2D case to see how you have to add points on the ends of your data vectors to "close" your filled polygons. Although creating a pattern (i.e. hatching) is difficult if not impossible, an alternative is to simply adjust the alpha transparency of the filled patch. Here's a simple example for just one patch:

x = 1:10;
y = rand(1, 10);
hFill = fill3(zeros(1, 12), x([1 1:end end]), [0 y 0], 'b', 'FaceAlpha', 0.5);
grid on

And here's the plot this makes:

enter image description here

You can also create multiple patches in one call to fill3. Here's an example with 4 sets of data:

nPoints = 10;  % Number of data points
nPlots = 4;    % Number of curves
data = rand(nPoints, nPlots);  % Sample data, one curve per column

% Create data matrices:
[X, Y] = meshgrid(0:(nPlots-1), [1 1:nPoints nPoints]);
Z = [zeros(1, nPlots); data; zeros(1, nPlots)];
patchColor = [0 0.4470 0.7410];  % RGB color for patch edge and face

% Plot patches:
hFill = fill3(X, Y, Z, patchColor, 'LineWidth', 1, 'EdgeColor', patchColor, ...
              'FaceAlpha', 0.5);
set(gca, 'YDir', 'reverse', 'YLim', [1 nPoints]);
grid on

And here's the plot this makes:

enter image description here

Embarkment answered 4/4, 2017 at 21:47 Comment(1)
Thank you very much! That helps me a lot!Contracture

© 2022 - 2024 — McMap. All rights reserved.