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 件のコメント
Mohammed Hammad
2019 年 3 月 18 日
wow, it's really simpler using plot, Thanks a lot
Star Strider
2019 年 3 月 18 日
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?
Star Strider
2019 年 3 月 22 日
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.
Mohammed Hammad
2019 年 3 月 22 日
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.
Star Strider
2019 年 3 月 22 日
As always, my pleasure.
I am glad it solved your problem, since I could not test it to be certain.
Mohammed Hammad
2019 年 3 月 23 日
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

Star Strider
2019 年 3 月 23 日
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
Star Strider
2019 年 3 月 23 日
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.
Mohammed Hammad
2019 年 3 月 23 日
Yeah the function works perfect. BUT still the question:
(WHY MATLAB DOESN'T PLOT JUST THE DATA IN THE ARRAY WITHOUT ADDING MORE TICKS !?)
Star Strider
2019 年 3 月 23 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
