How to change the origin from (0,0) to different coordinates in a plot

144 vues (au cours des 30 derniers jours)
FD
FD le 18 Avr 2019
Réponse apportée : Phong le 28 Mai 2023
I have two datasets with 1100x1 double. The rows represent different time points, while the values within each cell of the dataset represent a certain width. Therefore, there are 1100 time points. Matlab treats these data as starting from time zero. However, recording started from time -100.
When I plot these data on the same figure (with Matlab 2018a)
plot(x,'-b');
hold on
plot(y, '-r');
the x and y axis are automatically placed on the bottom (the x axis) and left (y axis) of the plot. I would like to move the axes to time point 0 and width 0.
I can move the x axis with:
ax.XAxisLocation = 'origin';
However, I cannot do the same for the y axis, as Matlab uses as 'origin' for the y axis the default (0,0), rather than the (100,0).
I know that I could simply draw a new line to create the new y axis with
plot([100 100], ylim, 'k-', 'LineWidth',1);
but I want to keep ticks and tick labels as they are in the default settings.
How can I move the y axis? Is thre a way to do something like this: ax = gca; ax.YAxisLocation = (ax.XLim(1)+100); %which doesn't working

Réponses (3)

Hassan Nawaz
Hassan Nawaz le 18 Avr 2019
Hi, You can try using the axis command. With this you can adjust as per your desire how the overall posittive and negative range will look like. the syntax is as
axis([-x +x -y +y])

Kelly Kearney
Kelly Kearney le 18 Avr 2019
You don't need to move the axis; you need to provide x-values for your data.
Right now, you're using the ydata-only option for plot. When you do that, it assumes that the x values are 1:length(y). So
plot(x,'-b');
is the equivalent of
plot(1:length(x), x, '-b');
Instead, provide the real x values. In your case, that's probably something like:
t0 = -100;
dt = 0.01; % or whatever the spacing is in your points.
t = t0 + (0:length(x)-1).*dt;
plot(t, x, '-b')

Phong
Phong le 28 Mai 2023
% I'm Nguyen Tien Phong, Hung Yen University of Technical Education
%This is an application program to plot multiple graphs on the same axes
%You guys edit it to be beautiful and suitable for multi-paragraph math problems
%Ngtienphong126@gmail.com
%Good luck
clear all
syms z
y1=3*z^3+2 % Ve tu 0 den 10 Plot this graph from 0 to 10; starting position is 0
y2=3*z^4+2*z^2+6 % Ve tu 10 den 25 (0-10-10-15) Plot this graph from 0 to 15; starting position is 10
L1=10
L2=15
Xtong=linspace(0,L1+L2,20) % Chia thanh 20 doan boi 20 diem de cbi ve cho tat ca
Yt=zeros(1,20)% Tao ra de ve cac diem chia tren z cua tat ca cac doan i
X2=linspace(0,L1,100); % Chia doan 1 thanh 100 diem de ve do thi cho doan 1
A2=subs(y1,z,X2) % Nhan gia tri tai 100 diem de ve do thi cho doan 1
plot(Xtong,Yt,'Linewidth',1) % X tong la tong chieu dai de xac dinh chieu dai truc Z khi ve
hold on
plot(X2,A2,'g','Linewidth',4) % Do thi doan 1
Xve2=linspace(10,25,100); % Chia thanh 100 diem cho doan i=2 tro di
XXX2=linspace(0,L2,100)
B2=subs( y2,z,XXX2) % Nhan 100 gia tri de ve duong do thi
plot(Xve2,B2,'r','Linewidth',4); % Ve do thi
xlabel('Duong truc Z');
title(['Bieu do Nz ', ': kN'])

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by