draw the centered data on the same axes as the raw data
Accepted Answer
More Answers (1)
- Find the centred data
- Use the `scatter` function to plot both the raw and centred data on the same graph. To overlay these plots on the same axes, utilize the `hold on` command, which retains the current figure and any plots until `hold off` is called.
- https://www.mathworks.com/help/matlab/ref/scatter.html?s_tid=doc_ta
- https://www.mathworks.com/help/matlab/ref/hold.html
9 Comments
Hi @NAFISA SIDDIQUI ,
To achieve your given goal in Matlab, first you need to initialize two arrays, WireLength and DieHeight, which represent the raw data for wire lengths and corresponding die heights.
WireLength = [1, 4, 5, 6, 8, 10, 12, 14, 13, 18, 19, 22]; DieHeight = [125, 110, 287, 200, 350, 280, 400, 370, 480, 420, 540, 518];
Next, implement code which calculates the mean of both datasets and centers the data by subtracting the mean from each data point. This step is crucial for analyzing the data without the influence of the mean.
meanWireLength = mean(WireLength); meanDieHeight = mean(DieHeight); centeredWireLength = WireLength - meanWireLength; centeredDieHeight = DieHeight - meanDieHeight;
Afterwards, set up a figure for plotting. The hold on command allows multiple datasets to be plotted on the same graph.
figure; hold on; % Hold on to plot multiple datasets
The raw data is plotted using blue circles connected by lines. The DisplayName property is set for the legend.
plot(WireLength, DieHeight, 'bo-', 'DisplayName', 'Raw Data'); % Raw data in blue
Similarly, the centered data is plotted using red stars connected by lines. This visual distinction helps in comparing the two datasets effectively.
plot(centeredWireLength, centeredDieHeight, 'r*-', 'DisplayName', 'Centered Data'); % Centered data in red
After plotting both datasets, the figure is finalized by adding labels, a title, a legend, and a grid for better readability.
hold off; % Release the hold on the current figure xlabel('Wire Length'); ylabel('Die Height'); title('Raw and Centered Data Visualization'); legend('show'); grid on; % Add grid for better readability
Please see attached.


Please let me know if you have any further questions.
Hi @ NAFISA SIDDIQUI,
If I comprehend your instructions by looking at the Cartesian coordinate graph displaying all four quadrants on x and y axis. Then, please see modified updated codes below. You can customize the codes based on your preferences.
So, in the first modified code which shows both comparison and centered data on the same plot. The first part of the code remains the same while the visualization part is modified. So, in the visualization step, it creates a scatter plot for both the raw and centered data, using blue and red markers, respectively. The plot is further enhanced with titles, axis labels, and a legend for clarity. Additionally, the axis limits are set so that both datasets are visible, and the axes are positioned at the origin for better interpretation. Finally, a grid is added to the plot, improving the overall readability. Here is first updated code below.
Updated Code#1
% Step 1: Define the raw data WireLength = [1, 4, 5, 6, 8, 10, 12, 14, 13, 18, 19, 22]; DieHeight = [125, 110, 287, 200, 350, 280, 400, 370, 480, 420, 540, 518];
% Step 2: Center the data meanWireLength = mean(WireLength); meanDieHeight = mean(DieHeight);
centeredWireLength = WireLength - meanWireLength; centeredDieHeight = DieHeight - meanDieHeight;
% Step 3: Create the scatter plots figure; % Create a new figure window
% Plot raw data scatter(WireLength, DieHeight, 'filled', 'MarkerFaceColor', 'b'); hold on; % Hold on to the current plot
% Plot centered data scatter(centeredWireLength, centeredDieHeight, 'filled', 'MarkerFaceColor', 'r');
% Step 4: Customize the plot
title('Raw Data and Centered Data');
xlabel('Wire Length');
ylabel('Die Height');
legend('Raw Data', 'Centered Data', 'Location', 'best');
% Step 5: Set axis limits to ensure both plots are visible xlim([-max(WireLength) max(WireLength)]); % Set x-axis limits ylim([-max(DieHeight) max(DieHeight)]); % Set y-axis limits
% Step 6: Add x and y axes ax = gca; % Get current axes ax.XAxisLocation = 'origin'; % Set x-axis to the origin ax.YAxisLocation = 'origin'; % Set y-axis to the origin ax.XColor = 'k'; % Set x-axis color ax.YColor = 'k'; % Set y-axis color
% Step 7: Add a grid for better visualization grid on;
% Step 8: Display the plot hold off; % Release the hold on the current plot
Please see attached plot.

Updated code#2
Comparison of raw data and centered data
% Step 1: Define the raw data WireLength = [1, 4, 5, 6, 8, 10, 12, 14, 13, 18, 19, 22]; DieHeight = [125, 110, 287, 200, 350, 280, 400, 370, 480, 420, 540, 518];
% Step 2: Center the data meanWireLength = mean(WireLength); meanDieHeight = mean(DieHeight);
centeredWireLength = WireLength - meanWireLength; centeredDieHeight = DieHeight - meanDieHeight;
% Step 3: Create the figure window figure; % Create a new figure window
% Step 4: Create the first subplot for raw data
subplot(1, 2, 1); % 1 row, 2 columns, first subplot
scatter(WireLength, DieHeight, 'filled', 'MarkerFaceColor', 'b');
title('Raw Data');
xlabel('Wire Length');
ylabel('Die Height');
xlim([-max(WireLength) max(WireLength)]); % Set x-axis limits
ylim([-max(DieHeight) max(DieHeight)]); % Set y-axis limits
grid on; % Add grid for better visualization
ax1 = gca; % Get current axes for raw data
ax1.XAxisLocation = 'origin'; % Set x-axis to the origin
ax1.YAxisLocation = 'origin'; % Set y-axis to the origin
ax1.XColor = 'k'; % Set x-axis color
ax1.YColor = 'k'; % Set y-axis color
% Step 5: Create the second subplot for centered data
subplot(1, 2, 2); % 1 row, 2 columns, second subplot
scatter(centeredWireLength, centeredDieHeight, 'filled',
'MarkerFaceColor', 'r');
title('Centered Data');
xlabel('Centered Wire Length');
ylabel('Centered Die Height');
xlim([-max(centeredWireLength) max(centeredWireLength)]); % Set
x-axis limits
ylim([-max(centeredDieHeight) max(centeredDieHeight)]); % Set
y-axis limits
grid on; % Add grid for better visualization
ax2 = gca; % Get current axes for centered data
ax2.XAxisLocation = 'origin'; % Set x-axis to the origin
ax2.YAxisLocation = 'origin'; % Set y-axis to the origin
ax2.XColor = 'k'; % Set x-axis color
ax2.YColor = 'k'; % Set y-axis color
% Step 6: Adjust layout for better visibility
sgtitle('Comparison of Raw Data and Centered Data'); % Add a
super title for the figure
Please see attached plot.

Explanation
*The subplot(1, 2, 1) and subplot(1, 2, 2) commands create a 1-row by 2-column grid of subplots. The first subplot is designated for the raw data, while the second subplot is for the centered data.
*Each subplot contains a scatter plot. The first subplot visualizes the raw data in blue, while the second subplot visualizes the centered data in red.
*Each subplot has its own title and axis labels to clearly indicate what data is being represented.
*Axis Limits and Grid: The axis limits are set so that all data points are visible, and grids are added for better readability.
*The sgtitle function is used to add a super title to the entire figure, providing context for the comparison.
The reason for providing two codes is because then another comment will be posted mentioning that I wanted two plots next to each other. So, to make it easy for you, I provided both codes to suit your preferences. Hope this answers your question.
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

