You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to use multiple colums to make graph (scatter) in Matlab
1 view (last 30 days)
Show older comments
I want to make scatter plot suh that on left side plot using x1 and y1 values to make 1 point on left side and similar using x2 and y2 make a point on right side, Kindly help me with the code
for each row I want two points in graph
I have to do this process for the whole file ,
thanks
Answers (1)
SuperNova
on 10 Sep 2019
Edited: SuperNova
on 10 Sep 2019
I'm guessing that you want two plots on the same graph, and that you'd use different y axes to make a point. This answer assumes that is true.
First you need to import your data:
filename = 'YourData.csv';
delimiterIn = ',';
headerlinesIn = 1;
data = importdata(filename,delimiterIn,headerlinesIn); %you may want to export the Excel document as a .txt file first, those are easier
Then define your variables:
x1 = data(:,2); %the second column in your screenshot is the x1
y1 = data(:,3);
x2 = data(:,5);
y3 = data(:,6);
Now make your plot:
yyaxis left
plot(x1,y1) %you can also use scatter or whatever kind of graph you want to make
yyaxis right
plot(x2,y2)
yyaxis left
title('Plots with Different y-Scales')
xlabel('X')
ylabel('Y1')
yyaxis right
ylabel('Y2')
Also look here for more specifics: https://www.mathworks.com/help/matlab/creating_plots/plotting-with-two-y-axes.html
30 Comments
Maleeha Khalid Khan
on 12 Sep 2019
thank u so much for your help, it means a lot
actually I want to two points, point p1 and p2, p1 has its x and y value and p2 has a seprate x and y value both are on the same graph p1 on the left and p2 on the right side
this image might help in understanding
Adam Danz
on 12 Sep 2019
The image in your question lists many points. The comment above says you only want two points.
Each point has an (x,y) coordinate which is defined in the image in your question.
This is still very unclear.
Maleeha Khalid Khan
on 12 Sep 2019
Edited: Maleeha Khalid Khan
on 12 Sep 2019
I want only two points p1 and p2
p1 should be on the left and it has its own x and y value coordinates
similary p2 should be on right and has its own x and y value coordinates
Adam Danz
on 12 Sep 2019
So your quesiton is how to plot 2 points?
figure()
plot([-5,5], [1,2], 'bo')
ylim([-5 5])
xlim([-10,10])
Maleeha Khalid Khan
on 12 Sep 2019
and how to apply loop so I can plot points for the whole file, for each row a graph should be plotted
SuperNova
on 12 Sep 2019
Edited: SuperNova
on 12 Sep 2019
You don't need a for loop for plotting points. In my original answer I defined variables of the data that you have. The computer is smart enough to take each (x,y) pair and plot them. All you need to do is tell the computer what the data are (define the variables).
Unless you actually want two plots for every row of data with one data point on each plot?
Adam Danz
on 12 Sep 2019
Why is there so much discussion about right and left side? That's still not clear to me at all.
You have 2 sets of points. Is your goal merely to plot those points?
Maleeha Khalid Khan
on 12 Sep 2019
Edited: Maleeha Khalid Khan
on 13 Sep 2019
no, I dont want two plots, I want a single plot for every row with 2 points on it (like scatter points)
Maleeha Khalid Khan
on 12 Sep 2019
the reason of left and right side is that these points coorespond to an image
yes my goal is to plot these points, have to plot for every image and have data of 1400 images
Maleeha Khalid Khan
on 13 Sep 2019
Edited: Maleeha Khalid Khan
on 13 Sep 2019
I have tried this code have made few chaging accordingly but its not working, any suggestions??
Adam Danz
on 13 Sep 2019
After you import your data, all you have to do is,
plot(x1,y1)
hold on
plot(x2,y2)
Adam Danz
on 13 Sep 2019
'not working" doesn't tell us anything. We can't see what you see and we don't know what your expected outcomes are.
Maleeha Khalid Khan
on 13 Sep 2019
hope so now you can see, now it tells you a lot. I just want the two points , which are not plotted this is the output a blank plot with no points on it
Maleeha Khalid Khan
on 13 Sep 2019
thihs is what I want, graph like this with two points for each row value
SuperNova
on 13 Sep 2019
Right, but did you define the x and y values properly? It would help us out if we could see your code and identify if there are any discrepancies
Adam Danz
on 13 Sep 2019
Why do you need 2 y-axes?
Why doesn't this work?
figure()
plot(x1,y1)
hold on
plot(x2,y2)
Walter Roberson
on 13 Sep 2019
Instead of mydata(:,3) use mydata{:,3} and likewise for the other items you assign to variables from mydata .
Maleeha Khalid Khan
on 13 Sep 2019
Adam Danz, I mean one row should be plotted at a time then the second row and the rest, and i want to save each plot in jpeg format, I am using matllab r2017b
Walter Roberson
on 13 Sep 2019
Loop over the rows:
for r = 1 : size(y1,1)
thisfile = sprintf('plot_%03d.jpg', r);
thisx1 = x1(r,:);
thisy1 = y1(r,:);
plot(thisx1, thisy1);
xlabel and ylabel and title and legend as appropriate
pause(1) %give it time to render
saveas(gcf, thisfile)
end
Maleeha Khalid Khan
on 13 Sep 2019
thanks &
from every row there r 2 points like p1 and p2
p1 has its own x and y coordinates , p2 has sperate y coordinate
assume p1 has y axis on left and p2 has y axis on right, but same x axis
so how should I apply loop
Walter Roberson
on 13 Sep 2019
When I compare that situation to the data you presented an image of, it looks to me as if p1 x and p1 y are scalars. If you plot those (and y2) in one plot, then you would only get a single dot, not a line. It also looks to me like p2 x is not the same as p1 x ?
Maleeha Khalid Khan
on 14 Sep 2019
yes you are right its like this p1 and p3 hv different x value and y value but I want to plot them in same graph
Walter Roberson
on 14 Sep 2019
for r = 1 : size(y1,1)
thisfile = sprintf('plot_%03d.jpg', r);
thisx1 = x1(r,:);
thisy1 = y1(r,:);
thisx2 = x2(r,:);
yyaxis left
h1 = plot(thisx1, thisy1, 'r*');
xlabel('x')
ylabel('y1')
yyaxis right
h2 = plot(thisx1, thisy2, 'b^');
ylabel('y2')
legend([h1, h2], {'x1', 'x2'});
title( sprintf('row #%d', r) )
pause(1) %give it time to render
saveas(gcf, thisfile)
end
See Also
Categories
Find more on 2-D and 3-D 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)