how to have the same settings on right Y axis from the left Y axis

12 ビュー (過去 30 日間)
endystrike
endystrike 2020 年 6 月 10 日
コメント済み: Ameer Hamza 2020 年 6 月 12 日
Hello everyone,
I struggling in having Y axis both on left and right side of a graph, but with the same settings, the same color and the same Y ticks...
I tried moving the following command "yyaxis right;" before assigning "ytickformat" and so on, but it creates a new axis from scratch and I don't know how to get the settings from the left Y axis and set them into the Y right axis.
Is there the possibility to get all the settings from Y left axis, save them into a variable and then set the Y right axis from the variable that stores everything?
Thanks

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 11 日
ax = axes();
yyaxis right
copyAxis(ax.YAxis(1), ax.YAxis(2))
function copyAxis(a, b)
p = properties(a).';
for i=1:numel(p) %copy all public properties
try %may fail if property is read-only
b.(p{i}) = a.(p{i});
catch
warning('failed to copy property: %s', p{i});
end
end
end
You can save the copyAxis function in a seperate file.
  2 件のコメント
endystrike
endystrike 2020 年 6 月 11 日
Thank you very much Ameer! :)
I've finally fixed modifying a little bit the function you've provided me... :)
function cloneYAxisFromLeftToRight()
fmt = ytickformat(gca);
ax0 = get(gca);
yyaxis right;
ax1 = gca;
p = properties(ax0.YAxis).';
for i=1:numel(p) %copy all public properties
try %#ok<TRYNC> %may fail if property is read-only
ax1.YAxis(2).(p{i}) = ax0.YAxis.(p{i});
end
end
%extras
ax1.YColor = ax0.YColor;
ax1.YColorMode = ax0.YColorMode;
ax1.YDir = ax0.YDir;
ax1.YLimMode = ax0.YLimMode;
ax1.YScale = ax0.YScale;
ax1.YTickLabelMode = ax0.YTickLabelMode;
ax1.YTickLabelRotation = ax0.YTickLabelRotation;
ax1.YTickMode = ax0.YTickMode;
%core
ax1.YTickLabel = ax0.YTickLabel;
ax1.YLim = ax0.YLim;
ax1.YTick = ax0.YTick;
%restore original format on the right Y-axis
ytickformat(fmt);
end
Ameer Hamza
Ameer Hamza 2020 年 6 月 12 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by