Representing certain range of X axis using a single tick in MATLAB plot

4 vues (au cours des 30 derniers jours)
I have the following data:
X = [1,2,3,4,5,6,7];
Y = [10,10,10,10,20,30,40];
bar(X,Y)
For X axis value until 4, the Y axis value is the same which is 10. I want to represent the barplot where the first X axis tick will show 1:4 and Y axis value will show 10. For the other X axis values from 5 to 7 will be represented each using a single tick as usual, for example X axis will be 1:4 5 6 7, so 4 ticks in total in X axis. Please let me know how to do that.

Réponse acceptée

Adam Danz
Adam Danz le 29 Mar 2020
Modifié(e) : Adam Danz le 29 Mar 2020
Two options
X = [1,2,3,4,5,6,7];
Y = [10,10,10,10,20,30,40];
xIdx = [1,5,6,7];
bar(X(xIdx), Y(xIdx))
ax = gca();
ax.XTick = xIdx;
ax.XTickLabel{1} = '1:4';
xIdx = [1,5,6,7];
bar(Y(xIdx))
ax = gca();
ax.XTick = 1:numel(xIdx);
ax.XTickLabel = {'1:4','5','6','7'};
  3 commentaires
Adam Danz
Adam Danz le 29 Mar 2020
Modifié(e) : Adam Danz le 29 Mar 2020
Also see the width variable in bar() if you'd like the 1:4 bar to spread out across all 4 x-values in the first option, although you'd need to either plot it separately or use a grouping variable.
Shubhrajit Bhattacharjee
Shubhrajit Bhattacharjee le 29 Mar 2020
Sounds good, appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by