MATLAB-Plot two sets of data on the same graph with custom increments

In summary, to plot two sets of data on the same graph and customize the increments, use the "hold on" and "hold off" commands and adjust the axis parameters. To calculate the area under each curve, use the trapz or cumtrapz functions.
  • #1
Superposed_Cat
388
5
Hi, I was trying to plot two sets of data on the same graph and this was my solution but this generates a graph with the x-axis in 0.5 increments where I wanted 1. How do I plot two sets of data on the same graph and customize the increments. Thanks in advance. Also, how would I make MATLAB calculate the area under each curve?
Code:
x = [1 2 3 4 5];

y2 =[84,86,84,81,84,]; 
%72,84];
y=[74,85,89,96,99]; 

plot(x, [y(:) y2(:)])
 
Physics news on Phys.org
  • #2
In general you plot multiple things in the same figure window by using the "hold on" command. This keeps the current figure open and active for future plot commands (it plots overtop of what's already there). When you need a new figure, just type "hold off" and new plots will be done in a new window.

This page explains how to set the axis limits and tick intervals.
http://www.mathworks.com/help/matlab/creating_plots/setting-axis-parameters.html

Since you have discrete data, use the trapz(x,y) or cumtrapz(x,y) functions to calculate the area under each curve. The difference between the two is that trapz() just gives you the final area, whereas cumtrapz gives you the intermediate values as well.
http://www.mathworks.com/help/matlab/math/integration-of-numeric-data.html
http://www.mathworks.com/help/matlab/ref/trapz.html
http://www.mathworks.com/help/matlab/ref/cumtrapz.html
 

1. How do I plot two sets of data on the same graph using MATLAB?

To plot two sets of data on the same graph in MATLAB, you can use the "hold on" command. This allows you to plot multiple sets of data on the same figure. You can then use the "plot" function to plot each set of data, specifying different colors or markers to differentiate between them.

2. Can I customize the increments on the x and y axes when plotting in MATLAB?

Yes, you can customize the increments on the x and y axes when plotting in MATLAB. You can use the "xlim" and "ylim" functions to set the range of values for each axis, and the "xticks" and "yticks" functions to specify the interval between tick marks on each axis.

3. How can I add a legend to a plot with multiple sets of data in MATLAB?

To add a legend to a plot with multiple sets of data in MATLAB, you can use the "legend" function. This allows you to specify labels for each set of data and place the legend in a desired location on the graph.

4. Is it possible to plot data with different units on the same graph in MATLAB?

Yes, it is possible to plot data with different units on the same graph in MATLAB. You can use the "plotyy" function, which allows you to plot two sets of data with different y-axes. This is useful when one set of data has a significantly different range of values compared to the other set.

5. Can I save a plot with two sets of data as an image in MATLAB?

Yes, you can save a plot with two sets of data as an image in MATLAB. You can use the "saveas" function to save the current figure as an image, specifying the desired file format (e.g. png, jpg, etc.). You can also use the "print" function to save the figure as an image with more customization options.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
143
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top