Shading area between 2 curves (x function in terms of y)

Hello, I'm trying to shade an area between two functions (x depending on y), I shade the correct area but I get a reflection of that shaded region possibly about the line y=x. I don't want this reflection in my graph, I only want the shaded region. Here's my code:
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g'); hold off;

 Accepted Answer

Here are the plots of the functions. The independent variable is y, which spans the x-axis on the fplot
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
xlabel('y')
ylabel('x')
To shade the region in between we have to note that the xdata is actually the YData of the plot and the ydata is actually the XData of the plot, because we are plotting x = f(y);
figure
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
xlabel('y')
ylabel('x')
%x1 = fp1.XData;
%y1 = fp1.YData;
%x2 = fp2.XData;
%y2 = fp2.YData;
y1 = fp1.XData;
x1 = fp1.YData;
y2 = fp2.XData;
x2 = fp2.YData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g'); hold off;

8 Comments

Thanks. It's inconvinient that Matlab swaps the x,y axis. I was trying to rotate the graph through the camera toolbar after plotting, but no luck. The other option is probably to swap x and y.
It's not inconvenient at all. Isn't it standard that when plotting a function that the independent variable spans the horizontal axis of the plot and the dependent variable spans the vertical axis?
If you really want the area between the inverse function as shown in the Question, then you can either find the inverse functions and go from there, as shown by @the cyclist, or do something like this:
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g');
plot(fp1.YData,fp1.XData,'b','LineWidth',2)
plot(fp2.YData,fp2.XData,'r','LineWidth',2)
delete([fp1,fp2])
This is exactly what I'm looking for. Thanks.
I have to admit to I am perplexed by the fact that you seem to want
  • x to be the dependent variable (dependent on y), but also
  • the dependent variable to be plotted along the horizontal axis
These are both unconventional choices. But, the heart wants what the heart wants, I suppose. Be sure to label your axes in your final plot.
FYI, rather than delete the lines that you don't want to see (because you only need them to get the "flipped" data), you can just make them invisible in the first place.
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2],'Visible','off'); hold on; grid on;
fp2 = fplot(x2, [y1 y2],'Visible','off');
ylim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g');
plot(fp1.YData,fp1.XData,'b','LineWidth',2)
plot(fp2.YData,fp2.XData,'r','LineWidth',2)
@the cyclist, your code does well when considering 3 curves instead of two. For example, plotting and filling the area between these three functions;
x1=2*sqrt(y); x2=(y-1)^2; x3=3-y; y1=0; y2=1; y3=2;
I was able to do it using your logic. I haven't been able to do it using @Paul's way. Thanks for the help.
I think that my other solution (the 2nd one I posted) might make this even easier.
I don't understand what you want to do for the three-curve case. How should y3 = 2 influence the result?
@Paul: y1 to y3 are the limits of integration and the functions are x depending on y. The intersection points of x=3-y, with x=2*sqrt(y) is at y=1 and 3-y and (y-1)^2 intersect at y=2, so I plotted them based on those intersection points, sort of patching all three curves together. @the cyclist's 'visible off' method allowed me to just plot the patched areas where they're filled, but with the code you provided, I got an incorrect plotting of the curve x=2*sqrt(y) or an additional curve underneath it that I wasn't able to get rid of. Both of your codes are great and I used them for different purposes.

Sign in to comment.

More Answers (2)

I think you got yourself -- and me -- confused by swapping the definitions of what are conventionally called the X- and Y-axis.
Is this what you want? Or did you want the lower one?
syms x y
% Note that I changed the function definitions in this line
x1=y^(1/2); x2=y^(1/3); y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
x1 = fp1.YData; % In these lines, you need to be careful that MATLAB calls
y1 = fp1.XData; % the horizontal axis "X", but that is what you call "Y".
x2 = fp2.YData;
y2 = fp2.XData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g'); hold off;
Another (I think simpler) method is to change the "view" of the axes.
This way the XData and YData properties of line up with what you expected. If you label your axes -- which you should -- then those do need to be swapped (because of the view change).
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
xlim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([x1 fliplr(x2)],[y1 fliplr(y2)],'g');
xlabel("y")
ylabel("x")
view([90 -90])

Products

Release

R2021a

Asked:

on 25 Jun 2025

Edited:

on 1 Jul 2025

Community Treasure Hunt

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

Start Hunting!