在条形图中具有相同颜色的组并在条形图中具有图例元素
Having groups of the same color in a bar graph and having the legend elements in the bars
我有一个条形图,如下所示,x
是一个 8x3 矩阵。我的代码是
x=[0.2193 0.2281 0;
0.193 0.1404 0;
0.2045 0.1875 0.159;
0.0625 0.0568 0;
0.1993 0.1554 0.1318;
0.0878 0.0034 0;
0.1369 0.1103 0.1027;
0.0951 0.076 0];
x0=10;
y0=10;
width=1200;
height=500;
set(gcf,'position',[x0,y0,width,height])
a=bar(x,'BarWidth',0.9);
a(1).FaceColor=[0.9290, 0.6940, 0.1250];
a(2).FaceColor=[0, 0.4470, 0.7410];
a(3).FaceColor=[0.4660, 0.6740, 0.1880];
lgd=legend('Method 1','Method 2','Method 3');
title('False Negative Rates')
xlabel('Clusters')
ylabel('False Negative Rate')
xticklabels({'Cl1 - PRed','Cl1 - PYellow','Cl2 - PRed','Cl2 - PYellow', 'Cl3 - PRed','Cl3 - PYellow', 'Cl4 - PRed','Cl4 - PYellow'})
saveas(gcf,'False Negatives.png')
如您所见,图表中有 8 个组。
我需要用这张图做三件事:
- 第 1、3、5 和 7 组应为红色,其他为黄色。
- 我需要在每个条形图内写下图例元素。如果它不适合内部,则以垂直方式在其上方。红色条为白色字体,黄色条为黑色字体。
- 如果值为0,则在横轴上用一条线表示。
我经历过这个,但不能类似地申请。
我怎样才能实现其中的任何一个或所有目标?
您需要按照以下算法正确获取图表。
- 分别循环 x 矩阵中的每个元素。
- 并为每个元素绘制相应的条形图并坚持下去。
- 检查 switch-case 中的每个列号,并相应地将图例垂直放置在条形图上方。
- 接下来,检查行号是否为奇数,如果是,则将条形颜色更改为红色,将图例颜色更改为白色。在此步骤中,根据条形长度固定图例 inside/outside 条形的位置。
- 如果行是偶数,则将条形颜色更改为黄色并将图例颜色更改为黑色,并根据条形长度相应地固定图例位置。
- 最后,检查x条长度是否为0,如果是则增加Line-Width并向上滑动条的图例。
- 增加柱的间隙变量并关闭内循环。
- 再次增加柱的间隙变量并关闭外循环。
- 按住并放置标题、刻度和标签。
下面给出了要做的代码。
clc
close all
% declare the points, for the bars
x=[0.2193 0.2281 0;
0.193 0.1404 0;
0.2045 0.1875 0.159;
0.0625 0.0568 0;
0.1993 0.1554 0.1318;
0.0878 0.0034 0;
0.1369 0.1103 0.1027;
0.0951 0.076 0];
% declare the figure width and height
x0=10;
y0=10;
width=1200;
height=500;
% create the figure
set(gcf,'position',[x0,y0,width,height]);
% get the number of rows and column in x points
[row, col] = size(x);
% variable to decide the gap between the bars
p = 1;
%%%%%% Loop for Rows %%%%%%%%
for r = 1:row
%%%%%%%%%% loop for each elements in each row %%%%%%%
% loop for columns %
for c = 1:col
% draw the individual bar
a = bar(p, x(r,c), 'BarWidth',0.9); hold on
% switch the column value
switch(c)
% if column is 1 then create bar legend as "Method 1"
% vertically just above the bar
case 1
t1 = text(p,x(r,c),'Method 1','rotation',90);
% if column is 2 then create bar legend as "Method 2"
% vertically just above the bar
case 2
t1 = text(p,x(r,c),'Method 2','rotation',90);
% if column is 3 then create bar legend as "Method 3"
% vertically just above the bar
case 3
t1 = text(p,x(r,c),'Method 3','rotation',90);
end
% Now check whether the row is odd
% if yes then change the bar color to Red
if(rem(r, 2) ~= 0)
set(a, 'FaceColor', 'r')
% Change the legend color to white in red bars
t1.Color = 'White';
% check if x value is equals or greaten than
% 0.05, it means the legend fit inside the bars
if(x(r,c) >= 0.05)
% slide down the legend into the bars
t1.Position = [p, x(r,c)-0.04];
else
% else change the color of the
% legend to black and keep the
% position unchanged, because if the
% position remains outside the red bars
% then the legend would not be visible due
% to the color white and hence change it to black
t1.Color = 'black';
end
else
% else, it means row is even then
% change the bar color to yellow
set(a, 'FaceColor', 'y')
% if bar color is yellow then change the legend
% color to black
t1.Color = 'black';
% check if length of bar is greater than 0.05
% if yes slide the legend position inside the bar
if(x(r,c) >= 0.05)
t1.Position = [p, x(r,c)-0.04];
end
end
% finally, check if x is equal to 0
% then make the line width of bar to
% 2 points, to make it more visible
% and slide up the legend above the bar
% to few fractional points to make it more ligible
if(x(r,c) == 0)
a.LineWidth = 2;
t1.Position = [p, x(r,c)+0.002];
end
% increment the legend gap as y-axis
p = p+1;
end
%%%%% END the Column loop %%%%%%%%%
% on each row end increment the gap of bars to
% make it categorized as each row.
p = p+1;
end
%%%%%%%%% END the Row loop %%%%%%%%%%%
hold off
% set the title
title('False Negative Rates')
% set the labels
xlabel('Clusters')
ylabel('False Negative Rate')
% mark the xticks to put the xticklables
% in next line
xticks([2:4:32])
% change the xtick labels at corresponding xticks
xticklabels({'Cl1 - PRed','Cl1 - PYellow','Cl2 - PRed','Cl2 - PYellow', 'Cl3 - PRed','Cl3 - PYellow', 'Cl4 - PRed','Cl4 - PYellow'})
saveas(gcf,'False Negatives.png')
输出
我有一个条形图,如下所示,x
是一个 8x3 矩阵。我的代码是
x=[0.2193 0.2281 0;
0.193 0.1404 0;
0.2045 0.1875 0.159;
0.0625 0.0568 0;
0.1993 0.1554 0.1318;
0.0878 0.0034 0;
0.1369 0.1103 0.1027;
0.0951 0.076 0];
x0=10;
y0=10;
width=1200;
height=500;
set(gcf,'position',[x0,y0,width,height])
a=bar(x,'BarWidth',0.9);
a(1).FaceColor=[0.9290, 0.6940, 0.1250];
a(2).FaceColor=[0, 0.4470, 0.7410];
a(3).FaceColor=[0.4660, 0.6740, 0.1880];
lgd=legend('Method 1','Method 2','Method 3');
title('False Negative Rates')
xlabel('Clusters')
ylabel('False Negative Rate')
xticklabels({'Cl1 - PRed','Cl1 - PYellow','Cl2 - PRed','Cl2 - PYellow', 'Cl3 - PRed','Cl3 - PYellow', 'Cl4 - PRed','Cl4 - PYellow'})
saveas(gcf,'False Negatives.png')
如您所见,图表中有 8 个组。
- 第 1、3、5 和 7 组应为红色,其他为黄色。
- 我需要在每个条形图内写下图例元素。如果它不适合内部,则以垂直方式在其上方。红色条为白色字体,黄色条为黑色字体。
- 如果值为0,则在横轴上用一条线表示。
我经历过这个,但不能类似地申请。
我怎样才能实现其中的任何一个或所有目标?
您需要按照以下算法正确获取图表。
- 分别循环 x 矩阵中的每个元素。
- 并为每个元素绘制相应的条形图并坚持下去。
- 检查 switch-case 中的每个列号,并相应地将图例垂直放置在条形图上方。
- 接下来,检查行号是否为奇数,如果是,则将条形颜色更改为红色,将图例颜色更改为白色。在此步骤中,根据条形长度固定图例 inside/outside 条形的位置。
- 如果行是偶数,则将条形颜色更改为黄色并将图例颜色更改为黑色,并根据条形长度相应地固定图例位置。
- 最后,检查x条长度是否为0,如果是则增加Line-Width并向上滑动条的图例。
- 增加柱的间隙变量并关闭内循环。
- 再次增加柱的间隙变量并关闭外循环。
- 按住并放置标题、刻度和标签。
下面给出了要做的代码。
clc
close all
% declare the points, for the bars
x=[0.2193 0.2281 0;
0.193 0.1404 0;
0.2045 0.1875 0.159;
0.0625 0.0568 0;
0.1993 0.1554 0.1318;
0.0878 0.0034 0;
0.1369 0.1103 0.1027;
0.0951 0.076 0];
% declare the figure width and height
x0=10;
y0=10;
width=1200;
height=500;
% create the figure
set(gcf,'position',[x0,y0,width,height]);
% get the number of rows and column in x points
[row, col] = size(x);
% variable to decide the gap between the bars
p = 1;
%%%%%% Loop for Rows %%%%%%%%
for r = 1:row
%%%%%%%%%% loop for each elements in each row %%%%%%%
% loop for columns %
for c = 1:col
% draw the individual bar
a = bar(p, x(r,c), 'BarWidth',0.9); hold on
% switch the column value
switch(c)
% if column is 1 then create bar legend as "Method 1"
% vertically just above the bar
case 1
t1 = text(p,x(r,c),'Method 1','rotation',90);
% if column is 2 then create bar legend as "Method 2"
% vertically just above the bar
case 2
t1 = text(p,x(r,c),'Method 2','rotation',90);
% if column is 3 then create bar legend as "Method 3"
% vertically just above the bar
case 3
t1 = text(p,x(r,c),'Method 3','rotation',90);
end
% Now check whether the row is odd
% if yes then change the bar color to Red
if(rem(r, 2) ~= 0)
set(a, 'FaceColor', 'r')
% Change the legend color to white in red bars
t1.Color = 'White';
% check if x value is equals or greaten than
% 0.05, it means the legend fit inside the bars
if(x(r,c) >= 0.05)
% slide down the legend into the bars
t1.Position = [p, x(r,c)-0.04];
else
% else change the color of the
% legend to black and keep the
% position unchanged, because if the
% position remains outside the red bars
% then the legend would not be visible due
% to the color white and hence change it to black
t1.Color = 'black';
end
else
% else, it means row is even then
% change the bar color to yellow
set(a, 'FaceColor', 'y')
% if bar color is yellow then change the legend
% color to black
t1.Color = 'black';
% check if length of bar is greater than 0.05
% if yes slide the legend position inside the bar
if(x(r,c) >= 0.05)
t1.Position = [p, x(r,c)-0.04];
end
end
% finally, check if x is equal to 0
% then make the line width of bar to
% 2 points, to make it more visible
% and slide up the legend above the bar
% to few fractional points to make it more ligible
if(x(r,c) == 0)
a.LineWidth = 2;
t1.Position = [p, x(r,c)+0.002];
end
% increment the legend gap as y-axis
p = p+1;
end
%%%%% END the Column loop %%%%%%%%%
% on each row end increment the gap of bars to
% make it categorized as each row.
p = p+1;
end
%%%%%%%%% END the Row loop %%%%%%%%%%%
hold off
% set the title
title('False Negative Rates')
% set the labels
xlabel('Clusters')
ylabel('False Negative Rate')
% mark the xticks to put the xticklables
% in next line
xticks([2:4:32])
% change the xtick labels at corresponding xticks
xticklabels({'Cl1 - PRed','Cl1 - PYellow','Cl2 - PRed','Cl2 - PYellow', 'Cl3 - PRed','Cl3 - PYellow', 'Cl4 - PRed','Cl4 - PYellow'})
saveas(gcf,'False Negatives.png')
输出