【Matlab绘图】Matlab 多饼图绘制 pie饼图颜色绘制 饼图不同颜色

结果展示

在这里插入图片描述


数据准备

依次准备好每个 饼状图上方的标题,下方的自定义文本,每个扇形图旁边的数据,以及要设置的颜色。

% 数据准备:标题、文本、数据、标签、颜色
name = [1 2 3 4 5];
texts = {'Blue(6)','Blue(5)-Red(1)','Blue(4)-Red(2)','Blue(3)-Red(3)','Red(6)'};
data = {
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6]
};
labels = {
    {'16.67%', ' ', ' ',' ',' ',' '},
    {'16.67%', '16.67%', ' ', ' ',' ',' '},
    {'33.33%', ' ', ' 16.67%', ' ',' ',' '},
    {'50%', ' ', ' ', ' 16.67%',' ',' '},
    {'16.67%', ' ', ' ',' ',' ',' '}
};
colors = {
    [1 0 0; 0 0 1; 0 0 1; 0 0 1; 0 0 1; 0 0 1],
};

五个饼图绘制

将每个饼图设置为子图就可以正确实现

figure('Position', [100 100 1500 500]); % x, y, width, height
for i = 1:5
    % 绘图
    subplot(1,5,i);
    colormap(colors{1});
    pie(data{i}, labels{i});
    title(['Scale of the fan chart: ', num2str(name(i))],'FontSize', 16);

    %------循环未结束
end

在这里插入图片描述


下方文本设置

得到当前坐标轴的值,然后计算文本应该放置的位置

figure('Position', [100 100 1500 500]); % x, y, width, height
for i = 1:5
    % 绘图
    subplot(1,5,i);
    colormap(colors{1});
    pie(data{i}, labels{i});
    title(['Scale of the fan chart: ', num2str(name(i))],'FontSize', 16);

    %------下方文本设置
    ax = gca;
    xlims = ax.XLim;
    ylims = ax.YLim;
    x_center = mean(xlims);
    text_pos_y = ylims(1) + (ylims(2) - ylims(1)) * 0.05 - 0.25; % 从底部向上偏移5%
    text(x_center, text_pos_y, texts{i}, 'HorizontalAlignment', 'center', 'FontSize', 12);
    %------循环未结束
end

在这里插入图片描述

图例设置

为了将图例放到正中央,在第三个饼图设置图例即可

for i = 1:5
    % 绘图
    subplot(1,5,i);
    colormap(colors{1});
    pie(data{i}, labels{i});
    title(['Scale of the fan chart: ', num2str(name(i))],'FontSize', 16);

    % 计算文本放置的位置
    ax = gca;
    xlims = ax.XLim;
    ylims = ax.YLim;
    x_center = mean(xlims);
    text_pos_y = ylims(1) + (ylims(2) - ylims(1)) * 0.05 - 0.25; % 从底部向上偏移5%
    text(x_center, text_pos_y, texts{i}, 'HorizontalAlignment', 'center', 'FontSize', 12);

    % 添加 legend 只有在 i=3 的时候
    if i == 3
        legend('MS\_B','','MS\_N','Orientation','horizontal',...
               'FontSize',16,'Location','best');
    end
    
    %-----循环未结束
end

在这里插入图片描述

大小设置

为了实现从左到右饼图大小依次增大,这里设置axis坐标轴的大小。设置的scale尺寸需要从大到小,这是因为在MATLAB中使用axis函数来设置坐标轴的显示范围时,你所观察到的现象是由于axis函数如何定义坐标轴的显示区域造成的。当你在绘制一个饼图(或任何图表)后,使用axis([-scales(i), scales(i), -scales(i), scales(i)]),你实际上是在定义x轴和y轴的最小值和最大值。

对于一个饼图来说,它通常会自动适应坐标轴的范围,以便完整地显示所有的扇形部分。因此,如果你给axis一个更大的范围,那么整个饼图看起来就会相对于坐标轴变小,因为它需要适应这个更大的空间。反之,如果axis的范围被设置得更小,饼图为了适应这个更小的空间,其视觉效果上会显得更大。

在你的例子中,scales数组中的值较小,意味着你正在告诉MATLAB使用一个相对较小的坐标轴范围。这会导致饼图看起来更大,因为它必须完全适应在这个小范围内。

例如,如果你有一个scales值为1,那么axis([-1, 1, -1, 1])将会设定一个非常小的显示区域,饼图必须在这个区域内完全显示,因此它会显得比默认情况下大。而如果scales值为10,那么axis([-10, 10, -10, 10])将创建一个很大的显示区域,饼图在这个大区域内会显得相对较小。

所以,如果你想通过改变axis的范围来放大或缩小饼图的视觉效果,你确实可以通过减小或增加axis的范围来实现这一点。但是要注意,过小的范围可能会导致饼图的某些部分被裁剪掉,因为饼图的直径可能超出了你所设定的范围。同样,过大的范围虽然不会裁剪掉任何部分,但可能会让饼图看起来过于微小且不明显。

scales = [5 4 3 2 1]; %坐标轴比例因子反向设置才是从左到右依次增大
for i = 1:5
    % 绘图
    subplot(1,5,i);
    colormap(colors{1});
    pie(data{i}, labels{i});
    title(['Scale of the fan chart: ', num2str(name(i))],'FontSize', 16);

    % 计算文本放置的位置
    ax = gca;
    xlims = ax.XLim;
    ylims = ax.YLim;
    x_center = mean(xlims);
    text_pos_y = ylims(1) + (ylims(2) - ylims(1)) * 0.05 - 0.25; % 从底部向上偏移5%
    text(x_center, text_pos_y, texts{i}, 'HorizontalAlignment', 'center', 'FontSize', 12);

    % 添加 legend 只有在 i=3 的时候
    if i == 3
        legend('MS\_B','','MS\_N','Orientation','horizontal',...
               'FontSize',16,'Location','best');
    end
    
    % 设置坐标轴范围以调整饼状图大小
    axis([-scales(i), scales(i), -scales(i), scales(i)]);
    
    
    % saveas(gcf, [num2str(i), '.png']);
end

在这里插入图片描述

完整代码

这样代码部分就设置完成了,我本来想给每个饼图设置一个颜色的,但是由于colormap会将每个饼图特别设定的颜色给覆盖,所以只能生成figure图窗之后单独给每个扇形单独设置颜色,后期更改颜色,得到的就是如文章顶部的图片。

完整代码如下所示:

% 比例因子
scales = [5 4 3 2 1]; %坐标轴比例因子反向设置才是从左到右依次增大
name = [1 2 3 4 5];
texts = {'Blue(6)','Blue(5)-Red(1)','Blue(4)-Red(2)','Blue(3)-Red(3)','Red(6)'};
data = {
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6],
    [1/6 1/6 1/6 1/6 1/6 1/6]
};
labels = {
    {'16.67%', ' ', ' ',' ',' ',' '},
    {'16.67%', '16.67%', ' ', ' ',' ',' '},
    {'33.33%', ' ', ' 16.67%', ' ',' ',' '},
    {'50%', ' ', ' ', ' 16.67%',' ',' '},
    {'16.67%', ' ', ' ',' ',' ',' '}
};
colors = {
    [1 0 0; 0 0 1; 0 0 1; 0 0 1; 0 0 1; 0 0 1],
};

figure('Position', [100 100 1500 500]); % x, y, width, height

for i = 1:5
    % 绘图
    subplot(1,5,i);
    colormap(colors{1});
    pie(data{i}, labels{i});
    title(['Scale of the fan chart: ', num2str(name(i))],'FontSize', 16);

    % 计算文本放置的位置
    ax = gca;
    xlims = ax.XLim;
    ylims = ax.YLim;
    x_center = mean(xlims);
    text_pos_y = ylims(1) + (ylims(2) - ylims(1)) * 0.05 - 0.25; % 从底部向上偏移5%
    text(x_center, text_pos_y, texts{i}, 'HorizontalAlignment', 'center', 'FontSize', 12);
    
    % 设置坐标轴范围以调整饼状图大小
    axis([-scales(i), scales(i), -scales(i), scales(i)]);
    
    % 添加 legend 只有在 i=3 的时候
    if i == 3
        legend('MS\_B','','MS\_N','Orientation','horizontal',...
               'FontSize',16,'Location','best');
    end
    % saveas(gcf, [num2str(i), '.png']);
end

在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lydia.na

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值