Plotting a line over a bar chart

I’ve got a bar chart and I’m trying to plot a horizontal line over the top of it to show a threshold and it’s got me stumped.

What I have

What I want (achieved through image editing)

for Matlab I found this article Combine Line and Bar Charts Using Two y-Axes - MATLAB & Simulink but apparently yyaxis isn’t implemented in octave yet.

Any help would be much appreciated

Octave version: 7.1.0
OS: macOS Monterey 12.2.1

bar (1:5, [4 5 2 1 3]);
hold on;
plot (0:6, [2 2 2 2 2 2 2], 'r-');

Could be one way to get there

1 Like

@arungiridhar provided a valid solution if you want both the line and the bar chart to be referenced on the same y axis. The matlab yyaxis function is meant to be used when you want two separate y axes. In that case you can use plotyy in Octave, e.g.:

x = 1:5;
ybar =  [4 5 2 1 3];
lineval = 3.3;
## Create double y axes with 2 line objects 
[haxes, hline1, hline2] = plotyy (x, ybar, x, lineval * ones (size (x)));

## Delete unused line object and replace with your bar chart on first (left) y axis 
hold (haxes(1), "on")
bar (haxes(1), x, ybar, "facecolor", get (hline1, "color"), "edgecolor", get (hline1, "color"));
delete (hline1)


1 Like

Success, thanks for your help. It was the hold on; that I was missing