フィルターのクリア

Non-uniform axis spacing in a plot

65 ビュー (過去 30 日間)
Gianluca Ligresti
Gianluca Ligresti 2019 年 2 月 1 日
回答済み: Jianye Xu 2022 年 8 月 13 日
Hi all,
I have two vectors: A = [0.5,0.65,0.7,0.66,0.81] and B = [5,10,20,100,1000]
I would like to plot A on the y-axis and B on the x-axis, so that the x-axis has a UNIFORM spacing of the 5 elements in B. Instead, if I simply use plot(B,A) I obtain the usual Matlab plot, in which the point 5,10,20,100 are very close one to the other on the x-axis, while 1000 is very distant.
How can I make this plot, having uniform spacing between the distante values in B?
Thanks
  2 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 1 日
xlim([0 1000])
xticks(linspace(min(B),max(B),numel(B)))
Gianluca Ligresti
Gianluca Ligresti 2019 年 2 月 1 日
Sorry, probably I didn't explain well my problem.
On the x-axis I want to have ONLY the values 5, 10, 20, 100, 1000
Corresponding to 5 I want have 0.5 on the y-axis
Corresponding to 10 I want have 0.65 on the y-axis and so on
But I would like to have on the x-axis an equal distance between the value 5 and 10, between 10 and 20 I would have the same distance, between 20 and 100 I would have the same distance and so on

サインインしてコメントする。

回答 (4 件)

Steven Lord
Steven Lord 2019 年 2 月 1 日
編集済み: Steven Lord 2019 年 2 月 1 日
About the only way you'll be able to get what you want is to lie a little bit.
A = [0.5,0.65,0.7,0.66,0.81];
B = [5,10,20,100,1000];
Plot the A data with X coordinates 1, 2, ... instead of the data in B. Don't worry, we'll use B soon.
plot(A);
Show only the ticks corresponding to those integer coordinates.
xticks(1:length(A));
Set the labels for those ticks to be the elements in B.
xticklabels(string(B))

Star Strider
Star Strider 2019 年 2 月 1 日
Try this:
x = 10.^(rand( 1, 20)*3); % Create Data
y = rand(1, 20); % Create Data
A = [0.5,0.65,0.7,0.66,0.81];
B = [5,10,20,100,1000];
figure
plot(x, y, 'p')
set(gca, 'XScale','log', 'XTick',B, 'XTickLabel',B, 'YTick',sort(A))
The x-axis spacing is not absolutely uniform, however this is as likely as close as it is possible to get to what you want.

KSSV
KSSV 2019 年 2 月 1 日
編集済み: KSSV 2019 年 2 月 1 日
Do interpolation....read about interp1
A = [0.5,0.65,0.7,0.66,0.81] ;
B = [5,10,20,100,1000] ;
plot(B,A,'*r')
hold on
Bi = linspace(min(B),max(B),100) ; % or you may use Bi = min(B):10:max(Bi)
Ai = interp1(B,A,Bi) ;
plot(Bi,Ai,'.-b')
  2 件のコメント
Gianluca Ligresti
Gianluca Ligresti 2019 年 2 月 1 日
Sorry, probably I didn't explain well my problem.
On the x-axis I want to have ONLY the values 5, 10, 20, 100, 1000
Corresponding to 5 I want have 0.5 on the y-axis
Corresponding to 10 I want have 0.65 on the y-axis and so on
But I would like to have on the x-axis an equal distance between the value 5 and 10, between 10 and 20 I would have the same distance, between 20 and 100 I would have the same distance and so on
KSSV
KSSV 2019 年 2 月 1 日
編集済み: KSSV 2019 年 2 月 1 日
That what the bove code does....or may be this fileexchange might be of your interest:

サインインしてコメントする。


Jianye Xu
Jianye Xu 2022 年 8 月 13 日
Firstly, turn x-data to categorical data. Then, use plot():
A = [0.5,0.65,0.7,0.66,0.81];
B = [5,10,20,100,1000];
B_cat = categorical(B);
figure
plot(B_cat,A)
Hope that helps.

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by