How to add newline to barplot x label?

7 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 14 Set 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.

Più risposte (0)

Categorie

Scopri di più su Color and Styling in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by