Documentation Center

  • Trial Software
  • Product Updates

Overlay Line Plot on Bar Graph Using Two y-Axes

This example shows how to combine a bar graph and a line plot 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 hAxes, the bar graph handle as hBar, and the line plot handle as hLine.

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

Change the line width and color using the line handle. Add circle markers to the line at each data point.

set(hLine,'LineWidth',2,'Color',[0,0.7,0.7],'Marker','o');

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(hAxes(1),'Temperature (^{o}C)')
ylabel(hAxes(2),'Concentration')

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?