Matlab:具有子图例的子图

4
我想在一个2x2的子图底部添加一个图例:
如你所见,第二行的图表被挤压了一点,因为我手动调整了它。
是否有像sublegend(...)这样的函数,还是需要大量编码?
源代码:此脚本生成类似的子图:
%weights/weightsMV are 3 x 30 matrices
ticker = {'A','B','C'};
weights = [0.764602615068780,0.762329415005434,0.760055503116586,0.757781382654864,0.755508517683302,0.753234375934985,0.750960611173760,0.748686727917457,0.746413866211585,0.744140033854148,0.738367347277555,0.699505907957926,0.660644468638298,0.621783029318668,0.582921589999040,0.544060150679411,0.505198711359782,0.466337272040153,0.427475832720524,0.388614393400895,0.349752954081266,0.310891514761637,0.272030075442009,0.233168636122380,0.194307196802750,0.155445757483122,0.116584318163493,0.0777228788438641,0.0388614395242353,4.10026844681349e-12;0.235397384931220,0.211630038764514,0.187863021792451,0.164096524898550,0.140329000457761,0.116562305396725,0.0927953654491337,0.0690285024746061,0.0452609759124620,0.0214940798746062,0,5.55111512312733e-17,0,5.55111512312746e-17,0,3.70817036264992e-30,4.73852588619852e-30,5.76503212536727e-30,5.38827593281225e-30,2.64267976542398e-18,5.69348240589127e-18,8.74428504635863e-18,1.17950876868260e-17,0,2.75245082116940e-18,1.22032105779319e-17,9.15240793746451e-18,6.10160529699720e-18,6.10160531305980e-18,1.30251232759518e-10;1.10307850379949e-18,0.0260405462300517,0.0520814750909638,0.0781220924465861,0.104162481858937,0.130203318668290,0.156244023377106,0.182284769607937,0.208325157875953,0.234365886271246,0.261632652722445,0.300494092042074,0.339355531361703,0.378216970681332,0.417078410000960,0.455939849320589,0.494801288640218,0.533662727959847,0.572524167279476,0.611385606599105,0.650247045918734,0.689108485238363,0.727969924557991,0.766831363877620,0.805692803197249,0.844554242516878,0.883415681836507,0.922277121156136,0.961138560475765,0.999999999865655]
weightsMV = [0.304769232969962,0.313206616760582,0.299868860275947,0.286531103791370,0.273193347306773,0.259855590822158,0.246517834337581,0.233180077852984,0.219842321368369,0.206504564883792,0.193166808399157,0.179829051914561,0.166491295429983,0.153153538945368,0.139815782460772,0.126478025976194,0.113140269491579,0.0998025130069825,0.0864647565224054,0.0731270000377706,0.0597892435531935,0.0464514870685779,0.0331137305839816,0.0197759740993660,0.00643821761478891,0,0,0,0,0;0.695230767030038,0.658949751075843,0.636813620182365,0.614677489288983,0.592541358395569,0.570405227502122,0.548269096608740,0.526132965715326,0.503996834821880,0.481860703928497,0.459724573035019,0.437588442141605,0.415452311248223,0.393316180354776,0.371180049461362,0.349043918567980,0.326907787674534,0.304771656781119,0.282635525887737,0.260499394994259,0.238363264100876,0.216227133207430,0.194091002314016,0.171954871420570,0.149818740527187,0.123200747149645,0.0924005603621983,0.0616003735748407,0.0308001867873497,0;0,0.0278436321635749,0.0633175195416878,0.0987914069196473,0.134265294297658,0.169739181675720,0.205213069053679,0.240686956431690,0.276160843809752,0.311634731187711,0.347108618565824,0.382582505943834,0.418056393321794,0.453530280699856,0.489004168077866,0.524478055455826,0.559951942833888,0.595425830211898,0.630899717589858,0.666373604967971,0.701847492345930,0.737321379723992,0.772795267102002,0.808269154480064,0.843743041858024,0.876799252850355,0.907599439637802,0.938399626425159,0.969199813212650,1];

figure('name','Weights as 3D Plot');
subplot(2, 2, 1);    
plot3([weights(1, :)' weightsMV(1, :)'], [weights(2, :)' weightsMV(2, :)'], [weights(3, :)', weightsMV(3, :)']);
grid on;
xlabel(ticker(1));
ylabel(ticker(2));
zlabel(ticker(3));

subplot(2, 2, 2);
plot([weights(2, :)' weightsMV(2, :)'], [weights(3, :)' weightsMV(3, :)'])
xlabel(ticker(2));
ylabel(ticker(3));
grid on

subplot(2, 2, 3);
plot([weights(1, :)' weightsMV(1, :)'], [weights(3, :)' weightsMV(3, :)'])
xlabel(ticker(1));
ylabel(ticker(3));
grid on

subplot(2, 2, 4);
plot([weights(1, :)' weightsMV(1, :)'], [weights(2, :)' weightsMV(2, :)'])
xlabel(ticker(1));
ylabel(ticker(2));
title('Top');
grid on
legend('TS1', 'TS2', 'Location', 'SouthOutside');

这个链接似乎是一个相对不错的解决方案,但我没有连接到我的许可证服务器来测试它。 - beroe
1个回答

1

我已经成功编写了一个类似于附件的图形的脚本,并尽可能少地使用了代码。图例元素的并排对齐很棘手,你需要外部脚本来实现。我使用了gridLegend,在Matlab Central文件交换中可用:

http://www.mathworks.com/matlabcentral/fileexchange/29248-gridlegend-a-multi-column-format-for-legends

以下是您的代码,经过修改后可以绘制类似于您图片上的图例:
%weights/weightsMV are 3 x 30 matrices
ticker = {'A','B','C'};
weights = [0.764602615068780,0.762329415005434,0.760055503116586,0.757781382654864,0.755508517683302,0.753234375934985,0.750960611173760,0.748686727917457,0.746413866211585,0.744140033854148,0.738367347277555,0.699505907957926,0.660644468638298,0.621783029318668,0.582921589999040,0.544060150679411,0.505198711359782,0.466337272040153,0.427475832720524,0.388614393400895,0.349752954081266,0.310891514761637,0.272030075442009,0.233168636122380,0.194307196802750,0.155445757483122,0.116584318163493,0.0777228788438641,0.0388614395242353,4.10026844681349e-12;0.235397384931220,0.211630038764514,0.187863021792451,0.164096524898550,0.140329000457761,0.116562305396725,0.0927953654491337,0.0690285024746061,0.0452609759124620,0.0214940798746062,0,5.55111512312733e-17,0,5.55111512312746e-17,0,3.70817036264992e-30,4.73852588619852e-30,5.76503212536727e-30,5.38827593281225e-30,2.64267976542398e-18,5.69348240589127e-18,8.74428504635863e-18,1.17950876868260e-17,0,2.75245082116940e-18,1.22032105779319e-17,9.15240793746451e-18,6.10160529699720e-18,6.10160531305980e-18,1.30251232759518e-10;1.10307850379949e-18,0.0260405462300517,0.0520814750909638,0.0781220924465861,0.104162481858937,0.130203318668290,0.156244023377106,0.182284769607937,0.208325157875953,0.234365886271246,0.261632652722445,0.300494092042074,0.339355531361703,0.378216970681332,0.417078410000960,0.455939849320589,0.494801288640218,0.533662727959847,0.572524167279476,0.611385606599105,0.650247045918734,0.689108485238363,0.727969924557991,0.766831363877620,0.805692803197249,0.844554242516878,0.883415681836507,0.922277121156136,0.961138560475765,0.999999999865655]
weightsMV = [0.304769232969962,0.313206616760582,0.299868860275947,0.286531103791370,0.273193347306773,0.259855590822158,0.246517834337581,0.233180077852984,0.219842321368369,0.206504564883792,0.193166808399157,0.179829051914561,0.166491295429983,0.153153538945368,0.139815782460772,0.126478025976194,0.113140269491579,0.0998025130069825,0.0864647565224054,0.0731270000377706,0.0597892435531935,0.0464514870685779,0.0331137305839816,0.0197759740993660,0.00643821761478891,0,0,0,0,0;0.695230767030038,0.658949751075843,0.636813620182365,0.614677489288983,0.592541358395569,0.570405227502122,0.548269096608740,0.526132965715326,0.503996834821880,0.481860703928497,0.459724573035019,0.437588442141605,0.415452311248223,0.393316180354776,0.371180049461362,0.349043918567980,0.326907787674534,0.304771656781119,0.282635525887737,0.260499394994259,0.238363264100876,0.216227133207430,0.194091002314016,0.171954871420570,0.149818740527187,0.123200747149645,0.0924005603621983,0.0616003735748407,0.0308001867873497,0;0,0.0278436321635749,0.0633175195416878,0.0987914069196473,0.134265294297658,0.169739181675720,0.205213069053679,0.240686956431690,0.276160843809752,0.311634731187711,0.347108618565824,0.382582505943834,0.418056393321794,0.453530280699856,0.489004168077866,0.524478055455826,0.559951942833888,0.595425830211898,0.630899717589858,0.666373604967971,0.701847492345930,0.737321379723992,0.772795267102002,0.808269154480064,0.843743041858024,0.876799252850355,0.907599439637802,0.938399626425159,0.969199813212650,1];

figure('name','Weights as 3D Plot');

% s1, s2, s3, s4 - subplot handles
s1 = subplot(2, 2, 1);    
plot3([weights(1, :)' weightsMV(1, :)'], [weights(2, :)' weightsMV(2, :)'], [weights(3, :)', weightsMV(3, :)']);
grid on;
xlabel(ticker(1));
ylabel(ticker(2));
zlabel(ticker(3));

s2 = subplot(2, 2, 2);
plot([weights(2, :)' weightsMV(2, :)'], [weights(3, :)' weightsMV(3, :)'])
xlabel(ticker(2));
ylabel(ticker(3));
grid on

s3 = subplot(2, 2, 3);
plot([weights(1, :)' weightsMV(1, :)'], [weights(3, :)' weightsMV(3, :)'])
xlabel(ticker(1));
ylabel(ticker(3));
grid on

s4 = subplot(2, 2, 4);
% get axes handle, we will need this for legend
ax4 = plot([weights(1, :)' weightsMV(1, :)'], [weights(2, :)' weightsMV(2, :)']);
xlabel(ticker(1));
ylabel(ticker(2));
title('Top');
grid on

hL = gridLegend( ax4, 2, {'TS1', 'TS2'} ,'location','southoutside', 'Orientation','Horizontal');

% 4th subplot needs correction of size and position, due to effects of gridLegend 
s3Pos = get(s3,'position');
s4Pos = get(s4,'position');
s4Pos(2:4) = s3Pos(2:4);
set(s4, 'position', s4Pos);

% manipulate the size and position of legend 
newPosition = [0.35 0.0 0.3 0.05];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接