Plot multiple y-value on a single x-value
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello,
I have array with two field (year and xx), for each year it has multiple y-value.
for exampel:
year = [2000 2001 2005 2008]
xx = [(5 10 20); (40 60); (30 20 10 50); (1)]
I am trying to plot scatter for each year (x-axis) all the coressponding values of xx (y-axis)
I was trying do it like this:
dataplot = [];
dataplot = [dataplot; year xx yy zz];
[ay,~,cy] = unique(dataplot(:,1),'rows'); % to get just the unique year
figure
scatter(year,xx,'*');
ax = gca;
ax.XTick = 1:numel(ay);
ax.XTickLabel = ay;
ax.XLim = [0 numel(ay)+1];
Please find the mat file of the array in the attachements, (PS just the first two fields).
Thanks in advance
채택된 답변
Star Strider
2019년 3월 18일
Try this:
year = [2000 2001 2005 2008];
xx = {[5 10 20]; [40 60]; [30 20 10 50]; 1};
figure
hold all
for k1 = 1:numel(year)
plot(ones(1,numel(xx{k1}))*year(k1), xx{k1}, 'p')
end
hold off
It is necessary to put ‘xx’ in a cell array, since the vector lengths are different.
Experiment to get the result you want.
Plotting your actual data are even easier, since all the data are the same and each row has an associated year:
D = load('dataplot.mat');
data = D.dataplot;
figure
plot(data(:,1), data(:,2:end)', 'p')
댓글 수: 12
wow, it's really simpler using plot, Thanks a lot
As always, my pleasure!
Mohammed Hammad
2019년 3월 22일
편집: Mohammed Hammad
2019년 3월 22일
Hello Star Strider,
I have a strange issue with the plots
the first one is alright but the second is shown more x-ticks (e.g 2002.5 2003 2003.5 2004 2004.5) which I don't have (2002.5 2003.5 ....) in my array, also I don't want them. I need just the years (2002 2003 2004) on X_Axis.
PS: If I edit the change the ticks properties from edit >> Axis properties >> ticks to make it manually or to make it step, it works fine. but I don't want to make it manually for each figure. Why MatLab change it automatically?
Can you help with that please?
The data you posted plots correctly for me in R2019a, and does not demonstrate the effect you observed.
I cannot reproduce what you are seeing, however this may fix it:
figure
plot(data(:,1), data(:,2:end)', 'p')
xt = get(gca, 'XTick');
Yu = unique(fix(xt));
xtix = linspace(min(Yu), max(Yu), numel(Yu));
set(gca, 'XTick', xtix)
This runs without error, although I am posting it as UNTESTED CODE since it is a fix for a problem I do not have with your data or plots.
Thank you for the answer,
Me neither, I couldn't know why it generate automatically some additional ticks for the years. But now with the code you posted, it works perfect and it shows only the values of the array on X-Axis.
As always, my pleasure.
I am glad it solved your problem, since I could not test it to be certain.
Sorry for the inconvience again, I updated to Matlab 2019a, I want to plot the data (attached) just the column 1 years (On X-axis) with the column 2 (on Y-axis). I got the same problem which it shows the years 2002.5 2003.5 on the X-Axis

This works when I run it (in R2019a):
D = load('data2.mat');
data = D.ALGOPARK_DATA;
figure
plot(data(:,1), data(:,2:end)', 'p')
xt = get(gca, 'XTick');
Yu = unique(fix(xt));
xtix = linspace(min(Yu), max(Yu), numel(Yu));
set(gca, 'XTick', xtix)
The y-axis in my plot doesn’t look like yours, though. I have no idea what you’re plotting.
Mohammed Hammad
2019년 3월 23일
편집: Mohammed Hammad
2019년 3월 23일
Yeah your code is working fine, but I think Matlab must plot just the data on column one (2002, 2003,2004,...) without adding any more ticks. Also I have around 300 figures, so I need now to add your code to each figure manually!.
The y-axis in my plot doesn’t look like yours:
Because I am plotting just the second column
If you need to use this for every one of 300 figures, you can either copy and paste my code to each one (inconvenient), or save this function:
function yrtix(axhand)
xt = get(gca, 'XTick');
Yu = unique(fix(xt));
xtix = linspace(min(Yu), max(Yu), numel(Yu));
set(gca, 'XTick', xtix)
end
to yrtix.m in your MATLAB search path, and then do this with each figure (convenient):
D = load('data2.mat');
data = D.ALGOPARK_DATA;
figure
plot(data(:,1), data(:,2:end)', 'p')
yrtix(gca)
That worked when I tested it.
Yeah the function works perfect. BUT still the question:
(WHY MATLAB DOESN'T PLOT JUST THE DATA IN THE ARRAY WITHOUT ADDING MORE TICKS !?)
I have no idea. Use the Contact Us link in the upper right corner of this page and ask MathWorks!
I’m happy my function works, though!
추가 답변 (0개)
카테고리
도움말 센터 및 File 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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
