How to add newline to barplot x label?

13 Ansichten (letzte 30 Tage)
regulo rosado
regulo rosado am 14 Sep. 2020
Kommentiert: regulo rosado am 15 Sep. 2020
I've been searching for some time now, and there seems to be no posible way to add labels to the columns of a barplot which may include more than one line. Or at least, set the labels to wrap text to avoid overlaping. My code:
y = [100 200 300];
b = bar(y);
grid on
hold on
name = {'label1','label1'; ...
'label2','label2'; ...
'label3','label3'};
set(gca,'xticklabel',name)
bar(1,y(1),'r');
bar(2,y(2),'g');
bar(3,y(3),'b');
ylim([0,400]);
ylabel('Units [u]');
text(1:length(y),y,num2str(y',4),'vert','bottom','horiz','center');
hold off
What I need is to be able to have a bar plot labeled like this one:
( I edited the bar labels on paint)
I want to label the bars this way because my bar labels are long.
I thank you in advance for your time.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 14 Sep. 2020
'$\begin{array}{c}upper\\lower\end{array}$'
You will need to set the axes TickLabelInterpreter property to 'latex'
For each column you want, you will need to add another character to the {c} portion. The 'c' stands for "centered"; you can also use "l" (left) and "r" (right). \\ marks the break. Put && between column entries.

Weitere Antworten (0)

Kategorien

Mehr zu Color and Styling finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by