How to add newline to barplot x label?

7 ビュー (過去 30 日間)
regulo rosado
regulo rosado 2020 年 9 月 14 日
コメント済み: regulo rosado 2020 年 9 月 15 日
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.

採用された回答

Walter Roberson
Walter Roberson 2020 年 9 月 14 日
'$\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.
  1 件のコメント
regulo rosado
regulo rosado 2020 年 9 月 15 日
Thank you Walter.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColor and Styling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by