draw the centered data on the same axes as the raw data

5 次查看(过去 30 天)
draw the centered data on the same axes as the raw data given 12 samples of the raw data
x y
2 125
7 110
8 287
10 200
6 350
15 280
4 400
13 370
12 480
10 420
19 540
22 518

采纳的回答

Umar
Umar 2024-8-27

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.

更多回答(1 个)

Shashi Kiran
Shashi Kiran 2024-8-23
To plot the "Wire Length" vs. "Die Height" along with their centred data on the same plot, you can follow these steps:
  • Find the centred data
% 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];
% Calculate the means
mean_WireLength = mean(WireLength);
mean_DieHeight = mean(DieHeight);
% Center the data
centered_WireLength = WireLength - mean_WireLength;
centered_DieHeight = DieHeight - mean_DieHeight;
  • 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.
% Plot the raw data
figure;
scatter(WireLength, DieHeight, 'b', 'filled');
hold on;
% Plot the centered data
scatter(centered_WireLength, centered_DieHeight, 'r', 'filled');
% Add labels, title, and legend
xlabel('Wire Length');
ylabel('Die Height');
title('Raw vs Centered Data');
legend('Raw Data', 'Centered Data');
grid on;
% Display the plot
hold off;
You can find the below resources useful.
  1. https://www.mathworks.com/help/matlab/ref/scatter.html?s_tid=doc_ta
  2. https://www.mathworks.com/help/matlab/ref/hold.html
Hope this helps!
  9 个评论
Image Analyst
Image Analyst 2024-8-28
I don't understand. The original poster was @NATASHA, not Nafisa Siddiqui, and the original post didn't mention anything about wire length. What's going on?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by