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

2 次查看(过去 30 天)
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 :(

采纳的回答

Ameer Hamza
Ameer Hamza 2020-10-29
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: https://www.mathworks.com/matlabcentral/answers/601147-extending-specific-y-axis-values

更多回答(1 个)

Mathieu NOE
Mathieu NOE 2020-10-29
hello
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);
figure(1)
plot(XA,YA,'b',X_shr_plot,Y_shr,'--b',XB_plot,YB,'b');grid
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));
end
xticks(0:length(x_vector_ticks)-1)
xticklabels(XtickLabel)
xlabel('X value')
ylabel('Y value')
  2 个评论
sun
sun 2020-11-3
编辑:sun 2020-11-3
hey, friend. Thanks so much for your code and time! It's awesome.
I do saw your style ~ you played tricks on the side of algorithm, and it's truly working! However, there is only one downside, it's not that simple. Buddy, please check another answer here. I applied his way, it's very simple styple. What you do is, you plot 3 different scale, then put them together on one figure.
Thank you again!
Mathieu NOE
Mathieu NOE 2020-11-4
hello
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

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by