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];
also I tried the solution from (Q&A) after changing it but also I didn;t get what I want.
Please find the mat file of the array in the attachements, (PS just the first two fields).
Thanks in advance

 Risposta accettata

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 Commenti

wow, it's really simpler using plot, Thanks a lot
As always, my pleasure!
Mohammed Hammad
Mohammed Hammad il 22 Mar 2019
Modificato: Mohammed Hammad il 22 Mar 2019
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
ALGOPARK_TL.png
The solution I wrote here (link) should work with your new data as well.
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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange

Prodotti

Release

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by