为条形图和线条绘制图例
Plot a legend for both a barplot and a line
如果我有一个条形图,然后在同一个图上有一条线,我该如何为两者写一个图例?我有
hold on
h1 = bar([x;y], 0.5);
h2 = plot(a, b);
l = cell(2,1);
l{1,1}='Label for x';
l{2,1}='Label for y';
hl=legend(h1, l);
set(hl,'FontSize',10,'Location','Northeast', 'Orientation', 'horizontal');
这会生成条形图的图例,但如何为折线图添加图例条目?
一种方法:
legend({'label for x','label for y'},'FontSize',10,'Location','Northeast','Orientation','vertical');
如果你想用图例条目保留你的单元格对象,你可以使用
l = cell(2,1);
l{1,1}='Label for x';
l{2,1}='Label for y';
legend(l,'FontSize',10,'Location','Northeast','Orientation', 'vertical');
您只将 bar
绘图句柄传递给了 legend
函数,这就是它要创建的全部内容。但是,您可以做的是将句柄(和标签)的 数组 传递给 legend
,然后将显示句柄数组的所有元素的图例条目。
h1 = bar([x;y], 0.5);
h2 = plot(a, b);
labels = {'Label for x', 'Label for y'};
L = legend([h1, h2], labels, ...
'FontSize', 10, ...
'Location', 'Northeast', ...
'Orientation', 'horizontal');
如果我有一个条形图,然后在同一个图上有一条线,我该如何为两者写一个图例?我有
hold on
h1 = bar([x;y], 0.5);
h2 = plot(a, b);
l = cell(2,1);
l{1,1}='Label for x';
l{2,1}='Label for y';
hl=legend(h1, l);
set(hl,'FontSize',10,'Location','Northeast', 'Orientation', 'horizontal');
这会生成条形图的图例,但如何为折线图添加图例条目?
一种方法:
legend({'label for x','label for y'},'FontSize',10,'Location','Northeast','Orientation','vertical');
如果你想用图例条目保留你的单元格对象,你可以使用
l = cell(2,1);
l{1,1}='Label for x';
l{2,1}='Label for y';
legend(l,'FontSize',10,'Location','Northeast','Orientation', 'vertical');
您只将 bar
绘图句柄传递给了 legend
函数,这就是它要创建的全部内容。但是,您可以做的是将句柄(和标签)的 数组 传递给 legend
,然后将显示句柄数组的所有元素的图例条目。
h1 = bar([x;y], 0.5);
h2 = plot(a, b);
labels = {'Label for x', 'Label for y'};
L = legend([h1, h2], labels, ...
'FontSize', 10, ...
'Location', 'Northeast', ...
'Orientation', 'horizontal');