Centering labels in a bar plot

11 vues (au cours des 30 derniers jours)
L'O.G.
L'O.G. le 22 Jan 2023
Modifié(e) : dpb le 22 Jan 2023
How do I center a string under each bar at in a bar plot? Something like the following is the best I can do, but it's a lot of trial-and-error, and the strings are still off center:
x1 = linspace(1,33,33);
x2 = linspace(0.6,33,33);
figure;
bar(x1,y)
xticks(x2)
xticklabels(names)
xtickangle(75)

Réponse acceptée

dpb
dpb le 22 Jan 2023
Modifié(e) : dpb le 22 Jan 2023
Let bar do the work for you...you didn't supply all the needed code to run the example, but something similar would be
x=1:33; % no need for linspace for integer spacing
y=randi(100,size(x)); % some data to go along with x...
names="Var"+x(:); % make up a a name to go along
names=categorical(names,names,names); % turn into categorical; keep ordering
bar(names,y)
I love to beat up on the bar() function as being a terrible user interface and all, but in this case there's a simple answer.
There is now an example in the bar documentation that illustrates putting a text string at the end of a bar, centered on the bars using the internal properties of the bar object. That's a sizable a step forward from the olden days when it was required to derive that location from the internal data or even worse, when those needed data were made hidden properties for a while...still, it should be a user-settable property, but at least it can be done and is an illustration of the basic "how".

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by