【问题标题】:Matlab Horizontal Bar Double GraphMatlab单杠双图
【发布时间】:2018-08-11 22:10:47
【问题描述】:

我在 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])

【问题讨论】:

  • 我不确定我是否理解你的问题,顺便说一句,你可以在plot 之后添加herrorbar(Data(1,2),1,e,f); 以画一条来自白色圆圈的线。尝试在您的代码中考虑line。

标签: matlab plot bar-chart graphing


【解决方案1】:

一种可能的解决方案是在figure 中添加一个新的axes,与第一个相同的位置和大小。

然后您可以在新坐标区中绘制第二个水平条。

为了使第二个条向右对齐,您必须将第二个轴的XDir 属性设置为reverse

将第二个坐标区的color 属性设置为“无”允许您不屏蔽第一个条形。

为了从右到左绘制第二个条形到圆,您可以评估从第一个轴的右边界(即其 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]);

【讨论】:

  • 完美!!谢谢!
猜你喜欢
  • 2010-12-14
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2012-02-10
  • 1970-01-01
相关资源
最近更新 更多