Matlab环形柱状图
数据准备:
名称 数值
Aa 21
Bb 23
Cc 35
Dd 47
保存为Excel文件后:
% Load data from Excel file
filename = 'data.xlsx'; % Ensure the file is in the current folder or provide full path
dataTable = readtable(filename); % Read data from Excel file
% Assuming the first column contains names and second column contains values
Name = dataTable{:, 1}; % Data names from the first column
Data = dataTable{:, 2}; % Data values from the second column
% Number of data points
N = length(Data);
% Select a few colors from the "cool" colormap
coolColors = cool(N); % Get N colors from the "cool" colormap
% Data range and ticks
YLim = [0, 50];
YTick = [];
% =========================================================================
% Start Plotting
if isempty(YLim) || isempty(YTick)
tFig = figure('Visible', 'off');
tAx = axes('Parent', tFig);
tAx.NextPlot = 'add';
bar(tAx, Data)
if isempty(YLim), YLim = tAx.YLim; else, tAx.YLim = YLim; end
if isempty(YTick), YTick = tAx.YTick; end
close(tFig)
end
% Create a figure window
fig = figure('Units', 'normalized', 'Position', [0.2, 0.1, 0.5, 0.8]);
ax = axes('Parent', fig, 'Position', [0, 0, 1, 1]);
ax.NextPlot = 'add';
ax.XColor = 'none';
ax.YColor = 'none';
ax.DataAspectRatio = [1, 1, 1];
% Draw the coordinate axis and ticks
TLim = [pi/2, -pi - pi/6];
t01 = linspace(0, 1, 80);
tT = t01 .* diff(TLim) + TLim(1);
tX = cos(tT) .* (N + N / 2 + 1);
tY = sin(tT) .* (N + N / 2 + 1);
plot(ax, tX, tY, 'LineWidth', .8, 'Color', 'k');
ax.XLim = [-1, 1] .* (N + N / 2 + 2);
ax.YLim = [-1, 1] .* (N + N / 2 + 2);
tT = (YTick - YLim(1)) ./ diff(YLim) .* diff(TLim) + TLim(1);
tX = [cos(tT) .* (N + N / 2 + 1); cos(tT) .* (N + N / 2 + 1 + N / 50); tT .* nan];
tY = [sin(tT) .* (N + N / 2 + 1); sin(tT) .* (N + N / 2 + 1 + N / 50); tT .* nan];
plot(ax, tX(:), tY(:), 'LineWidth', .8, 'Color', 'k');
for i = 1:length(YTick)
iT = tT(i);
iR = iT / pi * 180;
YTickHdl = text(ax, tX(2, i), tY(2, i), num2str(YTick(i)), 'FontName', 'Times New Roman', 'FontSize', 13, 'HorizontalAlignment', 'center');
if mod(iR, 360) > 180 && mod(iR, 360) < 360
YTickHdl.Rotation = iR + 90;
YTickHdl.VerticalAlignment = 'top';
else
YTickHdl.Rotation = iR - 90;
YTickHdl.VerticalAlignment = 'bottom';
end
end
% Plot the bars with colors from the cool colormap
for i = 1:N
tR = [(N + N / 2 + 1 - i - .4) .* ones(1, 80), (N + N / 2 + 1 - i + .4) .* ones(1, 80)];
tT = t01 .* (Data(i) - YLim(1)) ./ diff(YLim) .* diff(TLim) + TLim(1);
tX = cos([tT, tT(end:-1:1)]) .* tR;
tY = sin([tT, tT(end:-1:1)]) .* tR;
fill(ax, tX, tY, coolColors(i, :), 'LineWidth', 1, 'EdgeColor', 'k');
end
% Add data names
for i = 1:N
text(ax, 0, N + N / 2 + 1 - i, [Name{i}, ' '], 'FontName', 'Times New Roman', 'FontSize', 16, 'HorizontalAlignment', 'right');
end