Color graph area in different shades

Hi everyone, I'd like to do something but don't know whether it's possible or not in Matlab:
I plotted GDP values over time in a 2d graph.
I colored the area below the curve, but I'd also like this colored area to have different shades: a stronger shade when GDP gets higher, and a pale one when GDP values are lower.
Is it possible to do it?

 Accepted Answer

Adam Danz
Adam Danz on 14 Mar 2019
Edited: Adam Danz on 16 Mar 2019
Yep, it's possible.
Check out these examples using patch(). Your x and y values will be defined by the curve and the limits of your axes. The c value will be defined by GDP.
Demo
y = rand(1,100); % this would be GDP
x = linspace(1,200,100);
% x,y define the 'top' of the shape. Now we define and sides and bottom.
yy = [y, zeros(size(x))];
xx = [x, fliplr(x)];
figure()
patch(xx,yy, [y,zeros(size(y))])
colorbar
Note the redundancy between the y axis values and the colorbar values which indicate the same thing. The only added benefit is visual aesthetic.

5 Comments

Thank you for the suggestion!
It works, although maybe I'm doing something wrong, because it doesn't fill the entire area below the GDP curve, it kind of creates a polygon and then fills it.
For example:
figure()
xx = rand(1,100); % this would be GDP
patch( linspace(1,200,100), xx, xx )
colorbar
Instead I'd like to have something similar to this
area(linspace(1,200,100), xx)
with the same coloration that patch produces
Ok, maybe I was using it in the wrong way: this
figure()
xx = randn(1,100);
xx = [0 xx 0];
zz = linspace(1,200,100);
zz = [zz(1) zz zz(end)];
patch( zz, xx, xx )
seems to do the job
Adam Danz
Adam Danz on 15 Mar 2019
Edited: Adam Danz on 15 Mar 2019
You need to create the coordinates of a shape. Let's say your line is the 'top' of that shape. At each end you need to drop the sides of the shape down to zero and then at the bottom, you'll have a straight line across y=0.. I updated my solution to include a demo of what I think you're trying to do.
Just saw your 2nd reply. Way to go!
Thank you so much!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 14 Mar 2019

Edited:

on 16 Mar 2019

Community Treasure Hunt

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

Start Hunting!