Add cut line (horizontal line) in bar graph

Hello everybody.
I just made a bar graph using the bar function, but I want a horizontal line to appear on the x axis. I know the xline function exists, but my MATLAB version appears to be old. Below I show the simple code.
x = [3.10,8.17,9.84,4.71,1.43,6.90,3.70,10.24,9.61,5.92,4.51,7.12,2.99,4.73,5.96,9.58,7.22,4.91,4.80,8.79,8.45,6.38,2.83,1.10,7.47,5.36,10.42,5.11,5.15,4.25,7.65,7.05,3.60,4.08,7.29,4.72,4.90,8.74,5.42,9.96,4.88,4.06,4.68,6.67,4.16,6.20,7.34,2.96,7.16,6.34,9.20,4.66,7.42,3.32,8.45,5.59,4.97,5.50,6.20,1.30,3.84,3.97,7.98,6.71,5.26,6.39,6.24,9.19,9.27,4.63,7.20,7.45,4.94,7.57,6.79,8.58,6.51,5.98,5.44,5.47,6.67,5.33,7.59,5.82,4.24,2.86,3.14,4.93,4.15,4.58,7.20,5.33,4.17,6.30];
bar(x,0.8,'w');
ylabel('volume')
xlabel('Pacients')
xticks([1:94]);
yticks([1:11]);

 Accepted Answer

Horizontal line on the x axis. What does that mean? The x axis is already horizontal so the line would overlap it. Do you mean horizontal line parallel with the x axis, intersecting the y axis at some value? xline() gives a vertical line, not a horizontal line. So we're not sure what you want. So I'm doing it both ways:
x = [3.10,8.17,9.84,4.71,1.43,6.90,3.70,10.24,9.61,5.92,4.51,7.12,2.99,4.73,5.96,9.58,7.22,4.91,4.80,8.79,8.45,6.38,2.83,1.10,7.47,5.36,10.42,5.11,5.15,4.25,7.65,7.05,3.60,4.08,7.29,4.72,4.90,8.74,5.42,9.96,4.88,4.06,4.68,6.67,4.16,6.20,7.34,2.96,7.16,6.34,9.20,4.66,7.42,3.32,8.45,5.59,4.97,5.50,6.20,1.30,3.84,3.97,7.98,6.71,5.26,6.39,6.24,9.19,9.27,4.63,7.20,7.45,4.94,7.57,6.79,8.58,6.51,5.98,5.44,5.47,6.67,5.33,7.59,5.82,4.24,2.86,3.14,4.93,4.15,4.58,7.20,5.33,4.17,6.30];
bar(x,0.8,'w');
ylabel('volume')
xlabel('Pacients')
xticks([1 : 10 : 94]);
yticks([1 : 11]);
grid on;
% Draw a vertical line at x = 50;
line([50, 50], ylim, 'Color', 'm', 'LineWidth', 2);
% Draw a horizontal line at y = 6;
line(xlim, [6, 6], 'Color', 'r', 'LineWidth', 2);

5 Comments

Thanks for answering, yes, I was referring to a line parallel to the x axis. ¨
Many thanks.
In this case is it possible to give input value by using editfield. So that it should work for any value in the graph?
Now there are functions xline and yline if you want perfectly vertical or horizontal lines, respectively.
@Lavanya not sure what you're asking, but yes, you can get a numerical value from an edit field and pass that to xline or yline.
num = str2double(handles.edtNumber.String);
xline(num, 'LineWidth', 2, 'Color', 'm');
Ok, I will try to explain in a better way. As shown in 'image1.jpg', by using the ginput() , able to select the coordinates and using xline it's appearing as blue line. My question is If I want to try the ginput values manually( By using Editfield) the coordinates from the graph. Is it possible to that?
For example:
If I will give x=0.001,y = 12, from the edit field, 'image1.jpg', 1st blue line should appear.
Yes, does it not do that? Where did it draw the line when you ran my code. Perhaps start your own discussion thread.

Sign in to comment.

More Answers (1)

Duijnhouwer
Duijnhouwer on 30 Jun 2020
Edited: Duijnhouwer on 30 Jun 2020
hold on
plot(xlim,[5 5],'r-','LineWidth',2)

Categories

Community Treasure Hunt

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

Start Hunting!