【问题标题】:How to draw pie charts with text labels inside?如何绘制带有文本标签的饼图?
【发布时间】:2014-05-28 07:26:44
【问题描述】:

如何画饼图,里面有文字标签(最好能放在对应饼图的中心位置)?

为了明确,我想创建这样的东西,目前我必须在图形窗口中手动编辑它。

默认情况下,我只能得到这个(即饼图聊天之外的文本标签):


已编辑:我当前的代码:

%% data
data_raw = [68 58];
data = [100-data_raw' data_raw'];


%% draw here
figure

subplot(1,2,1)
h = pie(data(1, :)); % 2D pie chart
hp = findobj(h, 'Type', 'patch');
set(hp(1),'FaceColor',[165/255 165/255 165/255]);
set(hp(2),'FaceColor',[90/255 155/255 213/255]);

subplot(1,2,2)
h2 = pie(data(2, :)); % 2D pie chart
hp2 = findobj(h2, 'Type', 'patch');
set(hp2(1),'FaceColor',[165/255 165/255 165/255]);
set(hp2(2),'FaceColor',[90/255 155/255 213/255]);

【问题讨论】:

  • 您好,您当前的饼图的代码呢?
  • @matheburg 添加到问题中。感谢您的关注。
  • 自发:mathworks.de/de/help/matlab/creating_plots/… 描述了一种如何操作文本位置的方法,基本上是set(hText,{'Position'},num2cell(textPositions,[3,2]))

标签: matlab drawing matlab-figure pie-chart labels


【解决方案1】:

一种更简单的方法是将当前标签的位置乘以二分之一:

%% data
data_raw = [68 58];
data = [100-data_raw' data_raw'];

%% draw here
figure

subplot(1,2,1)
h1 = pie(data(1, :)); % 2D pie chart
h1(1).FaceColor = [165/255 165/255 165/255];
h1(2).Position = 0.5*h1(2).Position;
h1(3).FaceColor = [90/255 155/255 213/255];
h1(4).Position = 0.5*h1(4).Position;

subplot(1,2,2)
h2 = pie(data(2, :)); % 2D pie chart
h2(1).FaceColor = [165/255 165/255 165/255];
h2(2).Position = 0.5*h2(2).Position;
h2(3).FaceColor = [90/255 155/255 213/255];
h2(4).Position = 0.5*h2(4).Position;

正如上面的代码所示,句柄是成对出现的,即首先是一个补丁(一块饼),然后是一个文本(标签)。因此,对于任意数量的标签,以下将起作用:

%% arbitrary number of slices
data = rand(1,5);
labels = {'One','Two','Three','Four','Five'};
pieHandle = pie(data,labels);

%% reposition labels
for iHandle = 2:2:2*numel(labels)
    pieHandle(iHandle).Position = 0.5*pieHandle(iHandle).Position;
end

这是结果:

【讨论】:

    【解决方案2】:

    根据我的评论,它导致:

    %% data
    data_raw = [68 58];
    data = [100-data_raw' data_raw'];
    
    
    %% draw here
    figure
    
    subplot(1,2,1)
    h = pie(data(1, :)); % 2D pie chart
    hp = findobj(h, 'Type', 'patch');
    set(hp(1),'FaceColor',[165/255 165/255 165/255]);
    set(hp(2),'FaceColor',[90/255 155/255 213/255]);
    
    subplot(1,2,2)
    h2 = pie(data(2, :)); % 2D pie chart
    hp2 = findobj(h2, 'Type', 'patch');
    set(hp2(1),'FaceColor',[165/255 165/255 165/255]);
    set(hp2(2),'FaceColor',[90/255 155/255 213/255]);
    
    hText = findobj(h,'Type','text');
    textPositions_cell = get(hText,{'Position'}); % cell array
    textPositions = cell2mat(textPositions_cell); % numeric array
    textPositions = textPositions * 0.4; % scale position
    set(hText,{'Position'},num2cell(textPositions,[3,2])) % set new position
    
    hText = findobj(h2,'Type','text');
    textPositions_cell = get(hText,{'Position'}); % cell array
    textPositions = cell2mat(textPositions_cell); % numeric array
    textPositions = textPositions * 0.4; % scale position
    set(hText,{'Position'},num2cell(textPositions,[3,2])) % set new position
    

    【讨论】:

    • +1 不错的答案。你能解释一下最后两行是如何把它们放在里面的吗?
    • 不客气 ;) 它只是根据饼图中心为 (0,0) 的假设来缩放位置。当然,你还可以做更多花哨的东西,但乍一看这似乎就足够了。
    • 这里[3,2]是什么意思?
    • 这些是未使用num2cell 拆分数组的维度。由于textPositions 是一个包含饼图位置的 numPieces-times-3 矩阵(最后一维 = 0 通常),因此应始终包含 2。我会用num2cell(textPositions,2)(也可以)替换它,但由于某种原因,文档建议[3,2]。但是,使用 2[3,2] 并没有进行任何相关更改... :)
    猜你喜欢
    • 2019-02-13
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    相关资源
    最近更新 更多