Change yticks using a stacked plot

52 ビュー (過去 30 日間)
Clay Swackhamer
Clay Swackhamer 2019 年 10 月 22 日
コメント済み: Eric Roden 2021 年 3 月 12 日
Hello,
I am using the stackedplot function to display multiple series of data that share the same x axis. I would like to change the y ticks, but I can't figure out how to do it. In this case, I would like to make the y tick marks go by 4,000. So basically the y axis should have the following tick marks: 0, 4000, 8000, 12000. Attached is the figure. I can't seem to find a tick mark property in this graph and using
yticks(0:4000:12000)
returns the following error:example stacked plot.png
Using xticks with stackedplot is not supported.
Thanks,
Clay

採用された回答

Artemio Soto Breceda
Artemio Soto Breceda 2019 年 10 月 23 日
You should use the stackedplot with an output argument:
handle = stackedplot(rand(5)); % Plot 5 random vectors with 5 elements each
Then you can use the handle to change the properties of your figure:
handle.AxesProperties(1).YLimits = [0 12000]; % Changes the limits of the first plot
or in a for loop to change them all:
for i = 1:numel(handle.AxesProperties)
handle.AxesProperties(i).YLimits = [0 12000];
end
This does not quite control the ticks, instead it changes the limits of the axis, such as ylim([a b]) would do.
I do not think there is any property for stackedplot to do what you want, but this approach may suffice. Otherwise, I recommend you to use subplot and simply remove the xaxis of each subplot except for the last one.
for i = 1:5
h = subplot(5,1,i);
plot(rand(1,5)); % Plotting random numbers, here you put your data
if i < 5, h.XTick = [];end
box off
end
Then you can do what you want:
h.YTick = [0:4000:12000];
I think using subplot is a much better practice, because it gives you much more freedom.
  2 件のコメント
Clay Swackhamer
Clay Swackhamer 2019 年 10 月 23 日
Hi Artemio,
Thanks. I can create a handle for the stackedplot and change the limits. However I can't seem to adjust the tick marks (for instance make the y axis increment by 4000 instead of by 5000). I will try to use tiledplot or subplot. If nobody else weighs in I will accept your answer.
Thanks,
Clay
Joe Vinciguerra
Joe Vinciguerra 2019 年 10 月 23 日
Stackedplot is comparatively new, and lacks a lot of the fine-tune-ability available in other plot types. But I would expect to see this and similar shortcomings addressed in future releases.

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2020 年 11 月 19 日
編集済み: Adam Danz 2020 年 11 月 24 日
You can get the axis handles in stackedplot using the undocumented NodeChildren property. Then set YTick.
rng('default')
h = stackedplot(1:20, rand(20,4)*1000+500);
ax = findobj(h.NodeChildren, 'Type','Axes');
set(ax, 'YTick', [600:300:1500], 'YLim', [500,1600])
To set ytick of a selected axis number n,
% set(ax(n), ...)
  2 件のコメント
Clay Swackhamer
Clay Swackhamer 2020 年 11 月 19 日
This is great, thank you for the answer.
Eric Roden
Eric Roden 2021 年 3 月 12 日
Yes, thank you, it took me a long time to find this, and am very happy I did!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by