How to add newline to barplot x label?

7 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 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.

More Answers (0)

Categories

Find more on Color and Styling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!