Hey, 各位又是好久不见,最近忙到后台消息都有些来不及看,非常抱歉,今天带来一个环形柱状图绘制的简易小代码,绘制效果如下:
下面直接给出完整代码,替换一下数据即可,代码都有注释的:
完整代码
环形柱状图
% author : slandarer% 生成随机数据
rng(13)
Data = randi([1,100], [6,1]);% 数据名称
% 'Class-1','Class-2',... ...,'Class-6'
Name = compose('Class-%d', 1:6);% 配色
% 更多配色详见 Zhaoxu Liu / slandarer (2024).
% 2000 palettes (https://www.mathworks.com/matlabcentral/fileexchange/126969-2000-palettes),
% MATLAB Central File Exchange.
CList = [0.8941 0.8706 0.80780.9255 0.7412 0.58430.6078 0.6941 0.73330.4745 0.6745 0.74120.2039 0.3961 0.45880.0431 0.2588 0.1294];
% CList = colorDemo(11)% 数据展示范围及刻度
YLim = [0,100];
YTick = [];% =========================================================================
% 开始绘图
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; endif isempty(YTick), YTick = tAx.YTick; endclose(tFig)
end
% 创建图窗
fig = figure('Units','normalized', 'Position',[.2,.1,.5,.8]);
ax = axes('Parent',fig, 'Position',[0,0,1,1]);
ax.NextPlot = 'add';
ax.XColor = 'none';
ax.YColor = 'none';
ax.DataAspectRatio = [1,1,1];
% 绘制坐标轴和刻度线
TLim = [pi/2, -pi - pi/6];
t01 = linspace(0, 1, 80);
N = length(Data);
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) < 360YTickHdl.Rotation = iR + 90;YTickHdl.VerticalAlignment = 'top';elseYTickHdl.Rotation = iR - 90;YTickHdl.VerticalAlignment = 'bottom';end
end
% 绘制柱状图
for i = 1:NtR = [(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, CList(i,:), 'LineWidth',1, 'EdgeColor','k')
end
% 绘制数据名称
for i = 1:Ntext(ax, 0, N + N/2 + 1 - i, [Name{i},' '], 'FontName','Times New Roman',...'FontSize',16, 'HorizontalAlignment','right');
end
堆叠环形柱状图
clc; clear
% author : slandarer% 生成随机数据
rng(6)
Data = randi([1,100], [6,5]);% 数据名称
% 'Class-1','Class-2',... ...,'Class-6'
Name1 = compose('Class-%d', 1:6);
Name2 = compose('Prop-%d', 1:5);% 配色
% 更多配色详见 Zhaoxu Liu / slandarer (2024).
% 2000 palettes (https://www.mathworks.com/matlabcentral/fileexchange/126969-2000-palettes),
% MATLAB Central File Exchange.
CList = [0.8941 0.8706 0.80780.9255 0.7412 0.58430.6078 0.6941 0.73330.4745 0.6745 0.74120.2039 0.3961 0.45880.0431 0.2588 0.1294];
% CList = colorDemo(11)% 数据展示范围及刻度
YLim = [];
YTick = [];% =========================================================================
% 开始绘图
if isempty(YLim) || isempty(YTick)tFig = figure('Visible', 'off');tAx = axes('Parent',tFig);tAx.NextPlot = 'add';bar(tAx, Data, 'stacked')if isempty(YLim), YLim = tAx.YLim; else, tAx.YLim = YLim; endif isempty(YTick), YTick = tAx.YTick; endclose(tFig)
end
% 创建图窗
fig = figure('Units','normalized', 'Position',[.2,.1,.5,.8]);
ax = axes('Parent',fig, 'Position',[0,0,1,1]);
ax.NextPlot = 'add';
ax.XColor = 'none';
ax.YColor = 'none';
ax.DataAspectRatio = [1,1,1];
% 绘制坐标轴和刻度线
TLim = [pi/2, -pi - pi/6];
t01 = linspace(0, 1, 80);
N = size(Data, 1);
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) < 360YTickHdl.Rotation = iR + 90;YTickHdl.VerticalAlignment = 'top';elseYTickHdl.Rotation = iR - 90;YTickHdl.VerticalAlignment = 'bottom';end
endData = cumsum([zeros(N, 1), Data], 2);
% 绘制柱状图
for i = 1:Nfor j = 1:(size(Data, 2) - 1)tR = [(N + N/2 + 1 - i - .4).*ones(1, 80), (N + N/2 + 1 - i + .4).*ones(1, 80)];tT = (t01.*(Data(i, j + 1) - Data(i, j)) + Data(i, j) - YLim(1))./diff(YLim).*diff(TLim) + TLim(1);tX = cos([tT, tT(end:-1:1)]).*tR;tY = sin([tT, tT(end:-1:1)]).*tR;tHdl = fill(ax, tX, tY, CList(j,:), 'LineWidth',1, 'EdgeColor','k');if i == 1lgdHdl(j) = tHdl;endend
end
% 绘制数据名称
for i = 1:Ntext(ax, 0, N + N/2 + 1 - i, [Name1{i},' '], 'FontName','Times New Roman',...'FontSize',16, 'HorizontalAlignment','right');
end
% 绘制图例
legend(lgdHdl, Name2, 'FontName','Times New Roman',...'FontSize',16, 'Box','off', 'Location','best',...'Position',[.22, .93 - .04*(size(Data, 2) - 1), .1, .04*(size(Data, 2) - 1)]);
更多配色
更多配色可以前往以下fileexchange或者gitee仓库获取:
fileexchange
- Zhaoxu Liu / slandarer (2024). 2000 palettes (https://www.mathworks.com/matlabcentral/fileexchange/126969-2000-palettes), MATLAB Central File Exchange. Retrieved June 22, 2024.
gitee
- https://gitee.com/slandarer/slanColor
当然若是懒得去下载,这里也准备了一系列配色数据,复制使用即可:
function CList = colorDemo(N)
% color demos
C{1} = [0.8627 0.7608 0.47840.6902 0.7255 0.74510.3882 0.3765 0.37250.5961 0.3686 0.36080.6824 0.7490 0.65880.9451 0.6078 0.2039
];
C{2} = [0.1098 0.2000 0.20000.1333 0.3765 0.37650.3882 0.6118 0.64310.8235 0.6784 0.48630.7451 0.4471 0.27060.2745 0.1294 0.1098
];
C{3} = [0.0314 0.0902 0.22750.0039 0.1843 0.32550.0039 0.4275 0.51370.0078 0.6275 0.63530.6745 0.5882 0.47450.9137 0.8706 0.7059
];
C{4} = [0.0235 0.5529 0.61570.3961 0.6588 0.67060.5490 0.7412 0.73730.6784 0.7255 0.63140.7373 0.7608 0.64710.8157 0.6863 0.5176
];
C{5} = [0.8627 0.6549 0.38040.7765 0.7569 0.42750.5451 0.6118 0.58040.3843 0.5490 0.64710.3529 0.4235 0.47840.3176 0.3098 0.3608
];
C{6} = [0.3490 0.3490 0.36080.4510 0.6196 0.67060.1725 0.2118 0.22350.2118 0.3333 0.36860.9765 0.6196 0.57650.7686 0.2392 0.1922
];
C{7} = [0.8588 0.7569 0.59220.2157 0.1765 0.18430.3569 0.4863 0.65490.8588 0.6000 0.25880.5529 0.6824 0.52940.5961 0.3529 0.4431
];
C{8} = [0.8353 0.6824 0.38820.4314 0.4235 0.50590.9686 0.9255 0.84710.2471 0.2235 0.22350.5765 0.6784 0.56470.7882 0.7176 0.5765
];
C{9} = [0.5176 0.4863 0.63920.8941 0.3529 0.35290.9569 0.6510 0.36860.5020 0.4745 0.16860.9490 0.8353 0.43530.1020 0.0706 0.2157
];
C{10} = [0.4314 0.4863 0.72550.4824 0.7373 0.83530.8157 0.8863 0.68630.9608 0.8588 0.60000.9098 0.6118 0.50590.8235 0.5176 0.5529
];
C{11} = [0.8353 0.4784 0.42750.9098 0.7176 0.38430.6118 0.8039 0.87450.3216 0.3137 0.32160.9020 0.8078 0.68630.7294 0.5843 0.4392
];
C{12} = [0.8510 0.3373 0.36080.9490 0.5412 0.54120.9294 0.6627 0.67060.1059 0.7137 0.68630.0314 0.5451 0.74510.0902 0.1569 0.4118
];
CList = C{N};
end
完
以上已经是完整代码,若懒得复制可去以下gitee仓库获取完整文件夹:
- https://gitee.com/slandarer/spdraw