【发布时间】:2018-03-14 17:50:34
【问题描述】:
我正在使用三个轴对象在 x 轴上缩放我的数据。
我的问题是我不知道如何为这三个情节获得一个不错的传说。
我必须这样做,因为我的真实数据是以不同的采样率进行采样的。
我稍微编辑了图表的 m 文件,因为通常我从一些 txt 文件中读取数据。
在这个例子中,我使用 example_data 1 到 3 作为我的数据。
在此示例中,我正在缩放 example_data1,使其看起来与 example_data2 的频率相同。
我进行“缩放”ax1.XLim = [0 length(x2)]。
这就是为什么这个解决方案对我不起作用:Plot with multiple axes but only one legend。
它使用set(l3,'Parent',ax2); 以某种方式破坏了我扩展数据的方法。缩放是解决我的问题的唯一方法,因为我不知道两个采样率之间的确切关系。
我的代码:
example_data1 = repmat(1:100,1,10);
example_data2 = 2 * repmat(1:0.5:100.5,1,5);
example_data3 = [1:500 500:-1:1];
whole_length_data1 = length(example_data1);
% 1. step
start_of_data = 1;
end_of_data = 1000;
% data2
y2 = example_data2(start_of_data:end_of_data);
x2 = 0:length(y2)-1;
% data3
y3 = example_data3(start_of_data:end_of_data);
x3 = 0:length(y3)-1;
% data1
y1 = example_data1(1:length(example_data1));
x1 = 0:length(y1)-1;
% 2. step
start_one = 1;
y1 = example_data1(start_one:length(example_data1));
x1 = 0:length(y1)-1;
% 3.step
end_one = whole_length_data1 - 500;
y1 = example_data1(start_one:end_one);
x1 = 0:length(y1)-1;
Farbe1 = [0,1,0]*0.6; % Dunkelgrün
Farbe2 = [1,0,0]*0.8; % Dunkelrot
Farbe3 = get(groot,'DefaultAxesColorOrder') % default values
Farbe3 = Farbe3(1,:); % 1. Zeile der defaultvalues
figure(1)
% 3 axes
clf
%------------------------------------------------------------------
%-------------------------- plot1: ---------------------------
%------------------------------------------------------------------
plot(x2,y2,'green','LineWidth',2,'Color',Farbe1,...
'DisplayName','name of the first plot')
ax1 = gca;
ax1.XLim = [0 length(x2)]
ax1.YLim = [min(y2) max(y2)]
ax1.YTick = [0:25:300]
ax1.FontSize = 12;
legend('show')
%----------------------------------------------------------------
%-------------------------- plot2: --------------------------
%----------------------------------------------------------------
ax2 = axes('Position',ax1.Position);
plot(x3,y3,'blue','LineWidth',2,'Color',Farbe3,...
'DisplayName','plot2')
ax2.Color = 'none';
ax2.XTick = [];
ax2.XLim = [0 length(x3)];
ax2.YAxisLocation = 'right';
ax2.FontSize = 12;
legend('show')
%----------------------------------------------------------------
%-------------------------- plot3: -------------------------
%----------------------------------------------------------------
ax3 = axes('Position',ax1.Position);
plot(x1,y1,'red','LineWidth',2,'Color',Farbe2,...
'DisplayName','3')
ax3.XTick = [];
ax3.YTick = [];
ax3.Color = 'none';
ax3.XAxisLocation = 'top';
ax3.YAxisLocation = 'right';
ax3.XLim = [0 length(x1)];
ax3.YLim = [min(y1) max(y1)*2];
legend('show')
这会导致图例看起来很糟糕:
我真的希望有人可以帮助我。
非常感谢。
【问题讨论】:
-
在最后一个(顶部)
axes中创建与前一个图相同颜色的虚拟图,然后只显示最后一个axes的图例。要创建具有所有正确属性的虚拟(不可见)图,只需使用NaN:plot(NaN,'Color',thisDataColor,'DisplayName',thisDataName)
标签: matlab plot matlab-figure legend legend-properties