# How can i draw the graph?

3 views (last 30 days)
zahid on 1 May 2015
Commented: Star Strider on 4 May 2015
Dear all i am new in matlab. Could.Could anyone help me in drawing the following graph for may data(data is large but i am showing you just 10 values for illustration purpose).Now i want to plot these values in xy plot such that our x-axis contains the values 1 to 10 and y-axis has the output which is given below .Our graph should look like the following image.Actual at y-axis i need three straight line parallel to x-axis at the point -3,0 and 3. -0.10890, -0.07835, 0.35470, -0.18300, 0.06880, 0.01870, 1.07650, 0.91125, 0.14735, 3.41050
THANKS

Star Strider on 1 May 2015
Edited: Star Strider on 1 May 2015
@zahid — I generally don’t check my ‘gmail’ account more often than once every two or three days. I saw yours a few minutes ago, and am altering my code and the posted plot to reflect your request with respect to the 'YTick' positions (only -3 0 3) and labels.
———————————————————————————————————————————————
Using xlim to define the x-coordinates of the red and green lines will allow it to adapt to your plot for all data vector lengths:
x = 1:10;
y = [-0.10890, -0.07835, 0.35470, -0.18300, 0.06880, 0.01870, 1.07650, 0.91125, 0.14735, 3.41050];
figure(1)
plot(x, y, '.-k')
hold on
plot([xlim;xlim]', [-3 -3; 3 3]','r', xlim,[0 0],'g')
hold off
axis([xlim -4 4])
y0 = text(0.75, 0, '$\bar{x}$','interpreter','latex', 'HorizontalAlignment','right', 'VerticalAlignment','middle');
ylbl = {'LCL', 'UCL'};
set(gca, 'YTick',[-3 3], 'YTickLabel',ylbl)
producing:
##### 3 CommentsShow 1 older commentHide 1 older comment
zahid on 4 May 2015
Dear Strider
I want further improvement in my graph such that if any point exceed the UCL or LCL ,the corresponding number of x-axis appear along with this point.For example in the previous graph at 10 on x-axis,y exceeds the UCL,so 10 should be appear with this point.This is an example where in actual problem this number can occur anywhere in the graph.
Star Strider on 4 May 2015
Those changes were easy!
x = 1:10;
y = [-0.10890, -0.07835, 0.35470, -0.18300, 0.06880, 0.01870, 1.07650, 0.91125, 0.14735, 3.41050];
xplot = [min(x) max(x)];
figure(1)
plot(x, y, '.-k')
hold on
plot([xplot;xplot]', [-3 -3; 3 3]','r', xplot,[0 0],'g')
hold off
axis([xplot+[-0.5 +0.5] -4 4])
y0 = text(xplot(1)-0.75, 0, '$\bar{x}$','interpreter','latex', 'HorizontalAlignment','right', 'VerticalAlignment','middle');
ylbl = {'LCL', 'UCL'};
set(gca, 'YTick',[-3 3], 'YTickLabel',ylbl)
extidx = find((y>3) | (y<-3));
extlbl = strsplit(sprintf('%.0f ', x(extidx)),' ');
text(x(extidx),y(extidx), extlbl(1:end-1))
You may want to experiment with it to get the exact look you want, but that should not be difficult.
The resulting revised plot:

pfb on 3 May 2015
Edited: pfb on 3 May 2015
vectors x and y contain your data
plot(x,y,'k.');
If you only have the ydata, just type
plot(y,'k.');
This holds the first plot, otherwise it would be replaced by the following ones
hold ;
This plots a green horizontal line at y=3, between 0 and 10
plot([0 10],[3 3],'g')
Type "doc plot" to get the documentation and more examples
---------------
Note This was my original answer to Zahid's question.
Star Strider gave a more complete answer, but Zahid mistakenly accepted mine instead of his.
At Zahid's request, I deleted my original answer to allow Zahid to accept Star Strider's. I'm posting my answer again as it was.

### Categories

Find more on Line Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!