我想在子图的底部添加一个图例(2 x 2):

正如您所看到的,由于我手动调整了第二行中的图表,因此被挤压了一点。

是否有像 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');

最佳答案

我已经设法用尽可能少的代码编写了一个与附件相似的脚本。图例元素的并排对齐很棘手,您将需要外部脚本。我使用了 gridLegend,可在 Matlab Central File Exchange 上获得:

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);

关于legend - Matlab : subplot with sublegend,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22490033/

10-12 18:45