How to set an axis with arbitrary, but equally spaced, scaling.

조회 수: 52 (최근 30일)
Zucrode
Zucrode 2024년 2월 13일
댓글: Voss 2024년 2월 15일
Hello,
I am attempting to replicate the following plot in a textbook using my own numerical solver. I am having trouble with replicating the x-axis in the plot for comparison since it is a bit odd. See the axes below:
I am aware that you can use Xtick to choose where ticks are located, but this does not affect the spacing of the ticks such that they are equally spaced like in the example above. The closest I am able to get is shown below:
Log scaling does slightly better, but is still not evenly spaced like I would hope:
Unfortunately I have not been able to find a way to change the scaling to be something other than linear or log. I am using MATLAB R2023a.
Does anyone know a way to create the evently spaced type of scaling seen in the first set of axes?

채택된 답변

John D'Errico
John D'Errico 2024년 2월 13일
편집: John D'Errico 2024년 2월 14일
You could not plot versus the numbers 0,1,2,3, but then set the x-tick labels as you wish?
x = [1/3 1/2 1 inf];
xfake = [0 1 2 3];
y = [2 3 5 7];
plot(xfake,y,'-or')
set(gca,Xtick = [0 1 2 3],XTickLabel = x)
  댓글 수: 6
William Rose
William Rose 2024년 2월 15일
@Zucrode, you are welcome. pchip is smooth and spline is smooth, but spline can be non-monotonic (can go backward).
Voss
Voss 2024년 2월 15일
@Zucrode: A side note: to get the labels aligned to the ticks properly, specify labels as text (i.e., a string array or a cell array of chars) instead of numeric. (Notice how the '1' label appears to be quite a bit to the left of the corresponding tick in the plots above.)
ax = subplot(2,1,1);
x=linspace(.2,3.2,180);
xfake = [0 1 2 3];
xlbl = [1/3 1/2 1 inf]; % numeric labels
plot(interp1([1/3 1/2 1 10],xfake,x,'pchip'),x.*x,'-r.')
xlim([0,3])
set(gca,Xtick = xfake,XTickLabel = xlbl)
title('numeric tick labels (original)')
ax(2) = subplot(2,1,2);
x=linspace(.2,3.2,180);
xfake = [0 1 2 3];
xlbl = string([1/3 1/2 1 inf]); % string labels
plot(interp1([1/3 1/2 1 10],xfake,x,'pchip'),x.*x,'-r.')
xlim([0,3])
set(gca,Xtick = xfake,XTickLabel = xlbl)
title('text tick labels')
The problem is that numeric text labels are stored as a char array, which can add white space, affecting the apparent alignment:
ax(1).XTickLabels
ans = 4x8 char array
'0.333333' '0.5 ' '1 ' 'Inf '
Specifying the tick labels as text doesn't have this problem:
ax(2).XTickLabels
ans = 4x1 cell array
{'0.33333'} {'0.5' } {'1' } {'Inf' }
Also, you may want to use text anyway to be able to do things like this:
figure
x=linspace(.2,3.2,180);
xfake = [0 1 2 3];
xlbl = ["1/3" "1/2" "1" "...Infinity"]; % string labels
plot(interp1([1/3 1/2 1 10],xfake,x,'pchip'),x.*x,'-r.')
xlim([0,3])
set(gca,Xtick = xfake,XTickLabel = xlbl)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by