How to make inset figure with two y-axes

5 次查看(过去 30 天)
I can make a figure with two y-axes, using yyaxis left and yyaxis right. See here, for instance. This results in a plot where the left and right y-axes have different colors, and so do the associated data. Now, I want to use this as an inset in another figure. I try something like this:
% initial plot
plot(d(:,1),d(:,2))
% make the inset
axes('Position',[0.4,0.7,0.2,0.2])
box on
% double y-axes for the inset
yyaxis left
scatter(d2x,d2y,'x','linewidth',2)
yyaxis right
scatter(d3x,d3y,'linewidth',2)
However, this results in the proper left and right axis colors from disappearing. All data points in the inset appear the same color, so do both the y-axes. How can I do this properly?

采纳的回答

Voss
Voss 2022-7-29
Seems to be ok:
% initial plot
plot(rand(1,10),rand(1,10))
% make the inset
axes('Position',[0.4,0.7,0.2,0.2])
box on
% double y-axes for the inset
yyaxis left
scatter(rand(1,5),rand(1,5),'x','linewidth',2)
yyaxis right
scatter(rand(1,5),rand(1,5),'linewidth',2)
Is the code you posted the actual code you are running, which has the problem? Or is it some illustrative example code? If it's only an example, please share the actual code that has the problematic behavior.
  2 个评论
Irem Altan
Irem Altan 2022-7-29
Ok so this works for me as well. In my original code, the main plot consists of 30 plots plotted on top of each other with hold on. And I use colororder(jet(30)). That seems to be the problem. For instance you could do
for i=1:30
plot(rand(1,10),rand(1,10))
hold on
end
colororder(jet(30))
% make the inset
axes('Position',[0.4,0.7,0.2,0.2])
box on
% double y-axes for the inset
yyaxis left
scatter(rand(1,5),rand(1,5),'x','linewidth',2)
yyaxis right
scatter(rand(1,5),rand(1,5),'linewidth',2)
I can change the inset plot color by passing a color argument in scatter but that doesn't fix the axis colors.
Voss
Voss 2022-7-29
Maybe set the colororder for the inset axes separately:
for i=1:30
plot(rand(1,10),rand(1,10))
hold on
end
colororder(jet(30))
% make the inset
ax = axes('Position',[0.4,0.7,0.2,0.2]);
colororder(ax,[0 0 1; 1 0 0]) % blue, red
box on
% double y-axes for the inset
yyaxis left
scatter(rand(1,5),rand(1,5),'x','linewidth',2)
yyaxis right
scatter(rand(1,5),rand(1,5),'linewidth',2)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by