2
votes

For example if I have the following code in MATLAB

x = 0:0.1:2*pi;
y = sin(x);

figure1 = figure;
axes1 = axes('Parent',figure1,'XTick',[0 2 5],'XGrid','on');
box(axes1,'on');
hold(axes1,'all');
plot(x,y);

This produces the following graph. enter image description here

I am trying to either hatch the graph or color the graph.

How can I hatch the graph from the region x = 0 and x = 2 and the function and similary hatch the plot from the region x = 5 till the end. Similary if I want to color the graph between these same regions, how can I do it?

I tried using the plot::hatch and the plot tools option but it didnt work. Any help will be greatly appreciated.

Thank you.

1
Could you specify exactly which areas you want to hatch/color? Do you mean the area between the graph and zero or something else? - 3lectrologos
The area between x = 0 and x = 2 (marked by the dotten line) and the function y. Similary the area between x = 5 (marked by the dotted line) till the end of the x-axis and the function y. In other words, between the x-axis extending from x=0 to x=2 and the function. - Sista

1 Answers

3
votes

Something like this:

x = 0:0.1:2*pi;
y = sin(x);

axes1 = axes('XTick',[2 5],'XGrid','on');
box(axes1,'on');

plot(x,y);
xlim(x([1 end]))
yl = ylim;

hold on
idx = x <= 2;
area(axes1, x(idx), y(idx), yl(1), 'FaceColor','r','EdgeColor','none')
idx = x >= 5;
area(axes1, x(idx), y(idx), yl(1), 'FaceColor','r','EdgeColor','none')
hold off