How can I name the x-axis elements on a plot?

10 ビュー (過去 30 日間)
Pietro Fiondella
Pietro Fiondella 2022 年 6 月 23 日
コメント済み: Pietro Fiondella 2022 年 6 月 23 日
I would like to obtain a vector that summarize a certain quantity over all the months.
I want to obtain the name of each month on te x-axis elements but it's not possible whit my script.
I can obtain the graph but then i'm not able to edit the x-element's name.
M=[1 2 3 4 5 6 7 8 9 10 11 12];
Q=[4 8 6 5 3 15 26 17 40 21 19 10];
plot(M,Q)
I try this but it dosent work.
Months={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
Q=[4 8 6 5 3 15 26 17 40 21 19 10 30];
plot(Months,Q)
Error using plot
Invalid data argument.
  2 件のコメント
KSSV
KSSV 2022 年 6 月 23 日
編集済み: KSSV 2022 年 6 月 23 日
Your M and Q should be of same size. In your case M is 1X12 and Q is 1X13, thats is why you are getting error.
Pietro Fiondella
Pietro Fiondella 2022 年 6 月 23 日
Thaks but that was just my error, now i 've edit the questione and you see the problem with the script.

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

採用された回答

Karim
Karim 2022 年 6 月 23 日
Here you go, you can overwrite the ticks with a string:
% data
M = [1 2 3 4 5 6 7 8 9 10 11 12];
Months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
Q = [4 8 6 5 3 15 26 17 40 21 19 10];
% Plot the figure
figure
plot(M,Q)
% Define the tick locations, and label them
set(gca,'XTick',M)
set(gca,'XTickLabel',Months)
ylabel('Y data')
xlabel('Month of the year')
  1 件のコメント
Pietro Fiondella
Pietro Fiondella 2022 年 6 月 23 日
Thank you KASR!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by