如何在matlab中绘制具有不同颜色和组的条形图
我想绘制一个总结算法性能的条形图。它有三个主要参数如何在matlab中绘制具有不同颜色和组的条形图
- 出版年(x轴)
- 数据类型(栏的颜色)
- 算法评分(杆高度)
下面是一个例子的数据:
dtypes = {'type1','type2','type3'}; %All of the possible data types
methods_name = {'method1','method2','method3'};
methods_accuracy = [89.2, 95.54, 85];
methods_year = [2016, 2017, 2016] ;
methods_dtype = {'type1', 'type2', 'type2'};
在这里,我希望得到3个酒吧,其中2个在2016年用不同的颜色,一个在2017年有一个色彩匹配一个F rom 2016.
出于某种原因,我似乎无法使用bar
函数来执行我想要的操作。这似乎很简单,但我认为我错过了这个功能的工作原理。 使用
bar(methods_year, methods_accuracy)
给出了一个错误:
XData cannot contain duplicate values.
绘制每个类别的多条似乎是通过使用矩阵常见的做。根据你的评论,这似乎不可能。似乎bar()
函数也不能接受颜色矢量。下面的代码应该更通用一些,并允许你根据你的'类型'来指定条的颜色。
我修改了MATLAB Answers论坛(https://ch.mathworks.com/matlabcentral/answers/310039-how-to-change-the-color-of-individual-bars-in-a-bar-chart)上提供的代码以解释您的数据类型并将数据处理成正确的格式。
% code modified from https://ch.mathworks.com/matlabcentral/answers/310039-how-to-change-the-color-of-individual-bars-in-a-bar-chart
% Plots a bar chart and gives a different color to each bar.
% Also plots the value of the bar above the bar.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 10;
format compact
%%%%%%%%%%%%%%%%%%%%% your data here %%%%%%%%%%%%%%%%%%%%%%%%
dtypes = {'type1','type2','type3'}; %All of the possible data types
methods_name = {'method1','method2','method3'};
methods_accuracy = [89.2, 95.54, 85];
methods_year = [2016, 2017, 2016] ;
methods_dtype = {'type1', 'type2', 'type2'};
% sorting all vectors into values
[sort_yrs,inds] = sort(methods_year);
sort_methods = methods_name(inds);
sort_dtype = methods_dtype(inds);
sort_accuracy = methods_accuracy(inds);
colors = zeros([length(sort_methods),3]);
colors_types = [[1,0,0];[0,1,0];[0,0,1]];
for i = 1:1:length(dtypes)
ind1 = strfind(sort_dtype, dtypes{i});
ind2 = find(not(cellfun('isempty', ind1)));
for j = 1:1:length(ind2)
colors(ind2(j),:) = colors_types(i,:);
end
end
%%%%%%%%%%% end of processing data %%%%%%%%%%%%%%
% plotting all bars in a loop
for b = 1 : length(sort_accuracy)
% plot a single bar
handlebar(b) = bar(b, sort_accuracy(b), 'BarWidth', 0.9);
% assigning color to bar
set(handlebar(b), 'FaceColor', colors(b,:));
hold on;
end
% title and axis labels, if desired
title('A title Here', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
% finding locations for tick labels for years, if desired
temp = histc(sort_yrs,unique(sort_yrs));
% locations to use if at the middle of the year
xTickLocs = cumsum(temp) - 0.5*temp;
% locations to use if at the beginning of the year
%xTickLocs = cumsum(temp) - 0.5*temp(1);
xticks(xTickLocs);
xticklabels(unique(sort_yrs));
% addind labels
set(gca, 'XTickLabels', xticklabels);
我看到你做了什么,但是有一个问题,当你有一个具有相同类型的两个方法的特定年份? (这是非常可能的) –
你可以在同一年用两种不同的颜色绘制两个小条。 [这](https://stackoverflow.com/questions/2379230/how-to-construct-unequal-width-histograms-with-matlab)可以帮助 – shamalaia
@itzikBenShabat,我认为这个新的代码将更一般,将得到一个接近你的目标的结果。 –
我得到了解决,包括可能发生的碰撞,但是在组与组之间的杆之间距离可变的价格的任务。这可以吗? – Gryphon
我不知道我明白你的意思。年组之间没有相同的距离是可以的。在每个年份组中,希望条间的间距相等。 –