plotting data with different dimension in a 2d line plot

10 次查看(过去 30 天)
I have to plot 2d line plot. the x axis has a scalar value from which are [0, 10, 20, 30, 40, 50] and the y axis data is an array of dimension 1x30. i want to plot these data for 3 different types of load values in a single plot. how to plot these data when the data on x and y are of different dimension?
  2 个评论
Dyuman Joshi
Dyuman Joshi 2024-4-4
"how to plot these data when the data on x and y are of different dimension?"
You can't. You will have to modify the data to be of same dimensions for plotting.
That modification depends on how the x-values and y-values are related.

请先登录,再进行评论。

回答(2 个)

Gayatri
Gayatri 2024-4-4
Hi Stefi,
  • To plot data where the x-axis values are scalars and the y-axis data consists of an array with a different dimension, you need to determine how the x values correspond to the y values.
  • Since your y-axis data is an array of dimension 1x30 and you have 6 x-axis scalar values, it seems like you might be dealing with a situation where the y-axis data should be segmented or grouped in some way to correspond to each x-axis value.
  • Assuming each x-axis scalar value corresponds to a group of 5 y-axis values (since 30 divided by 6 equals 5), you can plot the data by averaging these groups or by selecting a representative value for each group.
  • However, since you mentioned plotting these data for 3 different types of load values in a single plot, let's assume you have three 1x30 arrays for three different loads.
Here is a general approach to plotting this data in MATLAB:
% Example data
x = [0, 10, 20, 30, 40, 50];
y1 = rand(1, 30); % Example y-axis data for Load Type 1
y2 = rand(1, 30); % Example y-axis data for Load Type 2
y3 = rand(1, 30); % Example y-axis data for Load Type 3
% Number of values per group
valuesPerGroup = length(y1) / length(x);
% Preallocate arrays for the mean values
meanY1 = zeros(1, length(x));
meanY2 = zeros(1, length(x));
meanY3 = zeros(1, length(x));
% Calculate mean values for each group
for i = 1:length(x)
startIndex = (i-1) * valuesPerGroup + 1;
endIndex = i * valuesPerGroup;
meanY1(i) = mean(y1(startIndex:endIndex));
meanY2(i) = mean(y2(startIndex:endIndex));
meanY3(i) = mean(y3(startIndex:endIndex));
end
% Plotting
figure;
plot(x, meanY1, '-o', 'DisplayName', 'Load Type 1');
hold on;
plot(x, meanY2, '-x', 'DisplayName', 'Load Type 2');
plot(x, meanY3, '-s', 'DisplayName', 'Load Type 3');
hold off;
xlabel('X-axis Scalar Values');
ylabel('Mean Y-axis Values');
title('Plot of Mean Y Values for Different Load Types');
legend('show');
This script segments the y-axis data into groups corresponding to each x-axis scalar, calculates the mean of each segment, and then plots these mean values. The hold on; command is used to ensure all three load types are plotted on the same figure.
Please refer the below documentation for hold on: https://in.mathworks.com/help/matlab/ref/hold.html
I hope it helps!

Star Strider
Star Strider 2024-4-6
It would help to know what the (1x30) vector has in it, and how it was created.
One option is to reshape it so that it fits with the dimension of the x-vector —
x = [0, 10, 20, 30, 40, 50];
y = sin((0:29)*2*pi/15);
yrs = reshape(y, numel(x), []);
size(yrs)
ans = 1x2
6 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot(x, yrs)
grid
You can choose any three columns you want to plot.
.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by