基于Matlab绘制新式饼状图

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法  神经网络预测 雷达通信  无线传感器

信号处理 图像处理 路径规划 元胞自动机 无人机  电力系统

⛄ 内容介绍

基于Matlab绘制新式饼状图

⛄ 完整代码

% Example 1: Nested pie chart

close all

% Initialize data points

inner_pie = [0.1, 0.15, 0.2, 0.05, 0.3, 0.2];

outer_pie = [0.25, 0.25, 0.5];

C = {...

    inner_pie,... % Inner pie must come first!

    outer_pie};

% Spider plot

pie_nested(C);

% Title

title('Nested Pie Chart');

% Legend properties

legend_str = cell(length(inner_pie), 1);

for ii = 1:length(legend_str)

    inner_value = inner_pie(ii);

    legend_str{ii} = sprintf('Inner Pie #%i: %.1f%%', ii, inner_value*100);

end

lg =legend(legend_str, 'Location', 'southoutside');

lg.Title.String = 'Inner Pie';

function varargout = pie_nested(C, varargin)

%pie_nested Create a nested pie chart.

%

% Syntax:

%   pie_nested(C)

%   pie_nested(C, Name, Value, ...)

%   h = pie_nested(_)

%

% Documentation:

%   Please refer to the MathWorks File Exchange or GitHub page for the

%   detailed documentation and examples.

% Figure properties

fig = figure;

background_color = 'w';

fig.Color = background_color;

% Check for output arguments

if nargout > 1

    error('Error: Too many output arguments assigned.');

end

varargout{1} = fig;

% Axes properties

ax = gca;

ax.Color = background_color;

% Axis properties

hold(ax, 'on');

axis(ax, 'square');

scaling_factor = 1.2;

axis(ax, [-1, 1, -1, 1] * scaling_factor);

% Axis properties

ax.XTickLabel = [];

ax.YTickLabel = [];

ax.XColor = 'none';

ax.YColor = 'none';

% Pie properties

num_pie = length(C);

num_outmost = length(C{end});

pie_labels = cell(num_outmost, 1);

% Number of wedges

num_wedges = cellfun(@(C) length(C), C, 'UniformOutput', false);

num_wedges = cell2mat(num_wedges);

max_wedges = max(num_wedges);

% Default labels

for ii = 1:num_outmost

    pie_labels{ii} = sprintf('Label %i', ii);

end

% Pre-allocation

wedge_colors = cell(num_pie, 1);

percent_status = cell(num_pie, 1);

% Default arguments

rho_lower = 0.2;

edge_color = 'k';

line_width = 2;

line_style = '-';

wedge_colors(:) = {lines(max_wedges)};

percent_status(:) = {'on'};

percent_precision = 1;

percent_fontcolor = 'w';

percent_fontsize = 10;

percent_fontweight = 'bold';

percent_edgecolor = 'none';

percent_backgroundcolor = 'none';

fill_transparency = 1;

label_fontsize = 10;

label_fontcolor = 'k';

label_fontweight = 'bold';

label_edgecolor = 'none';

label_backgroundcolor = 'none';

label_rotation = 0;

label_offset = 0;

label_interpreter = 'none';

interval_res = 0.01;

% Number of optional arguments

numvarargs = length(varargin);

% Check if optional arguments were specified

if numvarargs > 1

    % Initialze name-value arguments

    name_arguments = varargin(1:2:end);

    value_arguments = varargin(2:2:end);

    % Iterate through name-value arguments

    for ii = 1:length(name_arguments)

        % Set value arguments depending on name

        switch lower(name_arguments{ii})

            case 'rholower'

                rho_lower = value_arguments{ii};

            case 'edgecolor'

                edge_color = value_arguments{ii};

            case 'linewidth'

                line_width = value_arguments{ii};

            case 'linestyle'

                line_style = value_arguments{ii};

            case 'wedgecolors'

                wedge_colors = value_arguments{ii};

            case 'percentstatus'

                percent_status = value_arguments{ii};

            case 'percentprecision'

                percent_precision = value_arguments{ii};

            case 'percentfontcolor'

                percent_fontcolor = value_arguments{ii};

            case 'percentfontsize'

                percent_fontsize = value_arguments{ii};

            case 'percentfontweight'

                percent_fontweight = value_arguments{ii};

            case 'percentedgecolor'

                percent_edgecolor = value_arguments{ii};

            case 'percentbackgroundcolor'

                percent_backgroundcolor = value_arguments{ii};

            case 'labelfontsize'

                label_fontsize = value_arguments{ii};

            case 'labelfontcolor'

                label_fontcolor = value_arguments{ii};

            case 'labelfontweight'

                label_fontweight = value_arguments{ii};

            case 'labeledgecolor'

                label_edgecolor = value_arguments{ii};

            case 'labelbackgroundcolor'

                label_backgroundcolor = value_arguments{ii};

            case 'labelrotation'

                label_rotation = value_arguments{ii};

            case 'labelinterpreter'

                label_interpreter = value_arguments{ii};

            case 'filltransparency'

                fill_transparency = value_arguments{ii};

            otherwise

                error('Error: Please enter in a valid name-value pair.');

        end

    end

end

% Initialize rho

rho_upper = 1;

rho_range = rho_upper - rho_lower;

rho_interval = rho_range/num_pie;

rho = rho_lower:rho_interval:rho_upper;

% Iterate through number of nested pies

for ii = 1:num_pie

    % Initialize

    sub_pie = C{ii}; % Convert from cell to numerical array

    num_wedge = num_wedges(ii);

    wedge_color = wedge_colors{ii};

    % Check if data does not sum to one

    if sum(sub_pie) ~= 1

        % Display warning

        warning('Data does not sum to 1. Attempting to normalize data...');

        % Normalize data to add up to one

        sub_pie = sub_pie / sum(sub_pie);

    end

    % Initialize theta

    theta = 2 * pi * sub_pie;

    theta = cumsum(theta);

    theta = [0, theta]; %#ok<*AGROW>

    % Iterate through number of wedges

    for jj = 1:num_wedge

        % Initialize

        theta_start = theta(jj);

        theta_end = theta(jj+1);

        rho_start = rho(ii);

        rho_end = rho(ii+1);

        % Make cyclic

        theta_sides = [theta_start, theta_end, theta_end, theta_start, theta_start];

        rho_sides = [rho_start, rho_start, rho_end, rho_end, rho_start];

        % Initialize

        x_patch = [];

        y_patch = [];

        % Iterate through a four-sided polygon

        for kk = 1:4

            % Initialize

            interval = interval_res;

            % Check if interval needs to be flipped

            if theta_sides(kk) > theta_sides(kk+1)

                interval = -interval_res;

            end

            % Polar coordinates of polygon

            theta_side = theta_sides(kk):interval:theta_sides(kk+1);

            rho_side = rho_sides(kk) * ones(1, length(theta_side));

            % Convert from polar to cartesian coordinates

            [x_cart, y_cart] = pol2cart(theta_side, rho_side);

            % Append to array

            x_patch = [x_patch, x_cart];

            y_patch = [y_patch, y_cart];

        end

        % Create patch object

        patch(x_patch, y_patch, wedge_color(jj, :),...

            'LineStyle', line_style,...

            'EdgeColor', edge_color,...

            'LineWidth', line_width,...

            'FaceAlpha', fill_transparency);

    end

    % Find midpoint of theta and rho

    theta_diff = diff(theta)/2;

    theta_diff = theta(1:end-1) + theta_diff;

    rho_wedge = rho(ii:ii+1);

    rho_delta = diff(rho_wedge)/2;

    rho_diff = rho_wedge(1:end-1) + rho_delta;

    rho_labels = rho_wedge(end) + rho_delta;

    % Iterate through rho midpoints

    for jj = 1:length(rho_diff)

        % Initialize

        rho_txt = rho_diff(jj);

        rho_label = rho_labels(jj);

        % Iterate through theta midpoints

        for kk = 1:length(theta_diff)

            % Initialize

            theta_txt = theta_diff(kk);

            % Check if display percent status

            if strcmp(percent_status{ii}, 'on')

                % Format percentage text

                precision_txt = sprintf('%i', percent_precision);

                percent_txt = sprintf(['%.', precision_txt, 'f%%'], sub_pie(kk)*100);

                % Convert from polar to cartesian

                [x_txt, y_txt] = pol2cart(theta_txt, rho_txt);

                % Display percentage text

                text(x_txt, y_txt, percent_txt,...

                    'Color', percent_fontcolor,...

                    'FontWeight', percent_fontweight,...

                    'FontSize', percent_fontsize,...

                    'EdgeColor', percent_edgecolor,...

                    'BackgroundColor', percent_backgroundcolor,...

                    'HorizontalAlignment', 'center');

            end

            % Only for outermost

            if ii == num_pie

                % Convert from polar to cartesian

                [x_label, y_label] = pol2cart(theta_txt, rho_label+label_offset);

                [horz_align, vert_align] = quadrant_position(theta_txt);

                % Display pie labels

                text(x_label, y_label, pie_labels{kk},...

                    'Color', label_fontcolor,...

                    'FontWeight', label_fontweight,...

                    'FontSize', label_fontsize,...

                    'EdgeColor', label_edgecolor,...

                    'BackgroundColor', label_backgroundcolor,...

                    'Rotation', label_rotation,...

                    'HorizontalAlignment', horz_align,...

                    'VerticalAlignment', vert_align,...

                    'Interpreter', label_interpreter);

            end

        end

    end

end

    function [horz_align, vert_align] = quadrant_position(theta_point)

        % Find out which quadrant the point is in

        if theta_point == 0

            quadrant = 0;

        elseif theta_point == pi/2

            quadrant = 1.5;

        elseif theta_point == pi

            quadrant = 2.5;

        elseif theta_point == 3*pi/2

            quadrant = 3.5;

        elseif theta_point == 2*pi

            quadrant = 0;

        elseif theta_point > 0 && theta_point < pi/2

            quadrant = 1;

        elseif theta_point > pi/2 && theta_point < pi

            quadrant = 2;

        elseif theta_point > pi && theta_point < 3*pi/2

            quadrant = 3;

        elseif theta_point > 3*pi/2 && theta_point < 2*pi

            quadrant = 4;

        end

        % Adjust label alignment depending on quadrant

        switch quadrant

            case 0

                horz_align = 'left';

                vert_align = 'middle';

            case 1

                horz_align = 'left';

                vert_align = 'bottom';

            case 1.5

                horz_align = 'center';

                vert_align = 'bottom';

            case 2

                horz_align = 'right';

                vert_align = 'bottom';

            case 2.5

                horz_align = 'right';

                vert_align = 'middle';

            case 3

                horz_align = 'right';

                vert_align = 'top';

            case 3.5

                horz_align = 'center';

                vert_align = 'top';

            case 4

                horz_align = 'left';

                vert_align = 'top';

        end

    end

end​

⛄ 运行结果

⛄ 参考文献

⛄ 完整代码

❤️部分理论引用网络文献,若有侵权联系博主删除

❤️ 关注我领取海量matlab电子书和数学建模资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

matlab科研助手

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

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

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

打赏作者

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

抵扣说明:

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

余额充值