Boxplot with multiline x axis labels (2024)

13 views (last 30 days)

Show older comments

Mark on 4 Oct 2011

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels

Accepted Answer: Fangjun Jiang

Open in MATLAB Online

Hello,

I'm trying to create a boxplot for two groups. Each group's label is multi line. I have tried the approach below, but the first line of each label is drawn on top of the x axis, while the second is drawn below the x axis (please try the code below)

Does anyone know how to make the two lines be drawn below the x axis in a boxplot?

Thanks

%%%%%%%%%%%

Dist1 = rand(10, 1);

Dist2 = rand(10, 1);

group = [repmat(cellstr(sprintf('First Line G1\nSecond Line G1')), length(Dist1), 1); ...

repmat(cellstr(sprintf('First Line G2\nSecond Line G2')), length(Dist2), 1)];

figurePlot = figure('visible', 'on');

boxplot([Dist1;Dist2], group)

ylabel('Y Label');

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Fangjun Jiang on 4 Oct 2011

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#answer_23514

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#answer_23514

Open in MATLAB Online

After your plot, use findobj() to find the handles of the two labels and then change the position.

h=findobj(gca,'type','text');

char(get(h,'String'))

get(h,'Position')

ans =

First Line G2

Second Line G2

First Line G1

Second Line G1

ans =

[1x3 double]

[1x3 double]

6 Comments

Show 4 older commentsHide 4 older comments

Mark on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38647

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38647

Thanks but then how do you set the new position? I tried the following but it does not work:

Dist1 = rand(10, 1);

Dist2 = rand(10, 1);

group = [repmat(cellstr(sprintf('First Line G1\nSecond Line G1')), length(Dist1), 1); ...

repmat(cellstr(sprintf('First Line G2\nSecond Line G2')), length(Dist2), 1)];

figurePlot = figure('visible', 'on');

boxplot([Dist1;Dist2], group)

ylabel('Y Label');

h=findobj(gca,'type','text');

posText = get(h,'Position')

offset = [0 10 0];

set(h,'Position', [posText{1, :} - offset;posText{2, :} - offset]);

Fangjun Jiang on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38650

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38650

You can't use set() like that. It has to be done one by one.

h=findobj(gca,'type','text');

set(h(1),'Position',get(h(1),'Position')-10);

set(h(2),'Position',get(h(2),'Position')-10);

Mark on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38657

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38657

Thanks so much Fangjun. That worked as I wanted. I owe you a beer :-)

Unfortunately, when I try to either print or save the figure, the axis labels go back to their original position. For instance, you can try this and see:

strFName = 'c:\someFig.png';

saveas(figurePlot, strFName);

The image is saved with the labels in the original place.

Any clue as to how to avoid this?

Thanks in advance.

Walter Roberson on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38662

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38662

The print() operation involves a resize callback on the figure to size it to the paper size. Setting the figure PaperPositionMode to manual can help that, as can providing your own figure resize function. What you are doing, though, is inherently a bit weak in the face of resizes.

What I would suggest is that instead of working with \n, that you instead pass in a LaTex or Tex command for each group that encodes the linebreak within it. Then, right after the boxplot(),

set(findobj(gca,'-depth',1,'Type','text'),'Interpreter','latex')

(or tex if you prefer that.)

Fangjun Jiang on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38665

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38665

I had the same problem using saveas() or print(). Walter's suggestion is probably the best option. However, I can't test it at this moment.

Fangjun Jiang on 5 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38755

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38755

I used Tex formatting but it still shows the first line label above the x-axis. So it still requires changing the position to move it down. Then when using saveas(), the label still moves up. Please post another question using the following code, there might be experts on graphics who can help you.

Dist1 = rand(10, 1);

Dist2 = rand(10, 1);

group = [repmat({'First Line G1\newlineSecond Line G1'}, length(Dist1), 1); ...

repmat({'First Line G2\newlineSecond Line G2'}, length(Dist2), 1)];

figurePlot = figure('visible', 'on');

boxplot([Dist1;Dist2], group)

ylabel('Y Label'); grid;

h=findobj(gca,'type','text');

set(h,'Interpreter','tex')

set(h(1),'Position',get(h(1),'Position')-10);

set(h(2),'Position',get(h(2),'Position')-10);

saveas(figurePlot,'test.png');

Sign in to comment.

More Answers (1)

Walter Roberson on 4 Oct 2011

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#answer_23519

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#answer_23519

Using string with newlines in them is not promised to work for any of the MATLAB graphical text operations that I can think of at the moment.

Some graphical operations permit character arrays with multiple rows; some graphical operations permit strings with the lines separated by '|'; some graphical operations permit cell arrays of strings; some permit TeX or LaTeX that include coded indications of line breaks. And some graphical text operations just plain only permit single lines. Sometimes the easiest way to work around the matter is to go in to the object (e.g., axes) properties and bash the usual text object handle to be an annotation object handle.

4 Comments

Show 2 older commentsHide 2 older comments

Fangjun Jiang on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38630

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38630

In this case, the OP is not using newline. cellstr() is used. However, I found that in R2007b, it is displayed in one line. In R2010b, it is displayed in two lines.

Walter Roberson on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38633

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38633

cellstr() does not break the strings at newline characters. A "row" for cellstr purposes is the same thing as a MATLAB array row.

Walter Roberson on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38634

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38634

Did you perhaps miss the sprintf('....\n...') ? That creates a character vector with the \n replaced by char(10) and does *not* create two rows of characters.

Fangjun Jiang on 4 Oct 2011

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38639

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/17440-boxplot-with-multiline-x-axis-labels#comment_38639

Yep. I see you point.

>> a=['abc';'efg'];

A=cellstr(a)

b=sprintf('abc\nefg');

B=cellstr(b)

A =

'abc'

'efg'

B =

[1x7 char]

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and Annotations

Find more on Labels and Annotations in Help Center and File Exchange

Tags

  • boxplot
  • multiline x axis labels

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Boxplot with multiline x axis labels (14)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Boxplot with multiline x axis labels (2024)

FAQs

What should the x-axis of a boxplot be? ›

Box plots are composed of an x-axis and a y-axis. The x-axis assigns one box for each category or numeric variable. The y-axis is used to measure the minimum, first quartile, median, third quartile, and maximum value in a set of numbers. You can use box plots to visualize one or many distributions.

How do you label a boxplot? ›

To label a box plot, you need to find the five number summary for the data set:
  1. the minimum – the least value.
  2. the lower quartile – the middle of the lower half of the data.
  3. the median – the middle of the data set.
  4. the upper quartile – the middle of the upper half of the data.

How to remove x-axis labels in boxplot? ›

After creating the boxplot, use . set() . . set(xticklabels=[]) should remove tick labels.

How do I change the x-axis scale in R boxplot? ›

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

What should be the label for the x-axis? ›

In a chart you create, axis labels are shown below the horizontal (category, or "X") axis, next to the vertical (value, or "Y") axis, and next to the depth axis (in a 3-D chart). Your chart uses text from its source data for these axis labels.

What should the x-axis be? ›

The independent variable belongs on the x-axis (horizontal line) of the graph and the dependent variable belongs on the y-axis (vertical line). The x and y axes cross at a point referred to as the origin, where the coordinates are (0,0).

What is the boxplot rule? ›

A box and whisker plot—also called a box plot—displays the five-number summary of a set of data. The five-number summary is the minimum, first quartile, median, third quartile, and maximum. In a box plot, we draw a box from the first quartile to the third quartile. A vertical line goes through the box at the median.

How do you label a box as fragile? ›

Clients will stress when they ship fragile items. Print “Handle With Care” or “Fragile” and directly affix a label on their boxes to take the stress out of the process.

How do you remove x-axis labels from plot? ›

Remove x or y axis labels: If you want to modify just one of the axes, you can do so by modifying the components of the theme() , setting the elements you want to remove to element_blank() . You would replace x with y for applying the same update to the y-axis. Note the distinction between axis. title and axis.

How do you hide x-axis tick labels? ›

Tip To hide tick marks or tick-mark labels, in the Axis labels box, click None.

How do you rename the x-axis in a boxplot? ›

In R, you can easily change the axis labels of a boxplot by using the xlab and ylab functions. For example, you can specify the x-axis label by using the xlab function and the y-axis label by using the ylab function. You can also use the main argument to set the title of the boxplot.

How do you change the x-axis values in a PLT plot? ›

To set the x-axis values, we use the xticks function. This function takes two arguments: the locations along the x-axis where the ticks will be placed, and the labels for these ticks. In this example, np. arange(0, 11, step=1) generates an array of numbers from 0 to 10 (inclusive) with a step of 1.

How to change x-axis labels in ggplot2 boxplot? ›

Changing axis labels

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

What is the x on a boxplot? ›

Boxplot notation isn't very well standardized, but it's likely that the "X" here indicates the mean of the data being plotted.

What is the x-axis of a dot plot? ›

Dot plot is special 2D scatterplot with X-axis is a categorical variable and Y-axis is a numerical variable, typically used to visualize gene expression (RNA-seq or scRNA-seq), with dots representing observations (samples or cells), X-axis showing treatment groups and y-axis showing expression level on an appropriate ...

How do you specify the x-axis values? ›

Right-click on the X-axis and then click on Format Axis. 3. Now click on Axis Options button and in the Labels option, under Interval between labels, select Specify interval unit and type your desired interval value in the box next to it.

What is the X and y-axis on a plot? ›

The x-axis is a horizontal number line and the y-axis is a vertical number line. These two axes intersect perpendicularly to form the coordinate plane.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6184

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.