How to plot a part of an array?

lets say i have
x = 1:20
and i have this eqution;
y = 2x+3
how to plot (x,y) where x only goes up to 10 while the whole 20 numbers are still avalible on the plot, just as empty space?
In another words i want to adjust the size of my x axis and y axis without it being automatically adjusted by Matlab.

 Respuesta aceptada

Hi, check this:
% Create the x vector
x = 1:20;
% Calculate y based on the equation
y = 2 * x + 3;
% Create a figure and plot the data
figure;
plot(x, y);
% Set the x-axis limits to display the full range of 1 to 20
xlim([1 20]);
% Add labels and a title
xlabel('x');
ylabel('y');
title('Plot of y = 2x + 3');
Thanks!

7 comentarios

Ahmed
Ahmed el 5 de Abr. de 2024
I Appreciate your help but that is not what i intended.
I want the full numbers displayed but the graph itself (the blue line) to stop at x = 10, not to go to the end of the graph if you catch my meaning.
Okay, you want like this right?
x = 1:20;
y = 2*x + 3;
% Plot only the first 10 points
plot(x(1:10), y(1:10), 'b'); % 'b' specifies blue color
% Hold on to the current plot
hold on;
% Set the limits for x and y axes
xlim([1 20]);
ylim([min(y) max(y)]);
% Label the axes
xlabel('x');
ylabel('y');
% Give the plot a title
title('Plot of y = 2x + 3');
% Release the hold on the plot
hold off;
Ahmed
Ahmed el 5 de Abr. de 2024
YES Exactly !
Thank you so much.
Even this is another way of representation, hope this helps you.
% Create the x vector
x = 1:20;
% Calculate y based on the equation for x values up to 10
y = 2 * x(1:10) + 3;
% Create a figure and plot the data
figure;
plot(x(1:10), y, 'LineWidth', 2);
hold on;
% Add remaining x-axis ticks without plotting data points
plot(x(11:end), nan(1,10), 'x', 'Color', [0.5 0.5 0.5]);
% Set the x-axis limits to display the full range of 1 to 20
xlim([1 20]);
% Add labels and a title
xlabel('x');
ylabel('y');
title('Plot of y = 2x + 3');
Manikanta Aditya
Manikanta Aditya el 5 de Abr. de 2024
Your Welcome @Ahmed!
Ahmed
Ahmed el 5 de Abr. de 2024
I always felt that graphs in matlab are tricky. But i started to understand how to do this.
Appreciate it mate.
Manikanta Aditya
Manikanta Aditya el 5 de Abr. de 2024
Great to hear, Hope you become proficient soon :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 5 de Abr. de 2024

Comentada:

el 5 de Abr. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by