Reputation: 502
For example I have an x values and y values like this :
x = [0.5 , 1.5 , 2.5];
y = [3, 6, 9];
And I want to draw a graph like below. (Red lines and spesific red values on axis are important for me, scale of axis doesnt matter)
I am searching for a while but no luck. How can i do that.
Upvotes: 1
Views: 408
Reputation: 21
The code below will pull the automatic x-axis and y-axis labels and add in any additional unique (not already included) axis labels for the lines that you've specified. IT will also plot any arbitrarily large data set provided.
clc; clear; clf; % // preamble to clear previous data and plots
x = [0.23 , 1.5 , 2.5];
y = [3, 4.5, 9];
hold on
for i=1:numel(x)
plot([x(i),x(i)],[0,y(i)],'-r'); % // plots vertical lines
plot([0,x(i)],[y(i),y(i)],'-r'); % // plots horizontal lines
end
scatter(x,y,70,'r','MarkerEdgeColor','r','MarkerFaceColor','w')
hold off
axis([0 max(x)+1 0 max(y)+1]); % // changes axis to be [0 1+max(x)] and [0 1+max(y)]
XTickAutomatic=get(gca,'XTick'); % // retrieves current x-axis tick marks
YTickAutomatic=get(gca,'YTick'); % // retrieves current y-axis tick marks
set(gca,'XTick',sort(unique([XTickAutomatic,x]))) % // create new ticks marks with any unique x-coordinates
set(gca,'YTick',sort(unique([YTickAutomatic,y]))) % // create new ticks marks with any unique y-coordinates
Upvotes: 0
Reputation: 112769
Try this. Note that x
and y
are assumed to be row vectors.
stem(x,y,'r'); %// plot vertical lines with circles
hold on %// keep current graph; more will be added
plot([zeros(1,numel(x)); x], [y; y], 'r') %// plot horizontal lines
set(gca, 'xtick',sort(x), 'ytick',sort(y)) %// set ticks on x and y axes
axis([0 6 0 10]) %// set axis size
Upvotes: 2