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
采纳的回答
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.
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)
