How to do different scaling in x in maltab plot (multiple scales in X axis)?

1 view (last 30 days)
sun on 29 Oct 2020
Commented: Mathieu NOE on 4 Nov 2020
Dear friends,
I have a "simple" plot question here, but I cant figure out. In my project, I need to give different scaling in x axis. Because the information in some ranges are not improtant. I made a simple example to demonstrate my question, hope it's clear enough.
Please check the pic I attached. Lets assume my "curve" is this Y = X, X= [0:1:90]. However, the Y information for given X = [A:1:B ] is not improtant, so I just want to SHRINK or make it smaller, so that other range X=[0:A] & [B:90] information will be enlarged and easier to read.
Lets say A=30, B =60, C = 90. (Actually, any number is fine, just trying to make an simple example.) Anyone can advise me how to do it ? thanks in advance.
I tried to do [A:10:B] , but after the plot, the position of A B dont change, so not working :(

Accepted Answer

Ameer Hamza
Ameer Hamza on 29 Oct 2020
There is no built-in command to do such a thing in MATLAB. One of the workarounds is to create multiple axes objects and connect them seamlessly. Refer to my answer here:

More Answers (1)

Mathieu NOE
Mathieu NOE on 29 Oct 2020
forget my first attempt (deleted)
here hopefully a better code.
% let's generate some data
X= [0:1:90];
Y = 2*X;
% A, B segment indexes
indA = (1:31); % take the first 30 values
indB = (61:91); % take the last values
ind_shr = (max(indA):10:min(indB)); % shrinked portion of data
XA = X(indA);
YA = Y(indA);
XB = X(indB);
YB = Y(indB);
X_shr = X(ind_shr);
Y_shr = Y(ind_shr);
% plot vectors / indexes
X_shr_plot = X_shr(1)+(0:length(X_shr)-1);
XB_plot = X_shr_plot(end)+(0:length(XB)-1);
x_vector_ticks = [XA X_shr(2:end-1) XB]; % remove for Xtick labels the first and last element of X_shr (they already appear in XA(end) and XB(1)
for ci = 1:length(x_vector_ticks)
XtickLabel{ci} = num2str(x_vector_ticks(ci));
xlabel('X value')
ylabel('Y value')
Mathieu NOE
Mathieu NOE on 4 Nov 2020
yes, I recognize it's a bit trickky - but the other option does not work on my older matlab.
I'm used to do complicated stuff that is easier now with newer matlab

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by