필터 지우기
필터 지우기

(Again) plots do not appear on the graph

조회 수: 1 (최근 30일)
Neel Gupta
Neel Gupta 2020년 4월 11일
댓글: Peng Li 2020년 4월 14일
I am trying to solve a simple problem. I have a bunch of data in neat little columns. So, if a particular record has the value "Dealer" in it's 'Seller' column, I want that particular record's data to be plotted using blue colored markers and green if the "Seller" column does not have "Dealer" for that particular record.
I hashed out some code, but it is not working. The graph appears, but no plots.
while i < 302
if Seller(i) == "Dealer" % __[1]
plot(year(i), distance(i), '.b') % ___[2]
Hold on;
i = i + 1;
else
plot(year(i), distance(i), '.r')
Hold on;
i = i + 1;
end
end
I have tried using the commands [1] and [2] Individually and they seem to be working but the whole thing does not work at all....
My data is very simple table. distance and year are like ===>
27000
1900
6.900
5.200
....
....
But in seperate columns. Note that the 0's at end are there because I just divided all values by 1000.
Any help?
  댓글 수: 2
John D'Errico
John D'Errico 2020년 4월 11일
You seem to indicate you are asking this again, but I don't see where it was asked before.
Anyway, we don't actually have your data. Just something vaguely related to your data. We don't actually see all of your code, or how you use this.
For example, what was i initially? Did you ever initiaize i?
You talk about neat little columns, but we don't see any columns.
So if you want serious help, you need to seriously show what you are doing, rather than a vague approximation to what you may be doing. Otherwise, anything that is said by us would be just a wild guess.
Neel Gupta
Neel Gupta 2020년 4월 12일
Ahh, I thought that my 'vague approximation' might be of some help as my data would just be like it, but with more rows. However I have attached the whole file, but am using only two columns - kms_driven and Year.
Also, this is how my code looks like. Hope it makes sense :--
% Making a Linear Regressor using MatLab!
% Initialize it first....
close all;
filename = 'car data.csv' % open file (make sure it exist in the current folder)
i = 1; % Initialize
T = readtable(filename); % read file as table
year = T.Year;
distance = T.Kms_Driven;
Seller = T.Seller_Type;
% while i < 302 % Just normalizing that data. Ignore
% distance(i) = distance(i)/1000
% i = i + 1;
% end
while i < 302
if Seller(i) == "Dealer"
plot(year(i), distance(i), '.b')
Hold on;
i = i + 1;
else
plot(year(i), distance(i), '.r')
Hold on;
i = i + 1;
end
end
axis equal normal;

댓글을 달려면 로그인하십시오.

채택된 답변

Peng Li
Peng Li 2020년 4월 11일
편집: Peng Li 2020년 4월 11일
Without data it’s difficult to tell. You tried set a break point and see if it ever appears any points?
Based on your description I think you prob don’t need a while loop.
Try
plot(year(Seller == Dealer), distance(Seller == Dealer), '.b');
hold on;
plot(year(Seller ~= Dealer), distance(Seller ~= Dealer), '.r');
  댓글 수: 2
Neel Gupta
Neel Gupta 2020년 4월 14일
Even though your answer wasn't used by me, I realised that I was writing
Hold off;
instead of
hold off;
Now it works as expected
Peng Li
Peng Li 2020년 4월 14일
I thought it was your typo here. Then it seems you never read your command window error message as when you run it matlab must have complained about this Hold stuff as it could not recognize it.
Anyway, instead of using your while loop, why don't you try this eaiser way?
figure
plot(year(Seller == "Dealer"), distance(Seller == "Dealer"), '.b');
hold on;
plot(year(Seller ~= "Dealer"), distance(Seller ~= "Dealer"), '.r');
left: your code; Right: my code. some red dots appear on top of blue dots on my code which is a matter of order when data were drawn.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by