Overlay Line Plot on Bar Graph Using Different Y-Axes

This example shows how to combine a line plot and a bar graph using two different y-axes.

Define the concentration and temperature data collected every 5 days for a 35 day period.

days = 0:5:35;
conc = [515,420,370,250,135,120,60,20];
temp = [29,23,27,25,20,23,23,27];

Use plotyy to display a bar graph of the temperature data and a line graph of the concentration data. Return the two axes handles as ax, the bar graph handle as hBar, and the line plot handle as hLine.

figure
[ax,hBar,hLine] = plotyy(days,temp,days,conc,'bar','plot');

Add a title and axis labels to the graph. Use the axes handles to label the left and right y-axis appropriately.

title('Trend Chart for Concentration')
xlabel('Day')
ylabel(ax(1),'Temperature (^{o}C)')
ylabel(ax(2),'Concentration')

Change the line width and color. To change properties of the line, use its handle.

hLine.LineWidth = 3;
hLine.Color = [0,0.7,0.7];

The graph uses two different y-axes. The left y-axis corresponds to the bar graph. The right y-axis corresponds to the line plot.

See Also

| | |

Was this topic helpful?