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

  • Context: MATLAB 
  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    Data Graph Sets
Click For Summary
SUMMARY

This discussion focuses on plotting two sets of data on the same graph in MATLAB while customizing the x-axis increments. The user initially faced an issue with the x-axis displaying 0.5 increments instead of the desired 1. The solution involves using the "hold on" command to overlay plots and adjusting axis parameters as detailed in MATLAB's documentation. Additionally, the area under each curve can be calculated using the trapz or cumtrapz functions, with trapz providing the total area and cumtrapz offering intermediate values.

PREREQUISITES
  • Familiarity with MATLAB plotting functions
  • Understanding of x-axis customization in MATLAB
  • Knowledge of numerical integration techniques in MATLAB
  • Basic programming skills in MATLAB
NEXT STEPS
  • Learn how to customize axis limits and tick intervals in MATLAB
  • Explore the trapz and cumtrapz functions for area calculations
  • Investigate the use of the "hold on" command for multiple plots in MATLAB
  • Review MATLAB's documentation on creating plots for advanced techniques
USEFUL FOR

This discussion is beneficial for data analysts, researchers, and engineers who utilize MATLAB for data visualization and analysis, particularly those needing to plot multiple datasets and perform numerical integration.

Superposed_Cat
Messages
388
Reaction score
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
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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K