Problem in Plotting bar graph with two y axis (Bars overlapping)

12 views (last 30 days)
Hi, I have the following code:
clc;
close all;
clear all;
sheet ='Src_1MIC_Array1';
dataset = xlsread('AllSource_MicArr1.xlsx',sheet,'A4:T51');
ratio_tau_diff = dataset(:,15);
abs_tau_diff = dataset(:,18);
yyaxis left
bar(ratio_tau_diff,'r')
ylabel('Values of ratio tau diff');
yyaxis right
bar(abs_tau_diff,'b')
xlabel('Mic Array 1 pair combinations')
ylabel('Values of abs tau diff');
title('Relative and Absolute differences for Time delays (Tau) (1st Mic Array)');
end
The problem is i am getting overlapping bar graphs. how can I make suitable changes in a code so that they do not come overlap but separately. Thanks
  4 Comments
Ahmad Bilal
Ahmad Bilal on 26 Sep 2018
I have tried this one but it is showing again not the required result.
yyaxis left
bar(ratio_tau_diff,'r')
ylabel('Values of ratio tau diff');
yyaxis right
bar(abs_tau_diff,'b')
ylim([-0.9 2]);
xlabel('Mic Array 1 pair combinations')
ylabel('Values of abs tau diff');
title('Relative and Absolute differences for Time delays (Tau) (1st Mic Array)');
Can you hep me in this regard??

Sign in to comment.

Answers (1)

jonas
jonas on 27 Sep 2018
The key here is to plot the two sets of bars as grouped. However, since they are on different axes you cannot plot the two data sets at the same time and thus cannot utilize the standard grouping option. Instead, you create fake groups by padding each vector with NaN's.
% Some data to play with
y1=rand(1,5).*10; %row vector with first set of data
y2=rand(1,5); %row vector with second set of data
% Create two axes
ax1=axes;
ax2=axes('xcolor','none','yaxislocation','right','color','none');
linkaxes([ax1 ax2],'x')
% Pad data with nans
y1=[y1;nan(1,numel(y1))];
y2=[nan(1,numel(y2));y2];
% plot data
axes(ax1);hold on
bar(y1')
axes(ax2);hold on
bar(y2','facecolor',[1 0 0])
xlim([0 length(y2)+1])

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!