Vigor831
Vigor831

Reputation: 105

Matlab Horizontal Bar Double Graph

I made in Matlab a horizontal bar plot, with a bar coming from left to right:

Horizontal Single Bar Plot

I would like to add an additional horizontal bar, on the same horizontal plan of the previous bar, this time coming from right to left. The bar is supposed to stop at the white dot (with its error bars). Both horizontal bars should be present at the same time.

Does anybody have any idea on how to do that? If I use the reverse function everything is reversed, but I need to reverse only the new specific bar without changing anything else.

Here is my script:

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

Upvotes: 1

Views: 322

Answers (1)

il_raffa
il_raffa

Reputation: 5190

A possible solution could be to add a new axes to the figure, in the same position and of the same size of the firt one.

You can then plot the second horizontal bar in the new axes.

In order to have the second bar aligned to the right, you have to set the XDir property of the second axes to reverse

Setting the color property of the second axes to `none" allows you not to mask the first bar.

In order to plot the second bar from right to left up to the circle, you can evalaute the distance from the right limit of the first axex (i. e. the max of its XLim) from the position of the circle.

I'm not sure how you want to evalaute the errorbar, so, in the following code I've used the same settings of the first one, you can easily adapt it.

In the following you can find a possible implementation of the proposed solution.

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]);

enter image description here

Upvotes: 1

Related Questions