Matlab;饼图带有2个以上/拆分图例R2017b [英] Matlab; Pie chart with 2+ / split legends R2017b

查看:246
本文介绍了Matlab;饼图带有2个以上/拆分图例R2017b的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个饼图,理想情况下是希望图例在顶部和/或底部水平显示.但是,在几乎所有情况下,这都是不可能的,因为图例会消失.因此,理想情况下,我想将图例分为两个(或多个)子图例,并将其分别放置.我知道这不是MATLAB的内置功能(我正在使用R2017b),但不确定是否可以使它正常工作?我见过一些人设法用折线图做类似的事情,但我无法使其适应于饼图.

I'm creating a pie chart and ideally want the legend to be displayed horizontally at the top and/or bottom. However, in almost all cases this isn't possible as the legend go off the figure. Therefore, I'd ideally like to split the legend into two (or more) sub-legends and place them individually. I'm aware that this isn't a built-in feature in MATLAB (I'm using R2017b) but I'm not sure if it's something that can be bodged to work? I've seen a few people manage to do similar things with line plots but I've not been able to adapt them to work with my pie charts.

示例代码:

% Set up a figure and make it a reasonable size/location.
figure( 1 )
set( gcf, 'Position', [ 350, 150, 750, 750 ] )

% Create a list of items for the food menu (example only).
Menu = { "Egg and Bacon", "Egg, Sausage and becon", "Egg and Spam", ...
         "Egg, bacon and Spam", "Egg, bacon, sausage and Spam",     ...
         "Spam, bacon, sausage and Spam", "Nothing"                    };

% Estimate the demand for said food items (example only).
Orders = randi( 150, 1, length( Menu ) );

% Make a pie chart showing what ratio the food was ordered.
Pie_Plot = pie( Orders );

% Create two ranges to grab the first and second half of the pie chart's 
% patches.
Range_1 =                  1 : 2 : ceil( length( Pie_Plot ) / 2 );
Range_2 = Range_1( end ) + 2 : 2 : length( Pie_Plot );

% In an ideal world this would be the first of two legends that would 
% display at the same time.
Ideal_Leg_Pt1 = legend( Pie_Plot( Range_1 ), ...
        Menu( round( Range_1 / 2 ) ), 'orientation', 'horizontal', ...
        'location', 'southoutside'                                    );

% A pause because the method doesn't work so without it, this legend 
% won't appear.                         
pause

% The second half of the ideal legend(s) solution; noting that when this 
% is created, the original
% legend is replaced.
Ideal_Leg_Pt2 = legend( Pie_Plot( Range_2 ), ...
        Menu( round( Range_2 / 2) ), 'orientation', 'horizontal', ...
        'location', 'northoutside'                                   );

% Pause for the same reasons as before.
pause

% This is what I'm currently stuck with; a legend that doesn't fit (I'm 
% aware I could make it vertical for example but this looks messy in my 
% eyes and I'm trying to avoid it unless there really is no way to make 
% the ideal method work).
Current_Leg = legend( Menu, 'orientation', 'horizontal', ...
        'location', 'northoutside'                          );

编辑:

这已被标记为可能重复,但是我认为不是(但是我可能错了).我已经看过与之链接的解决方案,但是这些解决方案在我的OP中通常是指类似但又无法适应饼图的事物.我可以接近(例如, zhqiat 的方法),但是我不能完全使它适用于派图表.

This has been marked as a possible duplicate, but I don't think that it is (I could be wrong, however). I've looked at the solutions that have been linked to, but they're mostly what I referred to in my OP as things that are similar but which I've been unable to adapt to work with a pie chart. I can get close with (for example, the method by zhqiat) but I can't quite make it work for a pie chart.

在上述示例中,它通过绘制一些零件,创建一个新轴然后绘制其余部分来工作;但是您无法使用饼图来做到这一点.我可以解决这个问题,但是最后我得到两个饼图,它们不能完全重叠.这就是为什么我不认为这是重复的问题的原因.饼形图似乎与常规图有本质上的区别,因此,许多似乎适用于常规线形图的解决方案似乎不适用于饼图(但是,我可以自由地承认,我只是忽略了一个简单的修改即可使它们全部起作用! ).

In the aforementioned example, it works by plotting some parts, creating a new axis then plotting the rest; but you can't do that with a pie chart. I can get close to solving the issue, but I end up with two pie charts that don't overlay perfectly. This is the heart of why I don't believe this is a duplicate issue; pie charts seems inherently different than regular plots and so many solutions that seem to work for regular line plots don't seem to work for pie charts (however, I freely admit I could just be overlooking a simple modification that would make them all work!).

上述示例的代码(直接放置在我的OP中的Ideal_Leg_Pt1下,其余所有内容都删除了)

Code for said example (placed directly under Ideal_Leg_Pt1 in my OP, with everything else after removed):

ax2 = axes('Position',get(gca,'Position'),...
           'Visible','off','Color','none');

Second_Pie = pie( Orders );

Ideal_Leg_Pt2 = legend( Second_Pie( Range_2 ), ...
        Menu( round( Range_2 / 2) ), 'orientation', 'horizontal', ...
        'location', 'northoutside' );

推荐答案

您说您在2018a(2017b)之前具有Matlab版本,因此不能应用

You said you have a version of Matlab prior to 2018a (2017b), hence you can't apply @Sardar's answer. So here is a way to do that without the NumColumns property:

我们从代码的第一部分(与我的2017a兼容的版本)开始,在其中创建派,并放置图例的前半部分:

We start with the first part of your code (in a version that compatible with my 2017a), where you create the pie, and place the first half of the legend:

figure(1);
set(gcf,'Position',[350,150,750,750])
% Create a list of items for the food menu (example only).
Menu = {'Egg and Bacon', 'Egg, Sausage and becon', 'Egg and Spam', ...
         'Egg, bacon and Spam', 'Egg, bacon, sausage and Spam',     ...
         'Spam, bacon, sausage and Spam', 'Nothing'};
% Estimate the demand for said food items (example only).
Orders = randi(150,1,length(Menu));
% Make a pie chart showing what ratio the food was ordered.
Pie_Plot = pie(Orders);
% Create two ranges to grab the first and second half of the pie chart's 
% patches.
Range_1 = 1:2:ceil(length(Pie_Plot)/2);
Range_2 = Range_1(end)+2:2:length(Pie_Plot);

% In an ideal world this would be the first of two legends that would 
% display at the same time:
Ideal_Leg_Pt1 = legend(Pie_Plot(Range_1),Menu(round(Range_1/2)),...
    'orientation', 'horizontal','EdgeColor','none');

接下来,我们将手动设置Ideal_Leg_Pt1位置以获取一些空间:

Next, we'll set the Ideal_Leg_Pt1 position manually to gain some space:

Ideal_Leg_Pt1.Position(1:3) = [0 1-Ideal_Leg_Pt1.Position(4) 1];

现在,我们添加具有相同饼图的隐藏轴,但是这次添加图例的第二部分:

Now we add hidden axes with the same pie, but this time with the second part of the legend:

% the hidden axes are neded so the first legend won't be replaced:
hidden_ax = axes('Visible','off','NextPlot','add');
% we plot the same pie, only to create the objects in the legend:
Pie_Plot_2 = pie(Orders);
% The second half of the ideal legend(s) solution:
Ideal_Leg_Pt2 = legend(Pie_Plot_2(Range_2),Menu(round(Range_2/2)),...
    'orientation', 'horizontal','AutoUpdate','off',...
    'EdgeColor','none');

请注意,我们设置了Ideal_Leg_Pt2的"nofollow noreferrer> 'AutoUpdate'属性设置为关闭",因此我们可以安全地删除虚拟派,而无需从图例中删除项目:

Note, that we set the 'AutoUpdate' property of the Ideal_Leg_Pt2 to 'off', so we can safely delete the dummy pie without removing items from the legend:

% remove the extra dummy pie:
Pie_Plot_2.delete

现在我们可以相对于Ideal_Leg_Pt1的位置设置Ideal_Leg_Pt2的位置,以使它们看起来像一个图例:

Now we can set the position of Ideal_Leg_Pt2 relative to the position of Ideal_Leg_Pt1, to make them look as one legend:

% Define a position of the second legend to fit the first one:
% make the legend equally wide
Ideal_Leg_Pt2.Position([1 3]) = Ideal_Leg_Pt1.Position([1 3]);
% attach the two parts of the leggend
Ideal_Leg_Pt2.Position(2) = Ideal_Leg_Pt1.Position(2)-Ideal_Leg_Pt2.Position(4);

结果:

请注意,对图形进行任何大小调整都需要重新设置图例,因此,如果出现此问题,您可以设置图形的'SizeChangedFcn'属性来为您完成此操作:

Note that any resize of the figure will need a reposition of the legends, so if this a problem you can set 'SizeChangedFcn' property of the figure to do that for you:

% updating the position of the legends upon resizing of the figure:
set(figure(1),'SizeChangedFcn',...
    ['Ideal_Leg_Pt1.Position(1:3) = [0 1-Ideal_Leg_Pt1.Position(4) 1];'...
     'Ideal_Leg_Pt2.Position([1 3]) = Ideal_Leg_Pt1.Position([1 3]);'...
     'Ideal_Leg_Pt2.Position(2) = Ideal_Leg_Pt1.Position(2)-Ideal_Leg_Pt2.Position(4);']);

或设置一个小功能来将其保存在其他M文件中:

or set a small function to do so, save it in different M-file:

function setLgePos(Ideal_Leg_Pt1,Ideal_Leg_Pt2)
Ideal_Leg_Pt1.Position(1:3) = [0 1-Ideal_Leg_Pt1.Position(4) 1];
Ideal_Leg_Pt2.Position([1 3]) = Ideal_Leg_Pt1.Position([1 3]);
Ideal_Leg_Pt2.Position(2) = Ideal_Leg_Pt1.Position(2)-Ideal_Leg_Pt2.Position(4);
end

并从Figure属性中调用它:

and call it from the figure property:

set(figure(1),'SizeChangedFcn','setLgePos(Ideal_Leg_Pt1,Ideal_Leg_Pt2)');

这篇关于Matlab;饼图带有2个以上/拆分图例R2017b的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆