MATLAB:保存为无花果后,plotlyy中的框未对齐 [英] MATLAB: misaligned boxes in plotyy after saving as fig
问题描述
我使用plotyy在一张图中放置两个图:
I use plotyy to put two plots in one graph:
f = figure('Color','white');
[ax,p1,p2] = plotyy(xx, yy1, xx, yy2);
ylabel(ax(1),'Phase','FontSize',18);
ylabel(ax(2),'Spectrum','FontSize',18);
set(ax,{'ycolor'},{'k';'k'});
set(p1,'LineWidth',2,'Color',[0.4940,0.1840,0.5560]);
set(p2,'LineWidth',2,'Color','red');
xlabel(ax(1),['Frequency [THz]'],'FontSize',18);
set(ax,'FontSize',14)
图形显示得很完美,但是当我尝试将其保存为无花果时,会出现诸如未对齐框之类的东西.
Figure is displayed perfectly, but when I try to save it as fig something like misaligned boxes appears.
我尝试使用链接轴,但没有结果.
I tried to use linkaxes, but with no result.
推荐答案
Position
属性设置为与主轴相同,它们可以很好地堆叠.
plotyy
has been one of my favorite MATLAB functions to love to hate. It's a really useful function that I always seem to run into bugs with, to the point where I've completely stopped using it in favor of just stacking two (or more) axes objects and plotting to them separately. You can then set the Position
property of the 'sub' axes to the same as your primary axes and they will stack nicely.
一个功能示例:
xx = linspace(-15,15,100);
yy1 = sin(xx);
yy2 = cos(xx);
f = figure('Color','white');
ax(1) = axes('Parent', f);
ax(2) = axes('Parent', f, 'Color', 'none', 'XTick', [], 'YAxisLocation', 'right');
p1 = plot(ax(1), xx, yy1);
hold(ax(2), 'on'); % Hold to preserve our axes properties set above
p2 = plot(ax(2), xx, yy2);
hold(ax(2), 'off');
ylabel(ax(1),'Phase','FontSize',18);
ylabel(ax(2),'Spectrum','FontSize',18);
set(ax,{'ycolor'},{'k';'k'});
set(p1,'LineWidth',2,'Color',[0.4940,0.1840,0.5560]);
set(p2,'LineWidth',2,'Color','red');
xlabel(ax(1),'Frequency [THz]','FontSize',18);
set(ax,'FontSize',14)
set(ax, 'ActivePositionProperty', 'position'); % Resize based on position rather than outerposition
set(ax(2), 'Position', get(ax(1), 'Position')); % Set last to account for any annotation changes
在堆叠轴的同时,您还将注意到我将ActivePositionProperty
设置为position
(而不是outerposition
).当Units
属性为时, MATLAB自动调整轴大小.设置为Normalized
,这似乎是问题出现的主要地点.在调整大小时,MATLAB还会修改第二个轴的OuterPosition
值,从而使其调整绘图部分的大小.在我的案例中,[0 0 1 1]
与[0 0.0371 1.0000 0.9599]
的差异很小,但是效果显然非常明显.您可以使用 get
和
Along with stacking the axes you will also note that I have set the ActivePositionProperty
to position
(rather than outerposition
). MATLAB resizes axes automatically when the Units
property is set to Normalized
, and it seems like this is the main spot where the issue is arising. On resizing, MATLAB also modifies the OuterPosition
value for the second axes, causing it to resize the plot portion. The difference is small, [0 0 1 1]
vs. [0 0.0371 1.0000 0.9599]
in my case, but the effect is obviously very pronounced. You can use get
and set
to fix this, but you'll have to do it on every resize which is fairly annoying. The alternative is to resize based on the Position
, which seems to alleviate the issue and is a tweak present in the R2015b implementation of plotyy
. This also fixes plotyy
except for cases where the window is very small, so I have left my answer with the more generic approach.
这篇关于MATLAB:保存为无花果后,plotlyy中的框未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!