plotting a horizontal line at each data point

How to plot a horizontal line at each co-ordinate.Here consider 1a,1b, 1c etc as horizontal points, energy values as vertical points.Also how to indicate which color is used for which.pls help.

1 Comment

This is not an elegant solution but you can probably solve this with a loop and line()

Sign in to comment.

 Accepted Answer

Try this:
x = 1:10; % Create Data
yh = rand(size(x))-2; % Create Data
yl = rand(size(x))-4; % Create Data
hl = @(x,y,l,c) plot(x+[-l l], y*[1 1],c); % Anonymous Function To Draw Horizontal Lines
figure
hold on
for k = 1:numel(x)
hl(x(k),yh(k),0.5,'b')
hl(x(k),yl(k),0.5,'r')
text(x(k),yh(k), sprintf('%5.2f',yh(k)), 'VerticalAlignment','bottom', 'HorizontalAlignment','center')
text(x(k),yl(k), sprintf('%5.2f',yl(k)), 'VerticalAlignment','top', 'HorizontalAlignment','center')
text(x(k),yh(k),'\uparrow', 'VerticalAlignment','top', 'HorizontalAlignment','center')
text(x(k),yl(k),'\downarrow', 'VerticalAlignment','bottom', 'HorizontalAlignment','center')
text(x(k), mean([yh(k) yl(k)]), sprintf('%5.2f',(yh(k)-yl(k))), 'VerticalAlignment','middle', 'HorizontalAlignment','center')
plot([1 1]*x(k),[yl(k) yh(k)+sign(yh(k))*0.1], '-k', 'LineWidth',0.5)
end
yl = ylim;
ylim(yl+[-1 1]*0.2)
.

10 Comments

If i want to put specific "y" values,then how can i do this?
THANKS
My pleasure!
I am not certain what you are asking. In my code, ‘yh’ are the higher y-values and ‘yl’ are the lower y-values. I have no idea what your data are, so you would have to separate them into those vectors to use my code.
If you provide your x and y data, I can (probably) change my code to accommodate them.
Also, I forgot about the x-tick labels. Use this call:
set(gca, 'XTick',x, 'XTickLabel',{'1a' '1b' '1c' '2a' '2b' '2c' '3a' '3b' '3c'})
after the plotting loop to set them.
And use this ‘x’ vector with my code and those x-tick labels:
x = 1:9; % Create Data
.
I have the following data.Also pls can you indicate which color is used for ' yh' and which for 'yl' like my first fig.on right side of the plot.
THANKS.
My pleasure!
The ‘c’ (fourth) argument in the ‘hl’ anonymous function denotes the line color:
hl = @(x,y,l,c) plot(x+[-l l], y*[1 1],c); % Anonymous Function To Draw Horizontal Lines
so calling it in the loop:
hl(x(k),yh(k),0.5,'b')
hl(x(k),yl(k),0.5,'r')
uses blue ('b') for ‘yh’ and red ('r') for ‘yl’, since that is how they are plotted in the image you posted. The line lengths are 1 in this example (going from -0.5 to +0.5 with respect to ‘x’). The third (‘l’) argument controls that. It can be anything you want.
The rest of the code prints the labels for the horizontal lines and y-value differences, and plots the arrowheads and the vertical lines connecting them.
.
As always, my pleasure!
Pls can you tell me how to insert legend in the above plot.Like 'red line' corresponds to 'homo' and 'blue line'
corresponds to 'lumo' .Thanks.
As always, my pleasure!
There are two ways to do that.
The first simply reverses the order of the legend arguments (with no other changes in the code), adding this line at the end:
legend('LUMO', 'HOMO')
The second requires returning the handles of the red and blue lines:
hu = hl(x(k),yh(k),0.5,'b');
hd = hl(x(k),yl(k),0.5,'r');
(with no changes in the rest of the code) and then at the end adding:
legend([hd hu], 'HOMO', 'LUMO')
Both of these work. Choose the one that you prefer.
.
As always, my pleasure!

Sign in to comment.

More Answers (1)

My first thought when I saw your picture was errorbar.

Categories

Products

Release

R2017b

Tags

Community Treasure Hunt

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

Start Hunting!