Matlab单杠双图
Matlab Horizontal Bar Double Graph
我在 Matlab 中制作了一个水平条形图,其中一条条从左到右:
Horizontal Single Bar Plot
我想添加一个 附加 水平条,在与前一个条相同的水平平面上,这次是从右到左。该条应该停在白点处(带有误差条)。两个水平条应同时存在。
有人知道怎么做吗?如果我使用反转功能,一切都会反转,但我只需要反转新的特定柱而不改变任何其他东西。
这是我的脚本:
figure
Data = [19 26; 1.7 2; 1.8 2]; % 1 Means 2 Error Bars1 3 Errors Bars2
%% Horizontal Bar Graph
barh(Data(1,1),'FaceColor',[0,0.5,0.5],'EdgeColor',[0,0,0],'LineWidth',2);
hold on
x = Data(1,1); %mean Bar 1
e = Data(2,1); %err Bar 1
f = Data(3,1); %err Bar 2
h = herrorbar(x,1,e,f);
set(h(1),'linewidth',6,'Color',[0 0 0]);
%% Point for other bar graph coming from other side
hold on;
plot(Data(1,2),1,'o','LineWidth',2,'MarkerSize',20,'MarkerEdgeColor','k','MarkerFaceColor',[1 1 1])
%% Axis limits etc etc
set(gca,'fontsize',20)
set(gca,'linewidth',3)
axis([10 30 0.5 1.5])
一个可能的解决方案是在 figure
中添加一个新的 axes
,与第一个 figure
在相同的位置和相同的大小。
然后您可以在新轴上绘制第二个水平条。
为了使第二个条形图右对齐,您必须将第二个轴的 XDir
属性 设置为 reverse
将第二个轴的 color
属性 设置为 `none" 允许您不屏蔽第一个柱。
为了从右到左绘制第二个柱直到圆,您可以从圆的位置计算第一个轴的右极限(即其 XLim 的最大值)的距离。
我不确定你想如何评估 errorbar
,所以,在下面的代码中我使用了与第一个相同的设置,你可以很容易地调整它。
在下文中,您可以找到建议解决方案的可能实施方式。
figure
Data = [19 26; 1.7 2; 1.8 2]; % 1 Means 2 Error Bars1 3 Errors Bars2
% Horizontal Bar Graph
barh(Data(1,1),'FaceColor',[0,0.5,0.5],'EdgeColor',[0,0,0],'LineWidth',2);
hold on
x = Data(1,1); %mean Bar 1
e = Data(2,1); %err Bar 1
f = Data(3,1); %err Bar 2
h = herrorbar(x,1,e,f);
set(h(1),'linewidth',6,'Color',[0 0 0]);
% Point for other bar graph coming from other side
hold on;
plot(Data(1,2),1,'o','LineWidth',2,'MarkerSize',20,'MarkerEdgeColor','k','MarkerFaceColor',[1 1 1])
% Axis limits etc etc
set(gca,'fontsize',20)
set(gca,'linewidth',3)
axis([10 30 0.5 1.5])
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ADD THE SECOND AXES AND %
% PLOT THE SECOND BAR %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get first axes position
xp=get(gca,'position')
% Get first axes X Limits
first_ax_lim=get(gca,'xlim')
% Get first axes X span
first_ax_span=first_ax_lim(2)-first_ax_lim(1)
% Create the second axes in the same position of the first one
new_ax=axes('position',xp)
% Get the data for the second bar
new_bar_data=first_ax_lim(2)-Data(1,2)
% Plot the second bar in the second axes
barh(new_bar_data,'FaceColor',[0,0.5,0.5],'EdgeColor',[0,0,0],'LineWidth',2);
% Set the properties of the second axes
set(new_ax,'xdir','reverse','color','none')
% Set the limits of the second axes
axis([0 first_ax_span 0.5 1.5])
% Remove the XTick from the second axes
new_ax.XTick=[]
new_ax.YTick=[]
% Add the error bar on the second axes
hold on
new_h = herrorbar(new_bar_data,1,e,f);
% Set the properties of the second error bar
set(new_h,'linewidth',6,'Color',[0 0 0]);
我在 Matlab 中制作了一个水平条形图,其中一条条从左到右:
Horizontal Single Bar Plot
我想添加一个 附加 水平条,在与前一个条相同的水平平面上,这次是从右到左。该条应该停在白点处(带有误差条)。两个水平条应同时存在。
有人知道怎么做吗?如果我使用反转功能,一切都会反转,但我只需要反转新的特定柱而不改变任何其他东西。
这是我的脚本:
figure
Data = [19 26; 1.7 2; 1.8 2]; % 1 Means 2 Error Bars1 3 Errors Bars2
%% Horizontal Bar Graph
barh(Data(1,1),'FaceColor',[0,0.5,0.5],'EdgeColor',[0,0,0],'LineWidth',2);
hold on
x = Data(1,1); %mean Bar 1
e = Data(2,1); %err Bar 1
f = Data(3,1); %err Bar 2
h = herrorbar(x,1,e,f);
set(h(1),'linewidth',6,'Color',[0 0 0]);
%% Point for other bar graph coming from other side
hold on;
plot(Data(1,2),1,'o','LineWidth',2,'MarkerSize',20,'MarkerEdgeColor','k','MarkerFaceColor',[1 1 1])
%% Axis limits etc etc
set(gca,'fontsize',20)
set(gca,'linewidth',3)
axis([10 30 0.5 1.5])
一个可能的解决方案是在 figure
中添加一个新的 axes
,与第一个 figure
在相同的位置和相同的大小。
然后您可以在新轴上绘制第二个水平条。
为了使第二个条形图右对齐,您必须将第二个轴的 XDir
属性 设置为 reverse
将第二个轴的 color
属性 设置为 `none" 允许您不屏蔽第一个柱。
为了从右到左绘制第二个柱直到圆,您可以从圆的位置计算第一个轴的右极限(即其 XLim 的最大值)的距离。
我不确定你想如何评估 errorbar
,所以,在下面的代码中我使用了与第一个相同的设置,你可以很容易地调整它。
在下文中,您可以找到建议解决方案的可能实施方式。
figure
Data = [19 26; 1.7 2; 1.8 2]; % 1 Means 2 Error Bars1 3 Errors Bars2
% Horizontal Bar Graph
barh(Data(1,1),'FaceColor',[0,0.5,0.5],'EdgeColor',[0,0,0],'LineWidth',2);
hold on
x = Data(1,1); %mean Bar 1
e = Data(2,1); %err Bar 1
f = Data(3,1); %err Bar 2
h = herrorbar(x,1,e,f);
set(h(1),'linewidth',6,'Color',[0 0 0]);
% Point for other bar graph coming from other side
hold on;
plot(Data(1,2),1,'o','LineWidth',2,'MarkerSize',20,'MarkerEdgeColor','k','MarkerFaceColor',[1 1 1])
% Axis limits etc etc
set(gca,'fontsize',20)
set(gca,'linewidth',3)
axis([10 30 0.5 1.5])
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ADD THE SECOND AXES AND %
% PLOT THE SECOND BAR %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get first axes position
xp=get(gca,'position')
% Get first axes X Limits
first_ax_lim=get(gca,'xlim')
% Get first axes X span
first_ax_span=first_ax_lim(2)-first_ax_lim(1)
% Create the second axes in the same position of the first one
new_ax=axes('position',xp)
% Get the data for the second bar
new_bar_data=first_ax_lim(2)-Data(1,2)
% Plot the second bar in the second axes
barh(new_bar_data,'FaceColor',[0,0.5,0.5],'EdgeColor',[0,0,0],'LineWidth',2);
% Set the properties of the second axes
set(new_ax,'xdir','reverse','color','none')
% Set the limits of the second axes
axis([0 first_ax_span 0.5 1.5])
% Remove the XTick from the second axes
new_ax.XTick=[]
new_ax.YTick=[]
% Add the error bar on the second axes
hold on
new_h = herrorbar(new_bar_data,1,e,f);
% Set the properties of the second error bar
set(new_h,'linewidth',6,'Color',[0 0 0]);